Can an Ada range be declared an unlimited upper bound?

I would like to declare a range of speeds for the type of record in Ada. The following steps will not work, but is there a way to make it work?

--Speed in knots, range 0 to unlimited Speed : float Range 0.0 .. unlimited ; 

I just want to get a zero positive value for this number ...

+2
source share
3 answers

You cannot - but since Speed is of type Float , its value cannot exceed Float'Last .

 Speed : Float range 0.0 .. Float'Last; 

(You probably want to declare an explicit type or subtype.)

+4
source

Just for completeness, you can also define your own basic floating point types, rather than using one called Float , which may or may not have the required range.

For example, Float defined somewhere in the sources of the compiler or RTS (Runtime System), possibly as type Float is digits 7; next to type Long_Float is digits 15; that gives you 7 and 15 digits respectively.

You can also define your needs to meet the accuracy and range required by your application. The philosophy is to indicate what you need (in range and accuracy) and let the compiler satisfy it most efficiently. This is programming in a problem area, stating what you want, not in the solution domain, tying your program to something that supports a specific computer or compiler.

The compiler will use the next top-level plugin with the highest accuracy (usually with 32-bit or 64-bit IEEE floats) or complains that it cannot do this

(e.g. if you declare

type Extra_Long_Float is digits 33 range 0.0 .. Long_Float'Last * Long_Float'Last;
your compiler may complain if it does not support 128-bit floats.

+4
source

Unlimited impossible. This will require unlimited memory. I do not know any platform that has this. You can write a package that provides rational numbers larger than the available memory can handle (see PragmARC.Rational_Numbers in

+2
source

Source: https://habr.com/ru/post/1273602/


All Articles