Misunderstanding something very simple in Ada.Big_Reals, To_String

Hi;

I’m working on an Ada solution of the Rosetta Code Task “Metallic Ratios” where I’m translating from a working Groovy solution.

The first compile error I get is:

metallic_ratios.adb:57:32: error: expected private type “Ada.Strings.Unbounded.Unbounded_String”
metallic_ratios.adb:57:32: error: found type “Standard.String”

Could someone please show me exactly what I have misunderstood from the Big_Real spec? My code is below.

Thanks,
Retired Build Engineer

pragma Ada_2022;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Numerics.Big_Numbers.Big_Reals; use Ada.Numerics.Big_Numbers.Big_Reals;

procedure Metallic_Ratios is
  type Metal_Name_Array is array (Positive range <>) of Unbounded_String;
  Metal_Names : Metal_Name_Array := (
    To_Unbounded_String ("Platinum"),
    To_Unbounded_String ("Golden"),
    To_Unbounded_String ("Silver"),
    To_Unbounded_String ("Bronze"),
    To_Unbounded_String ("Copper"),
    To_Unbounded_String ("Nickel"),
    To_Unbounded_String ("Aluminum"),
    To_Unbounded_String ("Iron"),
    To_Unbounded_String ("Tin"),
    To_Unbounded_String ("Lead"));

  procedure Lucas (B : Natural) is
    X0 : Big_Integer := To_Big_Integer (1);
    X1 : Big_Integer := X0;
    X2 : Big_Integer;
  begin
    Put ("Lucas sequence for ");
    Put (To_String (Metal_Names (B + 1)));
    Put (" ratio, where b = ");
    Put (B, 1);
    New_Line;
    Put ("First 15 elements: ");
    for I in 1..13 loop
      X2 := To_Big_Integer (B) * X1 + X0;
      Put (To_String (X2));
      Put (" ");
      X0 := X1;
      X1 := X2;
    end loop;
    New_Line (2);
  end Lucas;

  procedure Metallic (B, P : Natural) is
    X0 : Big_Integer := To_Big_Integer (1);
    X1 : Big_Integer := X0;
    X2 : Big_Integer;
    BB : Big_Integer := To_Big_Integer (B);
    Ratio : Big_Real;
    Iterations : Natural := 0;
    Previous_Real : Big_Real := To_Big_Real (To_Big_Integer (0));
    Current_Real : Big_Real;
    Previous_Real_As_String : Unbounded_String;
    Current_Real_As_String : Unbounded_String;
  begin
    Previous_Real_As_String := To_String (Arg => Previous_Real, Fore => 1, Exp => 0, Aft => P);
    loop
      Iterations := Iterations + 1;
      X2 := BB * X1 + X0;
      Current_Real := To_Big_Real (X2) / To_Big_Real (X1);
      Current_Real_As_String := To_String (Arg => Current_Real, Fore => 1, Exp => 0, Aft => P);
      if (Previous_Real_As_String = Current_Real_As_String) then
        Put ("Value after ");
        Put (Iterations, 1);
        Put ("iteration");
        if (Iterations = 1) then
          Put ("s");
        end if;
        Put (":");
        Put (Current_Real_As_String);
        New_Line;
        exit;
      end if;
      Previous_Real := Current_Real;
      Previous_Real_As_String := Current_Real_As_String;
      X0 := X1;
      X1 := X2;
    end loop;
  end Metallic;

begin
  for I in 0..9 loop
    Lucas (I);
    Metallic (I, 32);
  end loop;
  Put ("Golden ratio, where b = 1: ");
  Metallic (1, 256);
end Metallic_Ratios;
    Previous_Real_As_String : Unbounded_String;
    Current_Real_As_String : Unbounded_String;
  begin
    Previous_Real_As_String := To_String (Arg => Previous_Real, Fore => 1, Exp => 0, Aft => P);

I think this is the issue:
Here To_String returns a variable of type String while Previous_Real_As_String is of type Unbounded_String so the types do not match for the assignment.

Also affects a later line:

Current_Real_As_String := To_String (Arg => Current_Real, Fore => 1, Exp => 0, Aft => P);

Thank you, Jere.

I was trying to force-fit a poorly-conceived notion of using pre-formatted results and Ada refused to comply; I need to attack the problem in a different way. I was trying to avoid having a declare block inside a loop. Need to re-learn the Ada string types.

Retired Build Engineer