length's type.

Gary Chike chikega at gmail.com
Fri Feb 9 21:46:27 UTC 2024


On Friday, 9 February 2024 at 16:49:37 UTC, Gary Chike wrote:

The underlying architecture of the language will often times 
dictate how certain constructs or design decisions are made. For 
example in Ada, every array has a `Length` attribute and it 
returns an `integer` type.

And since Ada is a very strongly typed language, there is no 
choice but to explicitly cast both the numerator and denominator. 
Ada will not allow you to cast only the numerator and allow the 
compiler to implicitly cast the denominator as is the case in 
most C-based languages. This will not compile:
`Avg := Float(Sum) / Len;`

```
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Main is
    type Int_Array is array (Positive range <>) of Integer;

    A   : Int_Array := (-5000, 0);
    Len : Integer := A'Length;
    Sum : Integer := A(1) + A(2);
    Avg : Float;
begin
    Avg := Float(Sum) / Float(Len);
    Put(Avg, 1, 2, 0);
    New_Line;
end Main;
```
Output: -2500.00






More information about the Digitalmars-d-learn mailing list