Ada:检查除法结果是否没有小数

时间:2019-10-05 16:15:47

标签: ada

我写了我的第一个ada程序,其中包含一个条件,该条件检查值是否被特定数字除以小数点:

示例:

10 / 3 = 3.3333334 >> Wrong
12 / 2 = 6         >> Okay
45 / 5 = 9         >> Okay
...

但是我找不到任何函数来做... 这是我的代码:

with Ada.Text_IO, Ada.Integer_Text_IO ;
use Ada.Text_IO, Ada.Integer_Text_IO ;

procedure main is
   ...
   testing : Natural := 0 ;
   ...

begin
   ...
         if testing/i = ??? then -- if testing/i haven't decimal part --
         ...
         end if ;
...
end main ;

1 个答案:

答案 0 :(得分:2)

这可能有效:

main.adb

with Ada.Text_IO; 

procedure Main is

   procedure Test_Remainder (X, Y : Integer) is
      use Ada.Text_IO;
   begin

      --  Optional: add some test for Y being non-zero here...

      Put (X'Image & " / " & Y'Image & "  ==>  ");       

      if (X rem Y = 0) then
         Put_Line ("Okay");
      else
         Put_Line ("Wrong");
      end if;

   end Test_Remainder;

begin   
   Test_Remainder (10, 3);
   Test_Remainder (12, 2);
   Test_Remainder (45, 5);
end Main;

输出

 10 /  3  ==>  Wrong
 12 /  2  ==>  Okay
 45 /  5  ==>  Okay

注意:有关modrem之间的区别,请参见Wikipedia