typelevel/spire

math.log can not calculate some result.

Closed this issue · 3 comments

import spire.implicits._
import spire.math._
val list1 = for {
  i1 <- LazyList.from(2 to 40)
  i2 <- LazyList.from(1 to 4000)
} yield {
  val num1 = LongContext(i1, i2).log
  val num2 = log(i2, i1)
  (i1, i2, num1, num2)
}

val result = list1.filter(s => !(s._3 == s._4.toInt))

result.to(List).map(s => println(s"log(${s._1}, ${s._2}), number left in spire: ${s._4}, number right: ${s._3}"))

that report

log(3, 243), number left in spire: 4.999999999999999, number right: 5
log(10, 1000), number left in spire: 2.9999999999999996, number right: 3

Java's log has the same problem. I don't know if it's expected behavior.
And I use

println(log(BigDecimal(243, MathContext.DECIMAL32), 3))
println(log(BigDecimal(1000, MathContext.DECIMAL32), 10))

the application can not stop.

Possible dupe of #840. They're both non-terminating performance issues around log of BigDecimal.

Agree. Tracked in #840.

@armanbilge Reopen for new idea.
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.