Weird value return from scala.math.log?

3,238 views
Skip to first unread message

Steven Luu

unread,
Mar 19, 2012, 7:05:23 PM3/19/12
to scala-l...@googlegroups.com
I'm trying to take the logarithm (base 2) for some numbers but scala is returning weird values. Has anyone encountered this?

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.
scala> scala.math.log(2)
res0: Double = 0.6931471805599453
scala> scala.math.log(4)
res1: Double = 1.3862943611198906
scala> scala.math.log(8)
res2: Double = 2.0794415416798357
scala> scala.math.log(16)
res3: Double = 2.772588722239781
scala> scala.math.log(32)
res4: Double = 3.4657359027997265
scala> scala.math.log(64)
res5: Double = 4.1588830833596715
scala> scala.math.log(128)
res6: Double = 4.852030263919617

Vlad Patryshev

unread,
Mar 19, 2012, 7:12:45 PM3/19/12
to scala-l...@googlegroups.com
As you can see from results, the function log(x) returns ln(x).
 
Thanks,
-Vlad

Paul Phillips

unread,
Mar 19, 2012, 7:13:40 PM3/19/12
to scala-l...@googlegroups.com
On Mon, Mar 19, 2012 at 4:05 PM, Steven Luu <slu...@gmail.com> wrote:
> I'm trying to take the logarithm (base 2) for some numbers but scala is
> returning weird values. Has anyone encountered this?

Let's see if I can triangulate on those...

scala> scala.math.log(2.718281828459045d)
res0: Double = 1.0

Stan Campbell

unread,
Mar 19, 2012, 7:15:01 PM3/19/12
to scala-l...@googlegroups.com
scala> scala.math.log(128)
res0: Double = 4.852030263919617

scala> val x = res0
x: Double = 4.852030263919617

scala> val y = """4.8520302639196171659206248502072""".toDouble
y: Double = 4.852030263919617

scala> x == y
res1: Boolean = true

I think it's formatting in the REPL?

On Mon, Mar 19, 2012 at 4:05 PM, Steven Luu <slu...@gmail.com> wrote:

David Hall

unread,
Mar 19, 2012, 7:53:33 PM3/19/12
to scala-l...@googlegroups.com
FWIW, using standard properties of log, you can define log2 in terms
of the natural logarithm:

scala> def log2(x: Double) = scala.math.log(x)/scala.math.log(2)
log2: (x: Double)Double

scala> log2(4)
res0: Double = 2.0

scala> log2(32)
res1: Double = 5.0

scala> log2(128)
res2: Double = 7.0

scala>

On Mon, Mar 19, 2012 at 4:05 PM, Steven Luu <slu...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages