Money type

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Thu Jan 2 17:57:19 UTC 2020


On Thursday, 2 January 2020 at 17:18:04 UTC, Russel Winder wrote:
> On Thu, 2020-01-02 at 14:34 +0000, Ola Fosheim Grøstad via 
> Digitalmars- d wrote:
>> 
> […]
>> If you understand the floating point hardware specification 
>> then there are no issues with floating point.
>> 
>
> I am not an expert in this, but I have seen lots of rants and 
> arguments on this over the years.
>
> As I understand it the core problem is needing base 10 numbers 
> not base 2 ones – converting between them causes problems – and 
> using hardware floating point does not have enough accuracy for 
> compliance with requirements of FCA for financial calculations.

Oh, I am not arguing that banking institutions should use base 2 
floating point. 64 bit floating point base 2 can only represent 
90 trillion cents with exact precision, so if they do 
calculations involving base 10 constants that could easily lead 
to problems... (And jurisdictions have various rules for how to 
deal with the sub-cent remainder I believe).

But you have the same issues with bit-limited base-2 fixed point, 
and it is easy to make mistakes with fixed bit fix point in 
non-trivial formulas!

So, if correctness is important then you could use decimal (the 
exact Pythonic numeric type), large base-10 floats (rare in 
hardware), or big-int rationals (in this case base2 is ok, you 
take care of the remainder at the end)...

Power9 from IBM apparently has 128 bits floating point, and IIRC 
some IBM machines have base 10 floating point hardware, but that 
is rather esoteric...

In the more mundane world of every-day-computing you have to be 
able to represent currency in web clients and in 
TypeScript/JavaScript 64bit IEEE754 is the only reasonable 
alternative.

So if you want the same representation on the server and the 
client then it can be useful to use 64bit floating point. 
Although, it is sometimes useful to store them ints as Steve 
suggested.





More information about the Digitalmars-d mailing list