Money type

Ola Fosheim Grøstad ola.fosheim.grostad at
Thu Jan 2 19:25:08 UTC 2020

On Thursday, 2 January 2020 at 19:12:11 UTC, bachmeier wrote:
> I've never had a reason to avoid integers for performance 
> reasons, but I suppose there are cases in which it's relevant...

Scripting languages try to keep things simple, so having only one 
numeric type is an advantage from an implementation point of 
view. So, not really for performance.

However, if you mean fixed point integers, then you need a rather 
big bit width to avoid overflow errors to do the same 
calculations as with floating point. And it is easy to make 
mistakes, but older DSPs didn't have floating point units so they 
used fixed point math (and also older computer games) and 
programmers had to be very clever to make it perform well with 
good accuracy. In some ways fixed point is more tiresome than 
writing in assembly if you try to do something nontrivial.

E.g. in fixed point you would have to do 3% of 123 as 
(123*3+50)/100... Or 3.14% of 123 as (123*314+5000)/1000. 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). So if the 
percentage isn't known at compile time you have much work to do 
as a programmer to get it right...

More information about the Digitalmars-d mailing list