gmp + c++: assign rationals



  • I have initialize a rational and two integers. I want to assign to it different values:
    mpq_class a;
    int b;
    inc c;
    if (...){
    ... calculate b,c;
    assign a=b/c;
    }
    else {
    ... calculate b,c;
    assign a=b/c
    }

    So I want to assign to a, b/c but not wenn I initialize it but at an other point of the program. How can I do that?



  • you can do it by mpq_class a (b,c) also if you have already defined it by mpq_class a;



  • math student schrieb:

    I have initialize a rational and two integers. I want to assign to it different values:
    mpq_class a;
    int b;
    inc c;
    if (...){
    ... calculate b,c;
    assign a=b/c;
    }
    else {
    ... calculate b,c;
    assign a=b/c
    }

    So I want to assign to a, b/c but not wenn I initialize it but at an other point of the program. How can I do that?

    if the implementor of mpq_class has defined an mpq_class::operator=(mpq_class const&), you could assign a temporary object of that class to 'a' and initialize that temporary with b and c:

    a = mpq_class(b, c);
    

Anmelden zum Antworten