Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Mondo Math Libs

By Eric Olson
2003-08-22


Binary vs. decimal

This leads us to the next issue: binary vs. decimal representation of floating point numbers. The most commonly used floating point formats, whether IEEE-754 conforming or otherwise, are based on binary representations, just as integers are. Unfortunately, this leads to the endless discussion in newsgroups about unexpected results from some floating point operations. The explanation is simple -- there are many fractional values that can be represented precisely in decimal notation yet have no exact form in binary. This most often surfaces after repeated calculations with one or more such numbers, leading the result to drift from the expected answer. Or it surfaces when you print a result at high precision, and instead of getting a nice round number like 74.00000, you get 73.99998. If you think this behavior is inconvenient in your astronomy or ray tracing program, imagine the headache it causes scientists or financial institutions, where even a small mistake can be catastrophic in more ways than one.

The most obvious solution to this problem is simply to represent floating point values in decimal form, as in the BCD (binary-coded decimal)/"packed decimal" numbers used in mainframes for decades, and countless other custom formats cooked up by inventive programmers. This will solve the representation problem, but usually at a cost in runtime performance, because the math operations are all carried out entirely in software (as in the MPM libraries discussed below), instead of in hardware in the form of a floating point unit (FPU).

By contrast, multiple-precision integers are almost universally represented as binary numbers, because there's no benefit to going decimal, as there is with floating point values. Keeping the components in binary actually improves performance in most cases, because the most common approach is to represent each multiple-precision integer as an array of smaller integers, each of which is a format natively supported by the hardware.



Tutorial Pages:
» A look at some of the math libraries for Linux
» Ground rules
» Binary vs. decimal
» Some contenders
» Java's BigInteger and BigDecimal
» Hints and tips
» Resources


First published by

Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation

Ask A Question
characters left.