6.7.1. Numeric Derived Types - JulTob/Ada GitHub Wiki
You can define your own fixed point and floating point types and — with the aid of generic packages call all the mathematical functions you need.
In that respect Ada is on par with Fortran.
If we write
type Number is new Float;
Then Number has all the operations (+,*, etc) of Float. Generally speaking, it can be considered as equivalent.
Now, suppose we transfer the program to a different context where the predefined Float is not so accurate, so we want to use Long_Float. Assuming the program has been written using Number, rather than Float, then replacing the declaration to
type Number is new Long_Float;
Is the only change necessary.
We can also improve by stating the precision and range, or minimum accuracy.
type Number is digits 7;
-- Will make a type with at least 7 decimal digits of accuracy.
type Number is digits 10 range -1.0 .. 1.0;
-- Will make a Floating-point type with AT LEAST 10 digits of accuracy.
type Number is range -10.0 .. 10.0;
Pi : constant := 3.14159_26535;
-- πτΔδ
type Angle is delta Pi/(2.0**31) range -Pi .. Pi;
-- will generate fixed-point type of 32 bits for the values of a circle
type My_Float is digits 7 range -2.0 ** 32 .. 2.0 ** 32;
type Voltage is delta 0.125 range 0.0 .. 255.0;
type Money is delta 0.01 digits 15;
type <type-name> is delta <delta-value> digits <digits-value> range <range def>;
subtype temperature is float range 0.0 .. 100.0;
subtype result_range is float range 0.0 .. 1.0;
type Length is digits 5 range 0.0 .. 4.0E6;
type Velocity is new Float;
type Height is new Float;
-- can't accidentally mix velocities and heights without an
-- explicit type conversion.
type Euros is digits 2 range 0.00 .. 1_000_000.00;
type Dollars is delta 0.01 digits 9;
subtype Salary is Dollars digits 6
type Number is delta System.Fine_Delta range -1.0 .. +1.0;
-- Fraction'last = 1.0 - System.Fine_Delta
The Integer numbers also can be invoked.
type Number is new Integer;
type Number is new Natural;
-- Unsigned Integers
type Number is new Positive;
-- Non zero Naturals.
type Number is Integer range -1_000 .. +1_000;
-- The _ is ignored. Visual aid
type Number is range -1_000_000 .. +1_000_000;
-- The type is interpreted in context
Type forRange is range 1 .. 10;
Type image_X is range 0 .. (1024 - 1);
Month : number := 08 --Ada allows for leading zeros
subtype result_range is range 1..10;
They come with certain methods associated.
Positive’First; -- 1
Natural’First; -- 0
Integer’First; -- (-2^15) +1
Integer’Last; -- 2^15 -1
Number’Range;
Ada also allows modular types, as in cyclical topologies.
type Angle is mod 360;
-- 360 is 0 again
----------------
Type Byte is mod 16#10000#; --Base 16
...
Mask := 16#FF00#;
Byte := Byte and Mask; --Bitwise AND.
---------------
Type word_byte is mod 16#100#;
It provides a clear syntax for numeric bases between 2 and 16 inclusive (10 is of course the default)
You can also define types by certain conditional attributes.
for Number’Size use 32; --Memory space: 32 Bits
You can also define types by certain conditional attributes.
X := 16#CAFE#; -- Hexadecimal
X := 8#127#; -- Octal
X := 2#1101_1101_0001_1001#; -- Binary
X := 5#3420# -- Any
The compiler is responsible for choosing an appropriately sized integer type. On a PC, it would be a 32 bit size, equivalent to long_integer. On a Unix workstation it would still be a 32 bit integer, but this would be equivalent to an integer. Letting the compiler choose frees the programmer from having to choose. Compiling it on a new host does not require changing the source code.
Un rango es un subconjunto de valores de un tipo escalar especificado por un límite inferior y un límite superior. Si el límite inferior es L y el límite superior es R, el rango "L .. R" comprende todos los valores del tipo en cuestión que sean mayores o iguales que L y menores o iguales que R. Si L > R el rango es nulo.
-127 .. 128
A .. A + 10
0.0 .. 2.0
1 .. 0 -- rango nulo
-- Ejemplos de rango como range_attribute_reference
Integer'Range -- rango Integer'First .. Integer'Last
Mat'Range(1) -- rango 1 .. 5, supuesto que:
-- Mat : array (1 .. 5, 1 .. 10) of Integer;
--Ejemplos de restricción de rango
range -128.0 .. +128.0
range Integer'First .. Integer'Last - 1