Type Float, Strong Typing, Coercion,
Type Float
Type Float
- Use type Float for floating point numbers
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
procedure whatever is
f: float;
begin
f := 1.234_567;
-- f := 1.; -- Error
-- f := .1; -- Error
Ada.Text_IO.put(f'img); -- 1.23457E+01
Ada.Float_Text_IO.put(f); -- 1.23457E+01
put(f, fore => 2, aft => 2, exp => 0); -- 12.35
fore - mininum width left of decimal point
aft - mininum width right of decimal point
exp = number of exponent digits. 0 gives no exponent
Strong Typing and Explicit Coercion
Ada is Strongly Typed
- Strong Typing: Assignment, procedure calls, expressions/operators
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure whatever is
i: Integer := 11;
f: float := 22.2;
begin
-- i := f; -- Error
-- f := i; -- Error
-- i := 1.1; -- Error
-- f := 2; -- Error
-- Ada.Float_Text_IO.put(i); -- Error
-- Ada.Integer_Text_IO.put(f); -- Error
-- f := f / 2; -- Error
No implicit type conversion!
- Compare Java: f := 4; f = f / 2;
- Note: 4.4 / 2; // No such machine operation exists!
Strong Typing for Expressions
- Must use correct types on expressions
procedure hello4 is
f: Float := 2.3;
g: Float := 4.5;
avg: Float;
begin
f := g + 2.0; -- Okay
f := g + 2; -- ERROR
avg := (f + g) / 2; -- ERROR
end hello4;
Contrast with Java:
double g = 4.5
double f = g + 2.0;
f = g + 2; // Assumes f = g + (double)2;
double avg = (f + g) / 2 // Assumes (f + g) / (double)2;
Stronger Typing than Java
int i = 2;
int j = 6.7; // Error
int j = 6.0; // Error
double g = 6; // OKAY
System.out.println(i); // Calls toString
Java ASSUMES that you meant 6.0. Did you?
Strong Typing Rule
- Names of types must match in these contexts:
- Assignment: types of LHS and RHS of assignment
- Parameters: types of formal and actual parameters
- Expressions: types of both sides of binary operators (usually)
- Functions: return type of function and type of the value being returned
- Exception to rule: subtypes
- Subtypes of a given type can be freely mixed
- Example: Naturals, Positives, and Intgers can be mixed freely
i: Integer; n: Natural; p: Positive;
...
i := n;
i := p;
n := i;
p := i;
n := p;
p := n;
Disambiguation
- Disambiguation by type of parameter
with Ada.Text_IO; use ada.text_io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure hello3 is
f: Float := 2.3;
i: Integer := 4;
n: Natural := i;
begin
Ada.Float_Text_IO.put(f);
Ada.Integer_Text_IO.put(i);
put(f); --
put(i);
put(n);
end hello3;
Explicit Conversion
- Use type name as a function to convert (ie cast or coerce) a type:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure whatever is
i: Integer := 11;
f: float := 22.2;
sum: Integer;
begin
i := Integer(f); -- Truncate
f := Float(i);
f := f / Float(2);
f := Float(Integer(f) / 2);
---
avg := Float(Sum / 2);
f := f / Float(i);
Type Conversion
- Convert types (ie cast) by using type name as function:
f, avg: Float;
i, sum, count: Integer;
...
avg := float(sum) / float(count);
avg := float(sum / count);
f := float(2).
f := Float(i);
i := Integer(f); -- Truncates
i := Integer(4.3); -- Truncates
Something to think about: What do these conversion actually do at
runtime?
- Some type conversions actually produce a new value
- Some type conversions do nothing to the value
- These conversions simply tell the compiler to
do the operation on the given data value
Summary of Strong Typing
- The following are strongly typed:
- Assignment
- Parameters
- Expressions
- Function return types
- Rule: Type names must match
- Ada makes almost no assumptions
- But subtypes of a given type can be freely mixed
- Stronger type checking than Java
- Question: Does conversion change value