←back to thread

631 points wojtczyk | 4 comments | | HN request time: 0s | source
Show context
rgovostes ◴[] No.41410786[source]
A friend doing homework for a university assignment, circa Leopard or Snow Leopard, noticed that Calculator produced negative values when raising a negative number to an even power.

The bug turned out to be in CFNumber, in Core Foundation. CFNumber does a lot of fiddly stuff at the bit level for performance, and one of their optimizations for exponentiation was incorrect. Somehow it was never found by tests or due to buggy behaviors it created in other apps, but by someone clicking buttons and thinking critically about the output.

replies(5): >>41410865 #>>41411004 #>>41411294 #>>41411316 #>>41412437 #
FabHK ◴[] No.41410865[source]
In Excel (and some other languages), unary minus has higher precedence than exponentiation, so that -x^2 = (-x)^2 = x^2. That can bite you (for example when calculating the normal probability density function “manually”).
replies(2): >>41413220 #>>41418382 #
secondcoming ◴[] No.41413220[source]
Are there languages where this isn’t the case?
replies(3): >>41413237 #>>41413617 #>>41414823 #
1. grumpyprole ◴[] No.41414823{3}[source]
Standard mathematical notation?
replies(1): >>41416317 #
2. cubefox ◴[] No.41416317[source]
It's arguably ambiguous.
replies(1): >>41423369 #
3. FabHK ◴[] No.41423369[source]
Shouldn’t

    -3²
be the same as

    0-3²

?
replies(1): >>41423488 #
4. cubefox ◴[] No.41423488{3}[source]
Not necessarily. E.g. 2*-3 is not the same as 2*0-3.