spire icon indicating copy to clipboard operation
spire copied to clipboard

BUG: `Real.toRational` hangs on `pow` function

Open hmf opened this issue 10 years ago • 6 comments

Trying to use the Real to calculate an exponential using the latest stable version (0.10.1). When I create the type no problem. Its when the toRational is executed, whether directly or not, that it hangs. Examples (all those in comments will hang):

    //val r2 = Real(2).fpow(Real(3))
    //val r2 = Real.e.fpow(Real(3))
    //val r2 = Real(2).pow(3.0)
    //val r2 = Real(2.0).pow(3.0)
    //val r2 = Real(2).fpow(3)
    val r2 = Real(2).pow(3)

    val s = r2.toRational // hangs here

The power function seems to work only when the exponent is an integer. I tracked this down to the class:

case class Inexact(f: Int => SafeLong) 

When the applyis invoked it enters the None (second) case. Then it execute f(p) and Inexact.apply is called again. The looping seems to start here.

This is a show stopper for me. Any way I can get around this?

hmf avatar Aug 08 '15 18:08 hmf

@hmf Ouch! I'll investigate now. Thanks for reporting.

non avatar Aug 08 '15 19:08 non

@non I have tried to search a little more and it seems to lead to Rational@90:

def limitToInt: Rational = limitTo(BigInt(Int.MaxValue)) 

I now wonder if this is not the same issue as #393

hmf avatar Aug 08 '15 19:08 hmf

OK, I see what is happening. It is an easy bug to fix.

As a work-around you can break apart a rational number and apply the .pow and .nroot calls separately. I'll try to get a fix released ASAP.

non avatar Aug 08 '15 19:08 non

Yeah, it's definitely related to a problem with .limitToInt. In the case we care about it is easy to fix -- I'll have to see if it fixes that bug too.

non avatar Aug 08 '15 19:08 non

I'll try to get a release out with a fix for this ASAP.

non avatar Aug 08 '15 20:08 non

Thank you very much.

hmf avatar Aug 10 '15 06:08 hmf