How to transform integer to float and vice-versa in Ada?

寵の児 提交于 2019-12-11 05:34:24

问题


Is there some kind of implemented function that would allow to transform an integer to float and vice versa?

I managed to write a short function that transforms an integer to float:

function Transform(First: Integer) return Float is
    A: Integer := First;
    B: Float := 0.0;
begin
    For_Loop:
    for I in Integer range 1 .. A loop
        B := B + 1.0;
    end loop For_Loop;
    return B;
end Transform;

But I don't know how to go from Float to Integer.


回答1:


Ada can do explicit type conversions

with Ada.Text_IO; use Ada.Text_IO;
procedure Convert is
  A: Integer:= 4;
  B: Float;
  C: Float := 6.8;
  D: Integer;
begin
  B := Float(A);
  Put_Line(Float'Image(B));

  D:= Integer(C);
  Put_Line(Integer'Image(D));
end Convert;


来源:https://stackoverflow.com/questions/38679423/how-to-transform-integer-to-float-and-vice-versa-in-ada

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!