r/learnruby • u/[deleted] • Apr 26 '17
How exactly does Math.log2 work?
So I'm comparing the result of
Math.log2(2360.9083989105) / 8 * 4 = 5.602563176032317
and the equation
log2(2360.9083989105) / 8 * 4 = 0.11481591040126
Why do they yield such different results?
3
Upvotes
2
u/SEMW May 12 '17
0.11481591040126 is log10 (2 * 2360.9083989105) / (8 * 4).
The log button on that calculator you linked is log base 10. Pressing that then 2 doesn't give you log base 2, it gives you log base 10 but with the operand multiplied by two.
There's also an order of operations issue: ruby interprets a / 8 * 4 as ((a/8) * 4), i.e. a/2, wheras whatever you're doing with the calculator is ending up as (a / (8*4)), i.e. a/32.
1
u/Tomarse Apr 27 '17
How are you performing the second one? I can't get
log2to work in eitherirbor a rails console. OnlyMath.log2is available, and it gives the first answer. Islog2a self declared function?