Money type

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Thu Jan 2 23:13:00 UTC 2020


On Thursday, 2 January 2020 at 18:26:25 UTC, Ola Fosheim Grøstad 
wrote:
> On Thursday, 2 January 2020 at 18:20:38 UTC, Ola Fosheim 
> Grøstad wrote:
>> E.g. to get 3% of 123 cents: (3*123.0)/100
>
> Whoops, too quick, that won't work.

For the curious, on some FPUs you can test if the computations 
was inexact using the FE_INEXACT flag. So, then you can do all 
the computations and ensure that there is no information loss up 
to the division. Then you do an inexact division with 
rounding-down mode:

// set rounding down mode here and track FE_INEXACT exception
x = 3.0*value+0.5

// ensure that  FE_INEXACT is false

// do inexact computation
result = floor(x/100.0)

I think that should work out ok for all input values, but I've 
never actually used FE_INEXACT in practice, so maybe I got 
something wrong...




More information about the Digitalmars-d mailing list