* From_string(Arg : String) return Big_Integer
@ 2021-01-09 15:16 soren rundgren
2021-01-09 17:00 ` AdaMagica
0 siblings, 1 reply; 11+ messages in thread
From: soren rundgren @ 2021-01-09 15:16 UTC (permalink / raw)
This piece of code compile with no warnings;
S: constant Big_Integer := 4547337172376300111955330758342147474062293202868155909489;
T: constant Big_Integer := 4547337172376300111955330758342147474062293202868155909393;
However the runtime message is
raised CONSTRAINT_ERROR : bad input for 'Value: "4547337172376300111955330758342147474062293202868155909489"
[2021-01-09 15:58:43] process exited with status 1, elapsed time: 00.21s
Any ideas why?
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 15:16 From_string(Arg : String) return Big_Integer soren rundgren
@ 2021-01-09 17:00 ` AdaMagica
2021-01-09 20:09 ` soren rundgren
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: AdaMagica @ 2021-01-09 17:00 UTC (permalink / raw)
Since Ada 202X is not yet released, this package is not yet fully implemented. (I had a conversation with AdaCore about a similar case a year ago.)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 17:00 ` AdaMagica
@ 2021-01-09 20:09 ` soren rundgren
2021-01-09 20:09 ` soren rundgren
2021-01-09 20:11 ` soren rundgren
2 siblings, 0 replies; 11+ messages in thread
From: soren rundgren @ 2021-01-09 20:09 UTC (permalink / raw)
lördag 9 januari 2021 kl. 18:00:36 UTC+1 skrev AdaMagica:
> Since Ada 202X is not yet released, this package is not yet fully implemented. (I had a conversation with AdaCore about a similar case a year ago.)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 17:00 ` AdaMagica
2021-01-09 20:09 ` soren rundgren
@ 2021-01-09 20:09 ` soren rundgren
2021-01-09 20:11 ` soren rundgren
2 siblings, 0 replies; 11+ messages in thread
From: soren rundgren @ 2021-01-09 20:09 UTC (permalink / raw)
lördag 9 januari 2021 kl. 18:00:36 UTC+1 skrev AdaMagica:
> Since Ada 202X is not yet released, this package is not yet fully implemented. (I had a conversation with AdaCore about a similar case a year ago.)
I managed to make a very baic solution by creating my own:
function My_From_String(S : String) return Big_Integer is
sum : Big_Integer := 0;
Len : constant Integer := S'Length;
i : Integer := 1;
begin
-- Ada.Text_IO.Put_Line("My_From_String.S = " & S);
while i <= Len loop
case S(i) is -- i = Len : Len - Len = 0,
-- i = 1 : Len - 1
when '1' => sum := sum + 1*10**(Len - i);
when '2' => sum := sum + 2*10**(Len - i);
when '3' => sum := sum + 3*10**(Len - i);
when '4' => sum := sum + 4*10**(Len - i);
when '5' => sum := sum + 5*10**(Len - i);
when '6' => sum := sum + 6*10**(Len - i);
when '7' => sum := sum + 7*10**(Len - i);
when '8' => sum := sum + 8*10**(Len - i);
when '9' => sum := sum + 9*10**(Len - i);
when others => null;
end case;
i := i + 1;
end loop;
return sum;
end My_From_String;
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 17:00 ` AdaMagica
2021-01-09 20:09 ` soren rundgren
2021-01-09 20:09 ` soren rundgren
@ 2021-01-09 20:11 ` soren rundgren
2021-01-09 22:13 ` DrPi
2 siblings, 1 reply; 11+ messages in thread
From: soren rundgren @ 2021-01-09 20:11 UTC (permalink / raw)
lördag 9 januari 2021 kl. 18:00:36 UTC+1 skrev AdaMagica:
> Since Ada 202X is not yet released, this package is not yet fully implemented. (I had a conversation with AdaCore about a similar case a year ago.)
I managed to make a very basic solution by creating my own:
function My_From_String(S : String) return Big_Integer is
sum : Big_Integer := 0;
Len : constant Integer := S'Length;
i : Integer := 1;
begin
-- Ada.Text_IO.Put_Line("My_From_String.S = " & S);
while i <= Len loop
case S(i) is -- i = Len : Len - Len = 0,
-- i = 1 : Len - 1
when '1' => sum := sum + 1*10**(Len - i);
when '2' => sum := sum + 2*10**(Len - i);
when '3' => sum := sum + 3*10**(Len - i);
when '4' => sum := sum + 4*10**(Len - i);
when '5' => sum := sum + 5*10**(Len - i);
when '6' => sum := sum + 6*10**(Len - i);
when '7' => sum := sum + 7*10**(Len - i);
when '8' => sum := sum + 8*10**(Len - i);
when '9' => sum := sum + 9*10**(Len - i);
when others => null;
end case;
i := i + 1;
end loop;
return sum;
end My_From_String;
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 20:11 ` soren rundgren
@ 2021-01-09 22:13 ` DrPi
2021-01-09 23:40 ` Jeffrey R. Carter
2021-01-10 9:46 ` Dmitry A. Kazakov
0 siblings, 2 replies; 11+ messages in thread
From: DrPi @ 2021-01-09 22:13 UTC (permalink / raw)
Le 09/01/2021 à 21:11, soren rundgren a écrit :
> lördag 9 januari 2021 kl. 18:00:36 UTC+1 skrev AdaMagica:
>> Since Ada 202X is not yet released, this package is not yet fully implemented. (I had a conversation with AdaCore about a similar case a year ago.)
>
> I managed to make a very basic solution by creating my own:
>
> function My_From_String(S : String) return Big_Integer is
> sum : Big_Integer := 0;
> Len : constant Integer := S'Length;
> i : Integer := 1;
> begin
> -- Ada.Text_IO.Put_Line("My_From_String.S = " & S);
> while i <= Len loop
> case S(i) is -- i = Len : Len - Len = 0,
> -- i = 1 : Len - 1
> when '1' => sum := sum + 1*10**(Len - i);
> when '2' => sum := sum + 2*10**(Len - i);
> when '3' => sum := sum + 3*10**(Len - i);
> when '4' => sum := sum + 4*10**(Len - i);
> when '5' => sum := sum + 5*10**(Len - i);
> when '6' => sum := sum + 6*10**(Len - i);
> when '7' => sum := sum + 7*10**(Len - i);
> when '8' => sum := sum + 8*10**(Len - i);
> when '9' => sum := sum + 9*10**(Len - i);
> when others => null;
> end case;
> i := i + 1;
> end loop;
> return sum;
> end My_From_String;
>
A much simpler function for Integer :
function Int_From_String (Str : String) return Integer is
Num : Integer := 0;
begin
for C of Str loop
Num := (Num * 10) + Integer'Value((1 => C));
end loop;
return Num;
end Int_From_String;
Notes :
- I don't have a 202x compiler, so I can't check if it works with big ints.
- It does not manage the sign (like the function you designed).
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 22:13 ` DrPi
@ 2021-01-09 23:40 ` Jeffrey R. Carter
2021-01-10 9:46 ` Dmitry A. Kazakov
1 sibling, 0 replies; 11+ messages in thread
From: Jeffrey R. Carter @ 2021-01-09 23:40 UTC (permalink / raw)
On 1/9/21 11:13 PM, DrPi wrote:
>
> function Int_From_String (Str : String) return Integer is
> Num : Integer := 0;
> begin
> for C of Str loop
> Num := (Num * 10) + Integer'Value((1 => C));
> end loop;
>
> return Num;
> end Int_From_String;
>
> Notes :
> - It does not manage the sign (like the function you designed).
There's a fairly complete Value function for unbounded integers in
PragmARC.Unbounded_Numbers.Integers at
https://github.com/jrcarter/PragmARC
It handles negative values and based values.
--
Jeff Carter
"Help! Help! I'm being repressed!"
Monty Python & the Holy Grail
67
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-09 22:13 ` DrPi
2021-01-09 23:40 ` Jeffrey R. Carter
@ 2021-01-10 9:46 ` Dmitry A. Kazakov
2021-01-10 15:48 ` DrPi
1 sibling, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2021-01-10 9:46 UTC (permalink / raw)
On 2021-01-09 23:13, DrPi wrote:
> Le 09/01/2021 à 21:11, soren rundgren a écrit :
>> lördag 9 januari 2021 kl. 18:00:36 UTC+1 skrev AdaMagica:
>>> Since Ada 202X is not yet released, this package is not yet fully
>>> implemented. (I had a conversation with AdaCore about a similar case
>>> a year ago.)
>>
>> I managed to make a very basic solution by creating my own:
>>
>> function My_From_String(S : String) return Big_Integer is
>> sum : Big_Integer := 0;
>> Len : constant Integer := S'Length;
>> i : Integer := 1;
>> begin
>> -- Ada.Text_IO.Put_Line("My_From_String.S = " & S);
>> while i <= Len loop
>> case S(i) is -- i = Len : Len - Len = 0,
>> -- i = 1 : Len - 1
>> when '1' => sum := sum + 1*10**(Len - i);
>> when '2' => sum := sum + 2*10**(Len - i);
>> when '3' => sum := sum + 3*10**(Len - i);
>> when '4' => sum := sum + 4*10**(Len - i);
>> when '5' => sum := sum + 5*10**(Len - i);
>> when '6' => sum := sum + 6*10**(Len - i);
>> when '7' => sum := sum + 7*10**(Len - i);
>> when '8' => sum := sum + 8*10**(Len - i);
>> when '9' => sum := sum + 9*10**(Len - i);
>> when others => null;
>> end case;
>> i := i + 1;
>> end loop;
>> return sum;
>> end My_From_String;
>>
> A much simpler function for Integer :
>
> function Int_From_String (Str : String) return Integer is
> Num : Integer := 0;
> begin
> for C of Str loop
for C in Str'Range loop
> Num := (Num * 10) + Integer'Value((1 => C));
Num := Num * 10 + Character'Pos (C) - Character'Pos'('0');
> Notes :
> - I don't have a 202x compiler, so I can't check if it works with big ints.
> - It does not manage the sign (like the function you designed).
- It does not handle overflow;
- It does not handle missing numbers in the input;
- It does not handle malformed input;
- It does not handle bases other than 10;
- It does not handle blank characters at the ends.
Functions mimicking S'Value (X) cover only half of cases. For the other
half you need to get the number from a part of the string, so you have
to recognize the end of the number and return the index or position
where it ends or another token begins.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-10 9:46 ` Dmitry A. Kazakov
@ 2021-01-10 15:48 ` DrPi
2021-01-10 16:02 ` Dmitry A. Kazakov
0 siblings, 1 reply; 11+ messages in thread
From: DrPi @ 2021-01-10 15:48 UTC (permalink / raw)
>> A much simpler function for Integer :
>>
>> function Int_From_String (Str : String) return Integer is
>> Num : Integer := 0;
>> begin
>> for C of Str loop
>
> for C in Str'Range loop
In this case, C is a universal integer, not a character...
>
>> Num := (Num * 10) + Integer'Value((1 => C));
>
> Num := Num * 10 + Character'Pos (C) - Character'Pos'('0');
... so you have to use :
Num := Num * 10 + Character'Pos (Str(C)) - Character'Pos'('0');
I prefer this construct but when I quickly tried it, it did'nt work.
Surely a typo somewhere.
So either :
for C of Str loop
Num := (Num * 10) + Character'Pos (C) - Character'Pos ('0');
end loop;
or :
for Idx in Str'Range loop
Num := (Num * 10) + Character'Pos (Str(Idx)) - Character'Pos
('0');
end loop;
>
>> Notes :
>> - I don't have a 202x compiler, so I can't check if it works with big
>> ints.
>> - It does not manage the sign (like the function you designed).
>
> - It does not handle overflow;
It handles overflow.
> - It does not handle missing numbers in the input;
> - It does not handle malformed input;
> - It does not handle bases other than 10;
> - It does not handle blank characters at the ends.
Sure. A very simple implementation usable in a known and controlled context.
>
> Functions mimicking S'Value (X) cover only half of cases. For the other
> half you need to get the number from a part of the string, so you have
> to recognize the end of the number and return the index or position
> where it ends or another token begins.
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-10 15:48 ` DrPi
@ 2021-01-10 16:02 ` Dmitry A. Kazakov
2021-01-10 16:33 ` DrPi
0 siblings, 1 reply; 11+ messages in thread
From: Dmitry A. Kazakov @ 2021-01-10 16:02 UTC (permalink / raw)
On 2021-01-10 16:48, DrPi wrote:
> So either :
>
> for C of Str loop
> Num := (Num * 10) + Character'Pos (C) - Character'Pos ('0');
> end loop;
>
> or :
>
> for Idx in Str'Range loop
> Num := (Num * 10) + Character'Pos (Str(Idx)) - Character'Pos
> ('0');
> end loop;
Just for fun:
subtype Digit is Character range '0'..'9';
Encode : constant array (Digit) of Integer := (0,1,2,3,4,5,6,7,8,9);
for ... loop
Num := Num * 10 + Encode (C);
>> - It does not handle overflow;
> It handles overflow.
Only if Constraint_Error must be the result.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: From_string(Arg : String) return Big_Integer
2021-01-10 16:02 ` Dmitry A. Kazakov
@ 2021-01-10 16:33 ` DrPi
0 siblings, 0 replies; 11+ messages in thread
From: DrPi @ 2021-01-10 16:33 UTC (permalink / raw)
Le 10/01/2021 à 17:02, Dmitry A. Kazakov a écrit :
> On 2021-01-10 16:48, DrPi wrote:
>
>> So either :
>>
>> for C of Str loop
>> Num := (Num * 10) + Character'Pos (C) - Character'Pos ('0');
>> end loop;
>>
>> or :
>>
>> for Idx in Str'Range loop
>> Num := (Num * 10) + Character'Pos (Str(Idx)) - Character'Pos
>> ('0');
>> end loop;
>
> Just for fun:
>
> subtype Digit is Character range '0'..'9';
> Encode : constant array (Digit) of Integer := (0,1,2,3,4,5,6,7,8,9);
>
> for ... loop
> Num := Num * 10 + Encode (C);
>
Nice one !
>>> - It does not handle overflow;
>> It handles overflow.
>
> Only if Constraint_Error must be the result.
>
Right.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2021-01-10 16:33 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-09 15:16 From_string(Arg : String) return Big_Integer soren rundgren
2021-01-09 17:00 ` AdaMagica
2021-01-09 20:09 ` soren rundgren
2021-01-09 20:09 ` soren rundgren
2021-01-09 20:11 ` soren rundgren
2021-01-09 22:13 ` DrPi
2021-01-09 23:40 ` Jeffrey R. Carter
2021-01-10 9:46 ` Dmitry A. Kazakov
2021-01-10 15:48 ` DrPi
2021-01-10 16:02 ` Dmitry A. Kazakov
2021-01-10 16:33 ` DrPi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox