如何在Ada中打印出System.Min_Int?

时间:2016-07-07 20:06:10

标签: ada

使用GNAT,我正在尝试打印System.Min_Int

Ada.Integer_Text_IO.Put(System.Min_Int);

产生这个:

  

“警告:值不在”Ada.Text_Io.Integer_Io.Num“类型的范围内”

我也试过

  

Ada.Integer_Text_IO.Put_Line(Integer'Image(System.Min_Int));

哪个收益率:

  

值不在“Standard.Integer”类型的范围内

如何打印System.Min_Int

2 个答案:

答案 0 :(得分:4)

System.Min_IntSystem.Max_Int命名数字。从逻辑上讲,它们的类型为 universal_integer 。它们可以隐式转换为整数类型(就像整数常量一样42),但当然类型需要足够大才能保存它。

没有预定义的整数类型可以保证能够保存System.Min_IntSystem.Max_Int的值。甚至不需要实现来定义Long_Integer,并且Integer只需要至少16位。

幸运的是,使用必要的范围定义自己的整数类型很容易。

with Ada.Text_IO;
with System;
procedure Min_Max is
    type Max_Integer is range System.Min_Int .. System.Max_Int;
begin
    Ada.Text_IO.Put_Line("System.Min_Int = " & Max_Integer'Image(System.Min_Int));
    Ada.Text_IO.Put_Line("System.Max_Int = " & Max_Integer'Image(System.Max_Int));
end Min_Max;

我系统上的输出:

System.Min_Int = -9223372036854775808
System.Max_Int =  9223372036854775807

答案 1 :(得分:3)

令人困惑的是,System.Min_Int在至少一个最近的Gnat中,似乎是Long_Integer(虽然Simon指出,它实际上是Long_Long_Integer,在某些编译器上但不是全部,这些都有相同的范围内)。

因此,以下工作(在gcc4.9.3中):
Put_Line(Long_Integer'Image(System.Min_Int));
报告-9223372036854775808

Ada.Long_Integer_Text_IO.Put(System.Min_Int);

也是如此

另一方面,您可能一直在尝试找到Integer类型的最小值,即... Integer'First,当然,

Put_Line(Integer'Image(Integer'First));
报告-2147483648

差异的基本原理是Ada可以支持无数个整数类型,但为方便起见,提供了一些默认值,如Integer

System.Min_Int和朋友反映了您的特定系统的限制:尝试声明更大的整数类型是合法的,但不会在您的系统上编译(即在升级编译器之前)。

在正常使用中,您将使用Integer或更好的整数typ4es,并声明适合您的问题的范围。并且每种类型的限制显然不能构建到语言甚至System包中。相反,您使用预定义的属性(例如'First'Last)来查询相关的整数类型。

因此,您可以通过以下方式探索机器的限制:

with Ada.Text_IO; use Ada.Text_IO;
with System;

procedure pmin is
   type Big_Integer is range System.Min_Int ..System.Max_Int;
   package Big_Integer_IO is new Integer_IO(Num => Big_Integer);
begin
   Big_Integer_IO.Put(System.Min_Int);
   Put(" to ");
   Big_Integer_IO.Put(System.Max_Int);
   New_Line;
end pmin;

这里(gcc4.9.3)我得到了结果:

  

-9223372036854775808 to 9223372036854775807

如果System.Min_Int不再在Gnat / gcc 6.1中的Long_Integer范围内,我很想知道这对你的系统有什么影响。请在评论中添加您的测试结果。

相关问题