Re: A harder one
Bill Earl
The answer I got was $1.20, $1.25, $1.50 & $3.16
The 'obvious' way to program this is with floating point numbers. The problem is that binary floating point numbers are only an approximation of real numbers. Better programmers are aware of the limited precision for very large or very small numbers, but what is commonly overlooked is the imprecision that exists in the floating point representation of a number like 0.1.
In fact, there is no exact representation for 0.1 in floating point binary. The mantissa ends up as a repeating binary fraction. These errors accumulate with each operation. So, seemingly simple floating point calculations on relatively ordinary looking numbers might produce a result like $7.109999999999999999999 when what you expected was $7.11.
The common solution to this problem is to use Binary Coded Decimal (BCD). Instead of the exponent-mantissa representation of floating-point, BCD uses an array of binary coded decimal digits. This allows any decimal number to be represented to an arbitrary level of precision.
BCD is commonly used in calculators, financial software packages and scientific applications where extreme precision is required.