首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >增加整数大小/避免整数溢出?

增加整数大小/避免整数溢出?
EN

Stack Overflow用户
提问于 2016-04-05 18:38:53
回答 3查看 626关注 0票数 0

在FORTRAN中,我可以将整数的声明更改为integer(kind = 8),它可以工作。

我如何在Ada做类似的事情?

我的程序是处理非常大的数字,当它变得很大时,它会给我一些负数。

我试过:

代码语言:javascript
复制
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;

但我得到了以下错误:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-05 20:17:51

您不告诉我们什么是multiplication_io;但是如果要做与乘法相关的i/o (不管是什么!)您可能想要添加use multiplication_io;

我试过了

代码语言:javascript
复制
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;

它汇编得很好。

当然,我的Multiplication_IO应该叫做Unsigned_IO,就像@KeithThompson建议的那样。关键是,不管它叫什么,它都适用于Unsigned值;它不适用于Integer。您编写了get(multiplier);,编译器能够看到的唯一getInteger_Text_IO中的一个,这是用于Integer的。您需要阅读有关Ada和类型的内容!adaic.org的电子书之一将是一个开始。

对于更大的数字,您可以查看Long_Integer:在GNAT中

代码语言:javascript
复制
type Long_Integer is range -(2 **63) .. +(2 **63 - 1);
for Long_Integer'Size use 64;
票数 1
EN

Stack Overflow用户

发布于 2016-04-05 19:27:15

若要定义范围为0到2**32的整数类型,请执行以下操作:

代码语言:javascript
复制
type My_Integer is range 0 .. 2**32;

您不需要指定它是从哪个预定义的整数类型派生的;让编译器来处理它。

顺便提一句,你可能想:

代码语言:javascript
复制
type My_Integer is range 0 .. 2**32-1;

如果它应该占用32位。

您还需要决定是要普通整数类型还是模块类型。无论您选择什么范围,仍然有可能使其溢出,其结果取决于您是否禁用了范围检查。

对于现在添加到问题中的示例代码,需要实例化整数类型的Integer_IO

代码语言:javascript
复制
package Unsigned_IO is new Ada.Text_IO.Integer_IO(unsigned);
-- ...
Unsigned_IO.Get(Multiplier);
票数 1
EN

Stack Overflow用户

发布于 2016-04-08 18:37:01

如果计算结果符合常规整数(但计算可能溢出),则可能需要签出蚊帐溢出消除(execution.html#management-of-overflows-in-gnat)。例如使用语用Overflow_Mode (一般=>消除);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36434443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档