News for hackers. Stuff that matters.
Re:Beware of Fast-Math by MobiusHorizons on Saturday May 31, 2025 @11:07AM (#44144769)
This is what’s called a fixed point decimal type. If you need variable precision, then a decimal type might be a good idea, but fixed point removes a lot of potential foot guns if the constraints work for you.
Re:Beware of Fast-Math by osigurdson on Saturday May 31, 2025 @12:26PM (#44145323)
I meant fixed point decimal type (like C#) 128 bit. I don't understand why the parent commenter (top voted comment?) used unsigned integers to track individual cents. Why roll your own decimal type?

Using arbitrary precision doesn't make sense if the data needs to be stored in a database (for most situations at least). Regardless, infinite precision is magical thinking anyway: try adding Pi to your bank account without loss of precision.

Re:Beware of Fast-Math by MobiusHorizons on Saturday May 31, 2025 @02:04PM (#44145932)
the C# decimal type is not fixed point, its a floating point implementation, but just uses a base 10 exponent instead of a base 2 one like IEE754 floats.

Fixed point is a general technique that is commonly done with machine integers when the necessary precision is known at compile time. It is frequently used on embedded devices that don't have a floating point unit to avoid slow software based floating point implementations. Limiting the precision to $0.01 makes sense if you only do addition or subtraction. Precision of $0.001 (Tenths of a cent also called mils) may be necessary when calculating taxes or applying other percentages although this is typically called out in the relevant laws or regulations.

Re:Beware of Fast-Math by osigurdson on Saturday May 31, 2025 @02:14PM (#44145986)
Good to know. In a scientific domain so haven't used it previously.
Re:Beware of Fast-Math by MobiusHorizons on Saturday May 31, 2025 @06:29PM (#44147374)
Fun fact there is a decimal type on some hardware. I believe Power PC, and presumably mainframes. You can actually use it from C although it’s a software implementation on most hardware. IEEE754-2008 if you are curious.
Re:Beware of Fast-Math by jjmarr on Saturday May 31, 2025 @01:52PM (#44145864)
IEEE754 defines a floating point decimal type. What are your opinions on that?
Re:Beware of Fast-Math by MobiusHorizons on Saturday May 31, 2025 @06:32PM (#44147395)
It’s very cool, but not present on most hardware. Fixed point is a lot simpler though if you are dealing with something with inherent granularity like currency