New Numeric Types
Introduction: Kinds of Numeric Types
Kinds of New Numeric Types
- Possible kinds of numeric types:
- Integer
- Floating point
- Fixed point
- Modular
Integer and Floating Point Types
New Integer Types
Example: types ( not subtypes) for temperature and pressure
type Temperature is Range 32 .. 212;
type Pressure is Range 0 .. 500;
t: Temperature;
p: Pressure;
t := 100; -- valid
p := t; -- compile error
Reduce errors:
- Only valid values can be assigned to t and p
- Types cannot be mixed
Operations:
- Automatically get all operations on Integers
- Need to define I/O operations
Mixing Operations:
- Sometimes mixed type expressions are needed and conversions can be
used.
Result: Integer := Integer(t) * Integer(Pressure)
Both types share same underlying representation
- Conversion does not change value in any
- Conversion signals compiler to allow computation
Floating Point Types
- Like integer types, we can declare new floating point types
- Example:
type Volume is Digits 8 Range 10.0 .. 1_000.0; -- Cubic Centimeters
type Weight is Digits 8 Range 50.0 .. 500.0; -- Grams
h: Height;
w: Weight;
density: Float;
h := 100.0; -- valid
-- w := h; -- compile error
w := 50.0;
-- Sometimes need to convert types
density := float(h) / float(w);
The digits specifier tells how many digits of precision the number will have
- Type Float and Long_Long_Float have digits 6 and 18, respectively
Alternate Notation for Integer and Floating Point Types
- New number types can specify the underlying type:
type Temperature is new Integer Range 32 .. 212;
type Pressure is new Integer Range 0 .. 500;
type Volume is new Float Range 10.0 .. 1_000.0;
type Weight is new Float Range 50.0 .. 500.0;
Otherwise the compiler chooses the underlyin type
First notation is more portable: allow compiler to choose new type
Fixed Point Types
What Does Floating Point Mean?
- To learn Fixed Point Types (below) you must know floating point types
- Type Float: decimal point floats (ie it can move among the significant digits)
x: Float;
x := 12_345_678.9 -- 12_345_678.9
x := x / 10.0; -- 12_345_67.89
x := x / 100.0; -- 12_345.678_9
Floating point can give inexact results:
cent: Float := 0.01;
dollar: Float := 0.0;
for i in 1 .. 100 loop
dollar := dollar + cent;
end loop;
put(dollar); -- 0.9999993443
Decimal Fixed Point Types
- Decimal fixed point types have a constant, fixed number of decimal places
- Good for problems involving money
type Money is delta 0.01 digits 15;
cent: Money := 0.01;
dollar: Money := 0.0;
annualSal := 1234567.89;
put_line(annualSal'img); -- 1234567.89
annualSal := annualSal / 10; -- Division fixed by int is okay
put_line(annualSal'img); -- 123456.78 -- Truncates toward 0!
annualSal := annualSal / 10;
put_line(annualSal'img); -- 12345.67
for i in 1 .. 100 loop
dollar := dollar + cent;
end loop;
put(dollar); -- 1.00
Another example - calculate missing salary:
subtype Salary is Money digits 10;
annualSal: Salary := 100_000.00;
monthlySal: Salary := annualSal / 12; -- 8333.33
missingSal := annualSal - (12 * monthlySal); -- 0.04
- Shows number of cents that must be added to paycheck over the year
Fixed Point and Decimal Fixed Point Types
- Decimal Fixed point types are decimal based
- Ordinary Fixed point types are binary based
- Example:
subtype Volt is delta 0.125 range 0.0 .. 255.0;
- 0.125 = 2#0.001# = (0.5)**3
Modular Types
Modular Types
- Modular types:
- Values of modular types are never negative
- Modular types use modular arithmetic
- Example 1 - Hours: values range from 0 .. 11:
type Hour_t is mod 12;
Values always in 0 .. 11 (ie remainder mod 12)
All operations are mod 12
Example 1 - continued
type Hour_t is mod 12; -- values range from 0 to 11
startTime: Hour_t := 8;
breakTime: Hour_t := startTime + 4; -- 0
endTime: Hour_t := startTime + 6; -- 2
notifyTime: Hour_t := startTime - 11; -- 9
Example 2 - 32 bit unsigned value (no sign bit - useful for addresses):
type Address is mod 2**32;
Similar to C unsigned types and Java char type