Hacker News new | ask | show | jobs
by lacampbell 2524 days ago
(I could google this but I feel like a discussion would be more fun!)

What exactly is fixed point math? The way I deal with currency is to use decimal floating point, ie 'decimal' in C#. Is fixed point simply using integers with and making your base unit cents, not dollars?

2 comments

In fixed point math, the decimal point (the units digit) is "fixed" at a particular value instead of "floating". A fixed-point number is an integer that represents values as a multiple of 1/N, so fixed-point multiplication requires adding an extra divide-by-N and fixed-point division requires adding an extra multiply-by-N. A floating point number represents numbers as a pair (e, m) such that the result is m * b^e (where b is the base, see below).

Decimal versus binary is orthogonal. A decimal fixed-point number means that N will be a power of 10, and usually clips the range such that there are fixed number of decimal digits available (e.g., between -1,000,000 and +1,000,000). A binary fixed-point number means that N will be a power of 2, and clips its range to fix the number of binary digits (e.g., between -1,048,576 and +1,048,576). Binary floating point means that the base in arithmetic is 2, whereas decimal floating point means that base in the arithmetic is 10. C#'s `decimal` type is a non-standard decimal floating point, which looks to make the mantissa be a binary range instead of a decimal range (i.e., the maximum is not 10^n - 1 for some n).

Could be cents or even mills, but essentially yes.