spire icon indicating copy to clipboard operation
spire copied to clipboard

math.log estimation broken for BigDecimal (scala 2.13)

Open xmelko91 opened this issue 6 years ago • 3 comments

Trying to estimate log(BigDecimal) starts infinite loop for some random BigDecimals values, trying to test it and get this:

timer.start()
  for(_ <- 1 to 100){
    val bd = BigDecimal(math.random() * 100)
    print(s"for BigDecimal $bd")
    println(s" result is ${ln(bd)}")
    timer.restartTimer
  }

output:

for BigDecimal 95.43783506046007 result is 4.5584750937568150091505279932085940
for BigDecimal 65.74758834870731 result is 4.1858229912780658998029987138361140
for BigDecimal 19.975121517921245 result is 2.9944875751342717477239494914685500
for BigDecimal 35.87874081671405 result is 3.5801449422802959968367744428563220
for BigDecimal 57.00977196205218
1
2
3
4
5...

With Double values it works perfectly.

xmelko91 avatar Sep 27 '19 08:09 xmelko91

@xmelko91 Thanks for the bug report. I can't fix the issue right now.

In general, I think we need to be careful with BigDecimal support in Spire, due to the use of MathContext which does not fit exactly with the principles adopted by Spire.

You may want to have a look at https://github.com/eobermuhlner/big-math

(Also for us, for future reference when fixing this)

denisrosset avatar Oct 06 '19 22:10 denisrosset

See also report in https://github.com/typelevel/spire/issues/1051.

armanbilge avatar May 27 '22 15:05 armanbilge

Since #1051 is closed, copy the suggestion here.

@armanbilge spire is a type level math repo. So I think we can make log(Int, Int) different from log(double, double).
In BigDecimal 1 means 1.00000000 and in Int 1 just means 1. This in scala is easy and it's a worthwhile job.

djx314 avatar Jan 25 '23 01:01 djx314