4 comments

  • tomhow 8 hours ago
    What Every Computer Scientist Should Know About Floating-Point Arithmetic (1991) - https://news.ycombinator.com/item?id=23665529 - June 2020 (85 comments)

    What Every Computer Scientist Should Know About Floating-Point Arithmetic - https://news.ycombinator.com/item?id=3808168 - April 2012 (3 comments)

    What Every Computer Scientist Should Know About Floating-Point Arithmetic - https://news.ycombinator.com/item?id=1982332 - Dec 2010 (14 comments)

    What Every Computer Scientist Should Know About Floating-Point Arithmetic - https://news.ycombinator.com/item?id=1746797 - Oct 2010 (2 comments)

    Weekend project: What Every Programmer Should Know About FP Arithmetic - https://news.ycombinator.com/item?id=1257610 - April 2010 (9 comments)

    What every computer scientist should know about floating-point arithmetic - https://news.ycombinator.com/item?id=687604 - July 2009 (2 comments)

  • lifthrasiir 8 hours ago
    (1991). This article is also available in HTML: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.h...
  • emil-lp 3 hours ago

        > 0.1 + 0.1 + 0.1 == 0.3
        
        False
    
    I always tell my students that if they (might) have a float, and are using the `==` operator, they're doing something wrong.
    • zokier 1 hour ago
      That has more to do with decimal <-> binary conversion than arithmetic/comparison. Using hex literals makes it clearer

           0x1.999999999999ap-4 ("0.1")
          +0x1.999999999999ap-4 ("0.1")
          ---------------------
          =0x3.3333333333334p-4 ("0.2")
          +0x1.999999999999ap-4 ("0.1")
          ---------------------
          =0x4.cccccccccccf0p-4 ("0.30000000000000004")
          !=0x4.cccccccccccccp-4 ("0.3")
      • jacquesm 1 hour ago
        Absolutely nobody will think this is 'clearer', this is a leaky abstraction and personally I think that the OP is right and == in combination with floating point constants should be limited to '0' and that's it.
    • fransje26 1 hour ago
      I would argue that

          double m_D{}; [...]
      
          if (m_D == 0) somethingNeedsInstantiation();
      
      can avoid having to carry around, set and check some extra m_HasValueBeenSet booleans.

      Of course, it might not be something you want to overload beginner programmers with.

    • magicalhippo 2 hours ago
      I also like how a / b can result in infinity even if both a and b are strictly non-zero[1]. So be careful rewriting floating-point expressions.

      [1]: https://www.cs.uaf.edu/2011/fall/cs301/lecture/11_09_weird_f... (division result matrix)

      • StilesCrisis 15 minutes ago
        Anything that overflows the max float turns into infinity. You can multiply very large numbers, or divide large numbers into small ones.
    • jmalicki 2 hours ago
      .125 + .375 == .5

      You should be using == for floats when they're actually equal. 0.1 just isn't an actual number.

      • Sharlin 1 hour ago
        > 0.1 just isn't an actual number.

        A finitist computer scientists only accepts those numbers as real that can be expressed exactly in finite base-two floating point?

  • atoav 5 hours ago
    One thing that really did it for me was programming something where you would normally use floats (audio/DSP) on a platform where floats were abysmally slow. This forced me to explore Fixed-Point options which in turn forced me to explore what the differences to floats are.
    • jacquesm 1 hour ago
      Fixed point gave rise to the old programmers meme 'if you need floating point you don't understand your problem'. It's of course partially in jest but there is a grain of truth in it as well.
    • KeplerBoy 4 hours ago
      Also heavily used in FPGA based DSP.