When using the Ada Big_Numbers.Big_Integers package, can the To_String function output be sliced?
I'd like to slice out the first two digits from the output of the To_String function of Ada.Big_Numbers.Big_Integers.
When trying to do the usual string slicing on the output I get a silent failure, it is just as if I typed the null statement.
On 2023-07-27 07:26, Kenneth Wolcott wrote:
When using the Ada Big_Numbers.Big_Integers package, can the To_String function output be sliced?
I'd like to slice out the first two digits from the output of the To_String function of Ada.Big_Numbers.Big_Integers.
When trying to do the usual string slicing on the output I get a silent failure, it is just as if I typed the null statement.It would be helpful if you could show what you're doing and what results you get. This
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
procedure Func_Slice is
subtype Digit is Character range '0' .. '9';
package Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Digit);
Gen : Random.Generator;
function Image (N : in Natural) return String is
(if N = 0 then "" else Random.Random (Gen) & Image (N - 1) );
-- Returns a string of N random Digits
begin -- Func_Slice
Random.Reset (Gen => Gen);
Ada.Text_IO.Put_Line (Item => Image (10) (1 .. 2) );
end Func_Slice;
works as expected
$ gnatmake -m -j0 -gnat12 -gnatan -gnato2 -O2 -fstack-check func_slice.adb x86_64-linux-gnu-gcc-12 -c -gnat12 -gnatan -gnato2 -O2 -fstack-check func_slice.adb
x86_64-linux-gnu-gnatbind-12 -x func_slice.ali
x86_64-linux-gnu-gnatlink-12 func_slice.ali -O2 -fstack-check
$ ./func_slice
10
$ ./func_slice
51
This works for me:
for I in 1 .. Max loop
Powers_of_2 := Powers_of_2 * To_Big_Integer (2);
if I >= 4 then
if To_String (Powers_Of_2)(2 .. 3) = "12" then
Put ("2^");
Put (I, 0);
Put_Line (To_String (Powers_of_2));
end if;
end if;
end loop;
Powers_of_2 := Powers_of_2 * To_Big_Integer (2);
is illegal.Powers_of_2 := Powers_of_2 * To_Big_Integer (2);With the aspect Integer_Literal, also Powers_of_2 * 2 must work. The attributes Integer_Literal, Real_Literal, Put_Image make the new big numbers nearly work like numeric types (they are not). A difference: The range syntax for instance in loops A .. B
On Saturday, July 29, 2023 at 4:07:17 AM UTC-7, AdaMagica wrote:B is illegal.
Powers_of_2 := Powers_of_2 * To_Big_Integer (2);With the aspect Integer_Literal, also Powers_of_2 * 2 must work. The attributes Integer_Literal, Real_Literal, Put_Image make the new big numbers nearly work like numeric types (they are not). A difference: The range syntax for instance in loops A ..
I understand that the range syntax in loops implies that the loop variable is acting like an enumerated type which is not true for Big_Integers.
What confused me is that I thought I had to convert the numeric literal (I'll have to go back and look at the package spec again to get this firmly understood.
I have an another question about string slicing: I've tried (obviously incorrectly) to use a variable is one of the slice integers. Can this be done?
Thanks,
Ken
On Saturday, July 29, 2023 at 4:07:17?AM UTC-7, AdaMagica wrote:
I understand that the range syntax in loops implies that the loop variablePowers_of_2 := Powers_of_2 * To_Big_Integer (2);With the aspect Integer_Literal, also Powers_of_2 * 2 must work. The attributes Integer_Literal, Real_Literal, Put_Image make the new big numbers nearly work like numeric types (they are not). A difference: The range syntax for instance in loops A .. B is illegal.
is acting like an enumerated type which is not true for Big_Integers.
What confused me is that I thought I had to convert the numeric literal
(I'll have to go back and look at the package spec again to get this
firmly understood.
I have an another question about string slicing: I've tried (obviously incorrectly) to use a variable is one of the slice integers. Can this be done?
Thanks,
Ken
'Image doesn't support any formatting, so no "Aft" or "Exp". The slicing isn't the problem. :-)
You can use Text_IO to write to a string if you need formatting. 'Image is for quick & dirty debugging, not fancy output. (Of course, if you're lucky and it does what you need, then feel free to use it.)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 366 |
Nodes: | 16 (2 / 14) |
Uptime: | 17:04:33 |
Calls: | 7,756 |
Calls today: | 7 |
Files: | 12,894 |
Messages: | 5,744,578 |