Know your floating-point

Many computer programmers do not get taught about floating-point properly in college and they assume that it behaves just like real numbers would as the first few programs they write make it seem so. Integer and FP are like horse and donkey. While FP offers a wide range and good precision, it does not offer exact precision similar to integers and mathematical real numbers as one might naively expect from it. Many times these errors add up and become troublesome, so it’s good to have an understanding of the subject. The FP standard which is popularly used in CPUs, virtual machines and languages standards is IEEE 754. Apart from representation, this standard also defines various other bits such as rounding modes and exception handling. Old-skool programmers will probably expect floating-point x/0 to cause a divide-by-zero trap as it does with integers, but this’s not so in IEEE 754 by default.. it lets the process run on and uses special floating point values called -inf and +inf (although languages like Delphi enable traps by default by not following the standard and then we have fun in the Windoze world where a Delphi compiled DLL used in some arcane place like a file selection dialog enables traps and unrelated apps start crashing in various places when they cause traps). There are things a good programmer should know about FP’s accuracy and how to write good floating-point code, and this aptly titled paper is great for it: What Every Computer Scientist Should Know About Floating Point Arithmetic. If you haven’t read this one yet, take some time and read it (all ye C, Java, etcetera etcetera programmers). C99 has a bunch of functions to get/set trap handling behaviour, etc. and glibc includes some more extra convenience functions.

0 Responses to “Know your floating-point”


  1. No Comments

Leave a Reply