Fixed point arithmetic is a numeric type for representing real numbers that has a fixed number of digits before and after the radix point (i.e. the decimal). It is typically implemented using integer math operations. It has a few advantages over using floating point:
- Hardware Support
There are still chips out there without a hardware FPU. However, I am pretty sure every practical microprocessors has hardware support for integer math.
- Potential Cross-Platform Consistency
Read this for advice and opinions on making floating point behave consistently across platforms. Rust even has an RFC on the subject. The gist is: here be VERY large and VERY hungry dragons who will devour you VERY quickly.
My gut tells me that integer arithmetic operations are much more consistent across platforms (if this is not the case, please tell me). If, on some architecture, 0b010 + 0b010 = 0b101, everyone would say, “AHHH!! We’re living in an Orwellian nightmare!”
- The Far Lands
Because of the way floating point handles precision, calculations involving numbers further than the origin may not have the precision of numbers closer to the origin. Game engines tend to solve this problem by moving the origin around, but that is an ugly hack. If a result is within the supported range of a fixed point number, it should be just as precise throughout that range.
So, why should this be in the language? Why can’t it be in a crate? AFAIK a crate cannot do this:
let pi = 3.1415_fx32;
let e: fx32 = 2.71828;