This always caught me up until I was reading TAOCP one day and Knuth pointed out that every FP op was really "round(x OP y)"—that's when it really clicked for me.
round(round(x * y) / z) and round(x * round(y / z)) can be different. I've since found it much easier to reason about FP code if I can remember to think about it like that. On Fri, Aug 18, 2017 at 9:50 AM, Michael Jones <michael.jo...@gmail.com> wrote: > Here is a minor musing from something that came up yesterday. > > Sometimes we see a real number expression as simple as... > > x*y/z > > ...and knowing from basic algebra that... > > (x*y)/z == x*(y/z) > > ...we might not expect much difference between the two in our code. Alas, > computer floating point does not involve real numbers, rather it uses an > approximation of them. If you will copy https://play.golang.org/p/IlDxtvx7IY > to your computer, build and run it, (too much CPU for the playground) you'll > find that the order makes a difference. Or, you can just remember this > phrase, "multiply first, then divide." > > -- > Michael T. Jones > michael.jo...@gmail.com > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.