Hacker News new | ask | show | jobs
by purplesyringa 16 hours ago
That's interesting! On my PC it reproduces under Lua 5.5 (`log_a x > log_b x`), but not under LuaJIT (`log_a x = log_b x`). I took a look at LuaJIT's implementation (https://github.com/LuaJIT/LuaJIT/blob/faaf663340347a78b22ed9...) and noticed that it always uses the `ln x / ln a` formula -- or, rather, `log_2 x / log_2 a`, which is just as correct I guess. Have you perhaps misinterpreted the results? (I do think it's valuable to add that this doesn't apply to LuaJIT though.)
1 comments

Hm, perhaps, but I am confused now. In the article, you're comparing log_a x < log_b x, but now you're comparing log_a x > log_b x in your comment. To make it clear, I am running this code: https://bpa.st/ZCKA

Which prints, for LuaJIT: true true false

And for Lua 5.5: true false false

`log_a x < log_b x` is the mathematically correct result. Due to inherent rounding from floats being a finite representation of real numbers, `log_a x <= log_b x` would be expected from any correct implementation using floats. So either `true true false` or `true false true` would be reasonable. (I made a mistake in the previous comment, LuaJIT returns `<` for me, just like in your comment, not `=`.)

The topic of the post is that in PHP and Lua (without LuaJIT), sometimes this inequality doesn't hold, and instead we get `log_a x > log_b x`, which is very incorrect and cannot be explained away by rounding.

Does that make more sense?

Ahem. Clearly my math knowledge failing me here. Apologies, and thanks for clearing it up :)