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

时间:2016-04-05 18:38:53

标签: 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 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

2 个答案:

答案 0 :(得分:2)

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

我试过

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,用于Long_Integer。你需要阅读有关Ada和类型的内容! adaic.org的其中一本电子书将是一个开始。

对于较大的数字,您可以查看type Long_Integer is range -(2 **63) .. +(2 **63 - 1); for Long_Integer'Size use 64; :在GNAT中,它是

 var ref = new Firebase(url1);
ref.once("value", function(data) {    
    $scope.userList = data.val();
    var userArray = [];
    angular.forEach($scope.userList, function(value, key) {
        userArray.push(value);
    });
    $scope.searchName = userArray[0];
    var request = new Firebase(reqUrl);
    console.log($scope.searchName);
    request.set($scope.searchName);
    var ref2 = new Firebase(url);
    ref2.once("value", function(data) {
        console.log("enter");
        $scope.infoList = data.val();
        var infoArray = [];
        angular.forEach($scope.infoList, function(value, key) {
            infoArray.push(value);
        });
        $scope.params = infoArray;
        console.log($scope.params);
    });
});

答案 1 :(得分:1)

定义范围为0到2的整数类型** 32:

type My_Integer is range 0 .. 2**32;

您无需指定从中派生的预定义整数类型;让编译器为您处理。

顺便说一下,你可能想要:

type My_Integer is range 0 .. 2**32-1;

如果它应该占用32位。

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

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

package Unsigned_IO is new Ada.Text_IO.Integer_IO(unsigned);
-- ...
Unsigned_IO.Get(Multiplier);