Skip to content

Instantly share code, notes, and snippets.

@charlie5
Created January 6, 2023 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlie5/e25d80d1d9c341624ee499a4cf0cb5e1 to your computer and use it in GitHub Desktop.
Save charlie5/e25d80d1d9c341624ee499a4cf0cb5e1 to your computer and use it in GitHub Desktop.
with
ada.Text_IO,
ada.unchecked_Conversion;
-- procedure test
-- is
-- use ada.Text_IO;
-- type String_to_Float is (Behind, Close, Near, Far);
-- for String_to_Float use (Behind => -5,
-- Close => 5,
-- Near => 25,
-- Far => 55);
-- function to_Value (Key : in String) return Float
-- is
-- as_Enum : String_to_Float := String_to_Float'Value (Key);
-- function to_Integer is new ada.unchecked_Conversion (String_to_Float, Integer);
-- begin
-- return Float (to_Integer (as_Enum));
-- end;
-- begin
-- put_Line ("Behind => " & to_Value ("BEHIND")'Image);
-- put_Line ("Close => " & to_Value ("Close" )'Image);
-- put_Line ("Far => " & to_Value ("far" )'Image);
-- end;
procedure test -- Float accuracy is 3 decimal places.
is
use ada.Text_IO;
type String_to_Float is (Behind, Close, Near, Far);
for String_to_Float use (Behind => -5_500,
Close => 5_678,
Near => 25_321,
Far => 55_123);
function to_Value (Key : in String) return Float
is
as_Enum : String_to_Float := String_to_Float'Value (Key);
function to_Integer is new ada.unchecked_Conversion (String_to_Float, Integer);
begin
return Float (to_Integer (as_Enum)) / 1_000.0;
end;
begin
put_Line ("Behind => " & to_Value ("BEHIND")'Image);
put_Line ("Close => " & to_Value ("Close" )'Image);
put_Line ("Far => " & to_Value ("far" )'Image);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment