commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles <>
Subject Re: Math Fraction, reducing gcd() calls
Date Tue, 08 Apr 2014 11:16:44 GMT

On Tue, 08 Apr 2014 09:25:32 +0200, MARTEL Leo wrote:
> Hello everyone,
> I'm writing here to get help with an application I
> am developing using the Fraction class from org.apache.commons.math.
> But, since I am French, my English is quite bad ! I apologize, for 
> this
> and I hope, you will understand me anyway.
> Here is my problem:
> I
> need the rational numbers for my application but I have performances
> issues. I found out, using profiling (visualVM), that most of the CPU
> time was spent in the gcd() method since it is called every time a
> Fraction is instantiated and that it's the harder part of the
> computation. Moreover, a new instance is created each time an 
> operation
> (+ - * /) is called. That makes a lot of calls to gcd().
> In my
> application, most of the computations are : A + B * (C - D). Each 
> time,
> there are 3 operations so 3 calls to gcd().
> My questions are:
> Is it
> possible to force the library to call gcd() only once, at the end of 
> the
> formula ?

I think that calling "gcd" ensures that the result will not overflow 
the largest possible range of arguments.

It's part of the code logic.

Maybe you could write methods that implement the "naive" algorithms for
addition, subtraction, ...
Then test whether you always get correct results in your application.

> If i manage to do that, will it be faster? Or will the lone
> gcd() last the same time that the three previous gcd() together?

I guess that it depends on the magnitude of the resulting numerator
and denominator...

Best regards,

> Thank
> you for your answers, and again, my apologies for the bad English.
> --
> Léo Martel - Software Engineer
> Institut Supérieur de l'Aéronautique et
> de l'Espace (ISAE)

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message