Money type

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


On Thursday, 2 January 2020 at 19:25:08 UTC, Ola Fosheim Grøstad 
wrote:
> (123*3+50)/100... Or 3.14% of 123 as (123*314+5000)/1000.

So easy to make mistakes with fixed point, should've been 
/10000...

> Except division is slow so you would instead convert the 
> divisor into a reciprocal and do a multiply instead (modern 
> compilers do this for you if the divisor is known at compile 
> time).

Well, in this specific case you could probably do

      result=percentage*value

like this:

      result  = (value*percentage_reciprocal+5)/10;

but that is just one simple formula... it gets tricky real fast 
when trying to do something more advanced in fixed point 
efficiently and correctly.



More information about the Digitalmars-d mailing list