Most active commenters

    ←back to thread

    631 points wojtczyk | 24 comments | | HN request time: 1.072s | source | bottom
    1. 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 #
    2. 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 #
    3. 0x0 ◴[] No.41411004[source]
    Interesting, I started playing with spotlight and typing in (-20)^21 returns " = 0", which is obviously not correct.

    And typing in "(-22)^21" gives "-71100888972574851072", but wolfram alpha insists it should be "-15519448971100888972574851072".

    Looks like there are still bugs here.

    replies(4): >>41411908 #>>41412231 #>>41413462 #>>41414489 #
    4. LeoPanthera ◴[] No.41411294[source]
    The Windows 3.1 calculator has a fun bug where 2.01 - 2.00 returns the result of 0.00, not 0.01.

    That always amused me.

    replies(1): >>41411669 #
    5. zelphirkalt ◴[] No.41411316[source]
    Maybe they never had proper tests. Something as simple as exponentiating,which is a basic functionality of any modern calculator, not working for 50% of all integers? That looks to me like no one ever really tested that stuff.
    6. canucker2016 ◴[] No.41411669[source]
    Acording to https://en.wikipedia.org/wiki/Windows_Calculator#Windows_9x_..., the Win98 version of calc.exe got an arbitrary-precision math library for basic math functionality to fix this sort of problem.

    also mentioned by Raymond Chen, https://devblogs.microsoft.com/oldnewthing/20040525-00/?p=39... :

    "Today, Calc’s internal computations are done with infinite precision for basic operations (addition, subtraction, multiplication, division) and 32 digits of precision for advanced operations (square root, transcendental operators)."

    And they fixed the square-root-of-a-perfect-square bug a few years ago, https://www.reddit.com/r/Windows10/comments/89s53g/microsoft...

    replies(1): >>41415713 #
    7. Aloisius ◴[] No.41411908[source]
    Spotlight on Sequoia looks correct, though it limits precision more than wolfram alpha.

    (-20)^21 = -2.097152x10^27 and (-22)^21 = -1.5519448971*10^28

    8. crazygringo ◴[] No.41412231[source]
    Wow that's bizarre.

    At first I thought it was just an overflow error but no it's nothing like that. The math is indeed very clearly broken, as I play around with it on Sonoma on my M1.

    I'm genuinely shocked. I though this kind of floating-point math was rock-solid, tested thoroughly over the decades.

    9. supportengineer ◴[] No.41412437[source]
    There must be a way to do a mash-up between a spreadsheet and other languages like FORTRAN. Perhaps the language could be pluggable. You could mix and match and the spreadsheet formulas are the glue. Cell values abstract away the details of how that value got there. But, it accepts a refresh request.
    10. secondcoming ◴[] No.41413220[source]
    Are there languages where this isn’t the case?
    replies(3): >>41413237 #>>41413617 #>>41414823 #
    11. cjbillington ◴[] No.41413237{3}[source]
    Python for one, and given that, I'd assume most languages.
    12. rubatuga ◴[] No.41413462[source]
    No bug in Big Sur spotlight:

    "(-20)^21" = -2.097152e27 "(-22)^21" = -1.551944897e28

    13. smabie ◴[] No.41413617{3}[source]
    almost all of them?
    14. dchest ◴[] No.41414489[source]
    Hah,

    macOS Sonoma 14.6.1 on M1 = 0

    iOS 17.6.1 = -0

    WTF.

    replies(1): >>41419626 #
    15. grumpyprole ◴[] No.41414823{3}[source]
    Standard mathematical notation?
    replies(1): >>41416317 #
    16. zelphirkalt ◴[] No.41415713{3}[source]
    Can this be true? Does the calculator have any irrational number? What if I enter PI * 2? Infinite precision, wouldn't that mean running until your RAM is full? Or would it simply work, because no irrational number is calculated to infinite (as many as the device could) digits and is therefore finite and can easily be multiplied?

    Or another even simpler case: If division is infinite precision, and I enter (2/3) * X, does the calculator internally work with fractions? Otherwise it would have infinite digits to compute.

    replies(2): >>41423206 #>>41437507 #
    17. cubefox ◴[] No.41416317{4}[source]
    It's arguably ambiguous.
    replies(1): >>41423369 #
    18. 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 #
    19. wojtczyk ◴[] No.41419626{3}[source]
    Yes, it looks like spotlight math is broken on both.
    20. Khoth ◴[] No.41423206{4}[source]
    For 2/3, it does indeed work with fractions internally. For pi, it uses some floating point representation I think, you get about 50 digits.
    21. FabHK ◴[] No.41423369{5}[source]
    Shouldn’t

        -3²
    
    be the same as

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

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

    Bedmas, Christmas in bed!

    24. canucker2016 ◴[] No.41437507{4}[source]
    For PI, calc.exe, in scientific mode, returns 3.1415926535897932384626433832795 which is 32 digits of precision (33 chars if you include the decimal pt).