Ada 增加整数大小/避免整数溢出?
在FORTRAN中,我可以将整数的声明更改为Ada 增加整数大小/避免整数溢出?,ada,Ada,在FORTRAN中,我可以将整数的声明更改为integer(kind=8),并且它可以工作 我如何在Ada中做类似的事情 我的程序处理的是非常大的数字,当它变得非常大时,它会给我一些负数 我试过: with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; with multiplication_io; use multiplication_io; procedure multipl
integer(kind=8)
,并且它可以工作
我如何在Ada中做类似的事情
我的程序处理的是非常大的数字,当它变得非常大时,它会给我一些负数
我试过:
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
with multiplication_io; use multiplication_io;
procedure multiplication is
type unsigned is range 0 .. 2**32-1;
multiplier, multiplicand : unsigned;
begin
put_line("multiplier?");
get(multiplier);
end multiplication;
但我得到以下错误:
multiplication.adb:12:05: no candidate interpretations match the actuals:
multiplication.adb:12:05: missing argument for parameter "Item" in call to "get" declared at a-tiinio.ads:70, instance at a-inteio.ads:18
multiplication.adb:12:05: missing argument for parameter "Item" in call to "get" declared at a-tiinio.ads:50, instance at a-inteio.ads:18
multiplication.adb:12:05: missing argument for parameter "Item" in call to "get" declared at a-textio.ads:239
multiplication.adb:12:05: missing argument for parameter "Item" in call to "get" declared at a-textio.ads:205
multiplication.adb:12:09: expected type "Standard.Integer"
multiplication.adb:12:09: found type "unsigned" defined at line 7
multiplication.adb:12:09: ==> in call to "Get" at a-tiinio.ads:55, instance at a-inteio.ads:18
multiplication.adb:12:09: ==> in call to "Get" at a-textio.ads:240
multiplication.adb:12:09: ==> in call to "Get" at a-textio.ads:206
gnatmake: "src/multiplication.adb" compilation error
Makefile:18: recipe for target 'default' failed
make: *** [default] Error 4
要定义范围为0到2**32的整数类型,请执行以下操作:
type My_Integer is range 0 .. 2**32;
您不需要指定它派生自哪个预定义的整数类型;让编译器为您处理这个问题
顺便说一句,您可能想要:
如果它应该占用32位
您还需要决定是要普通整数类型还是模块类型。不管你选择什么范围,它仍然有可能溢出,结果取决于你是否禁用了范围检查
对于您现在添加到问题中的示例代码,您需要为您的整数类型实例化Integer\u IO
:
package Unsigned_IO is new Ada.Text_IO.Integer_IO(unsigned);
-- ...
Unsigned_IO.Get(Multiplier);
要定义范围为0到2**32的整数类型,请执行以下操作:
type My_Integer is range 0 .. 2**32;
您不需要指定它派生自哪个预定义的整数类型;让编译器为您处理这个问题
顺便说一句,您可能想要:
如果它应该占用32位
您还需要决定是要普通整数类型还是模块类型。不管你选择什么范围,它仍然有可能溢出,结果取决于你是否禁用了范围检查
对于您现在添加到问题中的示例代码,您需要为您的整数类型实例化Integer\u IO
:
package Unsigned_IO is new Ada.Text_IO.Integer_IO(unsigned);
-- ...
Unsigned_IO.Get(Multiplier);
你没有告诉我们什么是乘法;但是,如果要进行与乘法相关的i/o(不管是什么!),您可能需要添加
使用乘法代码>
我试过了
with Ada.Text_IO; use Ada.Text_IO;
procedure Multiplication is
type Unsigned is range 0 .. 2**32-1;
Multiplier, Multiplicand : Unsigned;
package Multiplication_IO is new Ada.Text_IO.Integer_IO (Unsigned);
use Multiplication_IO;
begin
Put_Line("multiplier?");
Get(Multiplier);
end Multiplication;
它编译得很好
当然,正如@KeithThompson所建议的,我的乘法IO
应该被称为无符号IO
。关键是,不管它叫什么,它都是针对无符号的值;它不适用于整数
。您编写了get(乘数)编译器可以看到的唯一的get
是Integer\u Text\u IO
中的一个,它代表Integer
s。你需要了解Ada和类型!一本电子书将是一个开始
对于较大的数字,您可以查看Long\u Integer
:在GNAT中,它是
type Long_Integer is range -(2 **63) .. +(2 **63 - 1);
for Long_Integer'Size use 64;
你没有告诉我们什么是乘法;但是,如果要进行与乘法相关的i/o(不管是什么!),您可能需要添加使用乘法代码>
我试过了
with Ada.Text_IO; use Ada.Text_IO;
procedure Multiplication is
type Unsigned is range 0 .. 2**32-1;
Multiplier, Multiplicand : Unsigned;
package Multiplication_IO is new Ada.Text_IO.Integer_IO (Unsigned);
use Multiplication_IO;
begin
Put_Line("multiplier?");
Get(Multiplier);
end Multiplication;
它编译得很好
当然,正如@KeithThompson所建议的,我的乘法IO
应该被称为无符号IO
。关键是,不管它叫什么,它都是针对无符号的值;它不适用于整数
。您编写了get(乘数)编译器可以看到的唯一的get
是Integer\u Text\u IO
中的一个,它代表Integer
s。你需要了解Ada和类型!一本电子书将是一个开始
对于较大的数字,您可以查看Long\u Integer
:在GNAT中,它是
type Long_Integer is range -(2 **63) .. +(2 **63 - 1);
for Long_Integer'Size use 64;
如果计算结果符合常规整数(但计算可能溢出),则可能需要检查GNAT overflow elimintation()。例如,使用pragma溢出_模式(一般=>消除) 如果计算结果符合正则整数(但计算可能溢出),您可能需要检查GNAT overflow elimintation()。例如,使用pragma溢出_模式(一般=>消除) 如果启用检查,将得到一个异常,而不是“某个负数”。Ada有多个整数类型,您可以使用编译器支持的最大值来定义自己的边界。在从终端获取输入和打印输入方面,我如何定义自己的边界并使用它?我需要将其作为一个整数来对输入的数字执行BEDMAS。你有Ada教科书或其他教程或参考资料吗?它将解释如何定义整数类型并对其执行I/O。我已经尝试了很多东西,参考了我的教科书和网上的参考资料(网上没有太多关于这方面的内容)。我尝试过:子类型unsigned是范围为0的整数。。2**32; 我试过模化类型,但它似乎不能作为整数工作。什么是乘法?如果启用检查,将得到一个异常,而不是“一些负数”。Ada有多个整数类型,您可以使用编译器支持的最大值来定义自己的边界。在从终端获取输入和打印输入方面,我如何定义自己的边界并使用它?我需要将其作为一个整数来对输入的数字执行BEDMAS。你有Ada教科书或其他教程或参考资料吗?它将解释如何定义整数类型并对其执行I/O。我已经尝试了很多东西,参考了我的教科书和网上的参考资料(网上没有太多关于这方面的内容)。我尝试过:子类型unsigned是范围为0的整数。。2**32; 我尝试过模类型,但它似乎不能作为整数工作。什么是乘法\u io
?抛出错误:乘法。adb:12:05:没有候选解释与实际值匹配:乘法。adb:12:05:调用中缺少参数“Item”,以在a-tiinio声明“get”。ads:70,实例a-inteio.ads:18您尚未显示导致该错误的代码。您需要为您的类型实例化Integer\u IO
。你的教科书真的没有涵盖这一点吗?我不清楚范围为0的类型。。2**32-1
将占用32位,没有表示子句。3.5.4(9)说这样定义的类型的基类型是对称的,大约为0,这意味着