How to use different fix point types in mathematical operations in Ada?
For the program at the end I get the following error messages from gnat:
test2.adb:23:61: error: invalid operand types for operator "-"
test2.adb:23:61: error: left operand has type "Gain_Type" defined at line 11
test2.adb:23:61: error: right operand has type "Offset_Type" defined at line 12
Unfortunately I did not find a good example how to resolve this in a way resulting in speed optimized code for rather small embedded targets.
Always casting everything to the biggest type does not make that much sense I feel. What is the best way to do that/ isn't there a good reference existing how to efficiently use fixed point for a bit more complicated mathematical problems?
procedure Test2 is
Adc_Width : constant Positive := 10;
Adc_Delta : constant Float := 2.0**(-Adc_Width);
Adc_Mod : constant := 2**Adc_Width;
Error_Delta : constant := 2.0**(-1);
Gain_Min : constant Float := 1.0 - 2.0 * Adc_Delta;
Gain_Max : constant Float := 1.0 + 2.0 * Adc_Delta;
Offset_Min : constant Float := -0.5 * Adc_Delta;
Offset_Max : constant Float := 2.0 * Adc_Delta;
type Gain_Type is delta Adc_Delta * Error_Delta range Gain_Min .. Gain_Max;
type Offset_Type is
delta Adc_Delta * Error_Delta range Offset_Min .. Offset_Max;
type Adc_Encoded_Type is mod Adc_Mod with
Size => 16;
subtype Adc_Value_Type is natural range 0 .. Adc_Encoded_Type'Modulus - 1;
type Adc_Delta_Type is delta Adc_Delta range 0.0 .. 1.0 - Adc_Delta;
function Compensate
(Adc : in Adc_Encoded_Type; Gain : in Gain_Type; Offset : in Offset_Type)
return Adc_Delta_Type
is
begin
return Adc_Delta_Type (((Adc_Value_Type (Adc) * Gain) - Offset) / Adc_Mod);
end Compensate;
begin
end Test2;