←back to thread

631 points wojtczyk | 10 comments | | HN request time: 0.064s | source | bottom
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 #
1. 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 #
2. secondcoming ◴[] No.41413220[source]
Are there languages where this isn’t the case?
replies(3): >>41413237 #>>41413617 #>>41414823 #
3. cjbillington ◴[] No.41413237[source]
Python for one, and given that, I'd assume most languages.
4. smabie ◴[] No.41413617[source]
almost all of them?
5. grumpyprole ◴[] No.41414823[source]
Standard mathematical notation?
replies(1): >>41416317 #
6. cubefox ◴[] No.41416317{3}[source]
It's arguably ambiguous.
replies(1): >>41423369 #
7. seanhunter ◴[] No.41418382[source]
In the UK at least if you don’t put the parentheses you will get flagged for a ‘bidmas error’ because indexes (exponentiation) have higher precedence than multiplication

Source: have made this mistake. Have been flagged.

replies(1): >>41424042 #
8. FabHK ◴[] No.41423369{4}[source]
Shouldn’t

    -3²
be the same as

    0-3²

?
replies(1): >>41423488 #
9. cubefox ◴[] No.41423488{5}[source]
Not necessarily. E.g. 2*-3 is not the same as 2*0-3.
10. fennecbutt ◴[] No.41424042[source]
I learnt this in NZ as bedmas.

Brackets, exponents, division, multiplication, addition, subtraction.

Bedmas, Christmas in bed!