Search code examples
decimalada

Ada: fixed decimal place


I am doing some numerical calculations dealing with iterations and I would like to know how to force Ada work only to a certain number of decimal places say 6. If I write

type Real is digits 6;

my understanding tells me that this forces the precision to be 6 Significant figures. But I am more interested in the number of decimal places after the decimal point as in 238.345891 and 0.297568 in which both numbers contain 6 decimal places.

The point of working with a fixed number of decimal places is that with numerical computations, I normally do the checking by hand and a calculator and I keep all calculations to a fixed number of decimal places, here 6. I would like to get the same results which I get by hand calculations and those from the Ada program.

I agree that working with significant figures makes more sense since a number such as 4 x 10^-7 will amount to just zeros if I work with 6 d.p. but I want to know if Ada offers the possibility to work with a fixed number of decimal places.


Solution

  • Something like type My_Real is delta 0.000001 digits 9;?

    The actual Digits value will depend on the range you need. Note that's a decimal type, not a binary type, which seems to be what you're asking for.

    Fixed point types don't behave as floating point ones so I would play a bit with the type to see if it does exactly what you want.