# Division from derived integer type to a derived floating point type

**URL:** https://forum.ada-lang.io/t/division-from-derived-integer-type-to-a-derived-floating-point-type/1079
**Category:** General
**Created:** [August 12, 2024, 11:53pm UTC](https://forum.ada-lang.io/t/division-from-derived-integer-type-to-a-derived-floating-point-type/1079 "2024-08-12T23:53:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)
#### Post date: [August 12, 2024, 11:53pm UTC](https://forum.ada-lang.io/t/division-from-derived-integer-type-to-a-derived-floating-point-type/1079/1 "2024-08-12T23:53:41Z")

</div>

Hi, so I have these generic formal types:

```ada
generic
	type TInteger is range <> or use Integer;
	type TFloat is digits <> or use Float;
package Fractions_ADT is

```

with later the type private type “Fractions” defined. But I don’t know exactly how to transform a fraction to a TFloat. This

> function To\_Float (A: Fractions) return TFloat is (TFloat’(A.Numerator / A.Denominator));

doesn’t work. I don’t know if TFloat automatically gets a constructor from all integer-like types… or do I have to convert each operand to Float before ?

---

<div class="post-metadata">

### Author: ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)
#### Post date: [August 13, 2024, 12:40am UTC](https://forum.ada-lang.io/t/division-from-derived-integer-type-to-a-derived-floating-point-type/1079/2 "2024-08-13T00:40:07Z")

</div>

> [@evanescente-ondine](#):
>
> function To\_Float (A: Fractions) return TFloat is (TFloat’(A.Numerator / A.Denominator))

Try:

```ada
function To_Float (A: Fractions) return TFloat is 
   (TFloat(A.Numerator) / TFloat(A.Denominator));

```

---

<div class="post-metadata">

### Author: ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)
#### Post date: [August 13, 2024, 12:43am UTC](https://forum.ada-lang.io/t/division-from-derived-integer-type-to-a-derived-floating-point-type/1079/3 "2024-08-13T00:43:42Z")

</div>

No because A’s components both have a TInteger type, not TFloat.

---

<div class="post-metadata">

### Author: ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)
#### Post date: [August 13, 2024, 1:48am UTC](https://forum.ada-lang.io/t/division-from-derived-integer-type-to-a-derived-floating-point-type/1079/4 "2024-08-13T01:48:44Z")

</div>

Sorry, I didn’t catch that you originally used qualified expressions instead of type conversions so I didn’t have it correct before. I updated it. Using type conversions it works:

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

procedure jdoodle is
    type Fractions is record
        Numerator : Integer;
        Denominator : Integer;
    end record;
    
    type TFLoat is new Float;
    
    function To_Float (A: Fractions) return TFloat is 
        (TFloat(A.Numerator) / TFloat(A.Denominator));
        
    F : Fractions := (Numerator => 1, Denominator => 3);
    T : TFLoat := To_Float(F);
begin
    Put_Line("Result = " & T'Image);
end jdoodle;

```

Output:

```ada
Result = 3.33333E-01

gcc -c jdoodle.adb
gnatbind -x jdoodle.ali
gnatlink jdoodle.ali -o jdoodle

```
