* Re: Constraint error overflow
2021-04-27 15:32 ` Richard Iswara
@ 2021-04-27 15:38 ` Mark Lorenzen
2021-04-27 15:44 ` Shark8
` (3 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Mark Lorenzen @ 2021-04-27 15:38 UTC (permalink / raw)
On Tuesday, April 27, 2021 at 5:32:20 PM UTC+2, haujek...@gmail.com wrote:
> So the compiler is restricted to 32 bit integer value. I had hoped for 64 bit to get 18 digits.
> Thank you.
Define your own type instead of relying on pre-defined types. Ada is not C.
Regards,
Mark L
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Constraint error overflow
2021-04-27 15:32 ` Richard Iswara
2021-04-27 15:38 ` Mark Lorenzen
@ 2021-04-27 15:44 ` Shark8
2021-04-27 15:52 ` Jeffrey R. Carter
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Shark8 @ 2021-04-27 15:44 UTC (permalink / raw)
On Tuesday, April 27, 2021 at 9:32:20 AM UTC-6, haujek wrote:
> So the compiler is restricted to 32 bit integer value. I had hoped for 64 bit to get 18 digits.
Depends on the compiler.
You can always model your domain more explicitly:
Bit_Size : Constant := 64;
Bit_First : Constant := 0;
Bit_Last : Constant := 2**Big_Bit_Size - 1;
Type Big_Integer_Test is range Bit_First..Bit_Last with Size => Bit_Size;
Function Factorial(Input:Big_Integer_Test) return Big_Integer_Test is
(case Input is
when 0 => 1,
when 1 | 2 => Input,
when others => Input * Factorial(Big_Integer_Test'Pred(Input))
);
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Constraint error overflow
2021-04-27 15:32 ` Richard Iswara
2021-04-27 15:38 ` Mark Lorenzen
2021-04-27 15:44 ` Shark8
@ 2021-04-27 15:52 ` Jeffrey R. Carter
2021-04-27 15:59 ` Dmitry A. Kazakov
2021-04-27 16:31 ` Simon Wright
4 siblings, 0 replies; 9+ messages in thread
From: Jeffrey R. Carter @ 2021-04-27 15:52 UTC (permalink / raw)
On 4/27/21 5:32 PM, Richard Iswara wrote:
>
> So the compiler is restricted to 32 bit integer value. I had hoped for 64 bit to get 18 digits.
No, the compiler defines type Integer as 32 bits. You can define other types
with larger ranges. For trying to calculate 100!, even 64 bits is not enough;
you'd need to use an unbounded integer package.
--
Jeff Carter
"There's no messiah here. There's a mess all right, but no messiah."
Monty Python's Life of Brian
84
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Constraint error overflow
2021-04-27 15:32 ` Richard Iswara
` (2 preceding siblings ...)
2021-04-27 15:52 ` Jeffrey R. Carter
@ 2021-04-27 15:59 ` Dmitry A. Kazakov
2021-04-28 7:03 ` Richard Iswara
2021-04-27 16:31 ` Simon Wright
4 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2021-04-27 15:59 UTC (permalink / raw)
On 2021-04-27 17:32, Richard Iswara wrote:
> On Tuesday, April 27, 2021 at 10:00:22 PM UTC+7, Dmitry A. Kazakov wrote:
>> On 2021-04-27 16:04, Richard Iswara wrote:
>>> Ada beginner here. I was trying to multiply the first 100 integer and GNAT throw me a constraint error, overflow. I check the error was on integer 13. So what did I do wrong here?
>> Nothing, it is just so that
>>
>> 14! > 2**31 - 1
> So the compiler is restricted to 32 bit integer value. I had hoped for 64 bit to get 18 digits.
That won't give you 100! That is 158 digits or so.
Long_Float gamma function should do the trick, or arbitrary precision
integer arithmetic.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Constraint error overflow
2021-04-27 15:59 ` Dmitry A. Kazakov
@ 2021-04-28 7:03 ` Richard Iswara
0 siblings, 0 replies; 9+ messages in thread
From: Richard Iswara @ 2021-04-28 7:03 UTC (permalink / raw)
On Tuesday, April 27, 2021 at 10:59:57 PM UTC+7, Dmitry A. Kazakov wrote:
> On 2021-04-27 17:32, Richard Iswara wrote:
> > On Tuesday, April 27, 2021 at 10:00:22 PM UTC+7, Dmitry A. Kazakov wrote:
> >> On 2021-04-27 16:04, Richard Iswara wrote:
> >>> Ada beginner here. I was trying to multiply the first 100 integer and GNAT throw me a constraint error, overflow. I check the error was on integer 13. So what did I do wrong here?
> >> Nothing, it is just so that
> >>
> >> 14! > 2**31 - 1
> > So the compiler is restricted to 32 bit integer value. I had hoped for 64 bit to get 18 digits.
> That won't give you 100! That is 158 digits or so.
>
> Long_Float gamma function should do the trick, or arbitrary precision
> integer arithmetic.
> --
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de
Thank you all of you who responded and give very helpful advices. i am very much an amateur beginner in programming . .
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Constraint error overflow
2021-04-27 15:32 ` Richard Iswara
` (3 preceding siblings ...)
2021-04-27 15:59 ` Dmitry A. Kazakov
@ 2021-04-27 16:31 ` Simon Wright
4 siblings, 0 replies; 9+ messages in thread
From: Simon Wright @ 2021-04-27 16:31 UTC (permalink / raw)
Richard Iswara <haujekchifan@gmail.com> writes:
> So the compiler is restricted to 32 bit integer value. I had hoped for
> 64 bit to get 18 digits.
GNAT comes with Long_Integer (64 bits).
Or yu could go with the Ada202x Big_Integers package
(http://www.ada-auth.org/standards/2xrm/html/RM-A-5-6.html),
pragma Ada_2020;
with Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
procedure Iswara is
use Ada.Numerics.Big_Numbers.Big_Integers;
Sum : Big_Natural := 0;
Mul : Big_Natural := 1;
begin
Ada.Text_IO.Put ( "Sum of the first 100 integers is :" );
Ada.Text_IO.New_Line;
Summing:
for I in 1 .. 100 loop
Sum := Sum + To_Big_Integer (I);
end loop Summing;
Ada.Text_IO.Put ( Sum'Image );
Ada.Text_IO.New_Line;
Ada.Text_IO.Put ( "Multiple of the first 100 integers is :" );
Ada.Text_IO.New_Line;
Multiplying:
for J in 1 .. 100 loop
Mul := Mul * To_Big_Integer (J);
end loop Multiplying;
Ada.Text_IO.Put ( Mul'Image );
Ada.Text_IO.New_Line;
end Iswara;
which compiles fine with GNAT CE 2020 & FSF GCC 11.0.1; result
$ ./iswara
Sum of the first 100 integers is :
5050
Multiple of the first 100 integers is :
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
^ permalink raw reply [flat|nested] 9+ messages in thread