Live data from Hacker News

Log is non-monotonic in PHP and Lua

purplesyringa.moe

11–20 of 58 posts

Re: Log is non-monotonic in PHP and Lua

#11
post #2

> you can compute any logarithm from two base-e ones. They need not be base e. Any base will do as long as it is the same for both the numerator and the denominator

Yeah, I simplified it a little. Though I must say I'm surprised pretty much every library I looked at uses the natural logarithm specifically, and not log2, which would seemingly be easier to compute with floats. Does anyone here know why, by any chance?

You can change base with a single multiplication by special constants if you accept 1ULP error, so working in one or the other doesn't really gain you much. But exp/logs are often implemented as a combination of a table lookup and polys, so you can just bake it into the constants even more easily.

Re: Log is non-monotonic in PHP and Lua

#13

Wouldn't a more applicable title be that the log base is non-monotonic?

The log base is a parameter, not a function, so that doesn't typecheck. Multivariate monotonicity isn't really a popular term, as far as I'm aware, so I'm not aware of good terminology for this. Maybe "log is non-monotonic with respect to the base"?

Re: Log is non-monotonic in PHP and Lua

#14
post #9

Earlier quoted context omitted.

Nice, thanks for tracking down those links, fascinating. Apparently being off by 1 ULP is within IEEE 754 floating-point spec, and "effectively negligible".

Correctly rounding transcendental functions is very difficult because of something called the table maker's dilemma, so the standard didn't want to impose a potentially extreme performance cost if they didn't have to. Correctly rounded single precision functions at a reasonable cost are much easier these days. LLVM 18+ implements them, but the gnu libraries have been slower to improve.

"The Table-Maker's Dilemma"

https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT

This is the best source I could find, funny enough the author doesn't explain the name "table maker".

I suppose it's from the parable of a table maker who finds that one leg is too long, so they sand down that leg, only to find that another leg is too long, so they sand that down... slowly sanding away all the legs.

As a simple programmer I don't understand the dilemma. I do understand that sin, tan, sqrt, etc., take one argument, so you can guarantee a certain precision for them, at least in 32-bit floats, whereas a log in arbitrary base has two inputs, so its input space is huge and it's hard to guarantee anything, especially for 64-bit doubles.

If someone could write up a good explanation that could be its own HN post

Re: Log is non-monotonic in PHP and Lua

#16
> Everyone already knows floating-point operations are imprecise and it wouldn’t be fun to blog about.

Wellll... Yes and no. And I want to nitpick "FP ops are imprecise" because it's important sometimes.

It does come up in Lua - Lua uses 64-bit double floats for everything, _even array indexing_, because they have 53 bits of mantissa and they're guaranteed to represent all 32-bit integers with 100% precision.

I just opened Lua and got `2 ^ 32 == 4294967296.0` and `2 ^ 32 + 1 == 4294967297.0`. You can store 4 billion things in a Lua table and access them with double float indexes.

The usual "0.1 + 0.2 != 0.3" is _not_ imprecision. You could dedicate 1,000 bits to a float and still find some example of a number that's trivial to represent in decimal but repeats forever in binary.

While I'm on it, fixed-point and floating-point aren't magically different. Floats work better on very big values. Fixed-points are more predictable but run out of range easily when squaring numbers. This comes in 3D math when finding the length of vectors or normalizing vectors.

The PlayStation 1 didn't have jiggly vertices and warpy textures because of fixed-point. It had jiggly vertices because its GPU didn't have subpixel-precise rendering, and it only had 16 bits of precision. It had warpy textures because it had affine texture mapping. The N64's GPU had more bits of precision and it had perspective-correct texture mapping. There's no 16-bit floating-point format that would have saved the PS1 from wobbling.

Also floats aren't the cause of T-junctions or "sparklies" that are common in 3D game levels. That will happen in any system with finite precision, and if fixed-point would fix it, we'd use fixed-point.

Re: Log is non-monotonic in PHP and Lua

#17

Earlier quoted context omitted.

Correctly rounding transcendental functions is very difficult because of something called the table maker's dilemma, so the standard didn't want to impose a potentially extreme performance cost if they didn't have to. Correctly rounded single precision functions at a reasonable cost are much easier these days. LLVM 18+ implements them, but the gnu libraries have been slower to improve.

"The Table-Maker's Dilemma" https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT This is the best source I could find, funny enough the author doesn't explain the name "table maker". I suppose it's from the parable of a table maker who finds that one leg is too long, so they sand down that leg, only to find that another leg is too long, so they sand that down... slowly sanding away all the legs. As a simple programm…

My assumption is it's referring to mathematical tables and the historical makers of them. https://en.wikipedia.org/wiki/Mathematical_table

Re: Log is non-monotonic in PHP and Lua

#18

Earlier quoted context omitted.

Correctly rounding transcendental functions is very difficult because of something called the table maker's dilemma, so the standard didn't want to impose a potentially extreme performance cost if they didn't have to. Correctly rounded single precision functions at a reasonable cost are much easier these days. LLVM 18+ implements them, but the gnu libraries have been slower to improve.

"The Table-Maker's Dilemma" https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT This is the best source I could find, funny enough the author doesn't explain the name "table maker". I suppose it's from the parable of a table maker who finds that one leg is too long, so they sand down that leg, only to find that another leg is too long, so they sand that down... slowly sanding away all the legs. As a simple programm…

> the author doesn't explain the name "table maker".

I suppose that parable fits, but also there is the dilema of someone making a table of (results) of transcendental functions. In the before times, most people got their sines and cosines and logs and what not by looking them up in tables. And someone has to make those tables. The example given where rounding to some specific number of digits depends on how many digits you calculate to is deeply unsatisfying, especially if you wanted to make a table for other people to use to lookup the results of functions.

Re: Log is non-monotonic in PHP and Lua

#20

Earlier quoted context omitted.

Correctly rounding transcendental functions is very difficult because of something called the table maker's dilemma, so the standard didn't want to impose a potentially extreme performance cost if they didn't have to. Correctly rounded single precision functions at a reasonable cost are much easier these days. LLVM 18+ implements them, but the gnu libraries have been slower to improve.

"The Table-Maker's Dilemma" https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT This is the best source I could find, funny enough the author doesn't explain the name "table maker". I suppose it's from the parable of a table maker who finds that one leg is too long, so they sand down that leg, only to find that another leg is too long, so they sand that down... slowly sanding away all the legs. As a simple programm…

The dilemma is hypothetical (because we now know the exact precision requirement for those functions over all 64-bit doubles) but let's assume that we don't know that and also we happen to use decimals instead. What if, some f(x) is known to be, say, 123.4999...9997 with 1,000 fractional digits? You have to calculate at least 1,000 decimal digits of f(x) in order to correctly round, before that we only know it's between 123.4999...999 and 123.5000...001. There seems no mathematical theory that specially prevents this annoying possibility, hence the dilemma.
Post reply on HN