]> Devoid-pointer.net GitWeb - Nine-Q.git/commitdiff
Fix Prepend_Zeros_To_Text to avoid getting "1nnnnn" when the input number is exactly...
authorMichal Malý <madcatxster@devoid-pointer.net>
Fri, 26 Dec 2014 01:44:41 +0000 (02:44 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Fri, 26 Dec 2014 01:44:41 +0000 (02:44 +0100)
src/formatting_helpers.adb
src/formatting_helpers.ads

index 8b04719d6cf0519470d8e6e7111cdd683937b6e9..1c884c7d37a4fcd6d9713faf6274aa3876e952dc 100644 (file)
@@ -48,7 +48,7 @@ package body Formatting_Helpers is
 
     Integer_Part := To_UB_Text(Ada.Strings.Fixed.Trim(Source => Integer'Image(Integer(Integer_Part_F)), Side => Ada.Strings.Left));
     Decimal_Part := To_UB_Text(Ada.Strings.Fixed.Trim(Source => Integer'Image(Decimal_Part_I), Side => Ada.Strings.Left));
-    Prepend_Zeros_To_Text(Decimal_Part_F, ExpDecimals, Decimal_Part);
+    Prepend_Zeros_To_Text(Decimal_Part_F, Decimals, Decimal_Part);
   end Split_Integer_Decimal_Unscaled_Strs;
 
   procedure Split_Integer_Decimal_Exponent_Nums(Num: in FH_Float; Decimals: in Natural; Integer_Part: out Integer; Decimal_Part: out FH_Float; Exponent_Part: out Integer) is
@@ -80,18 +80,22 @@ package body Formatting_Helpers is
 
     Log_Arg_Floored := FH_Float'Floor(Log(Base => 10.0, X => PNum));
     Expanded := 10.0 ** Log_Arg_Floored;
+    --PNum := Round_To_Valid_Nums(PNum, Decimals);
 
+      --Ada.Text_IO.Put_Line("EP: " & FH_Float'Image((PNum / Expanded) + EPSILON) & " N: " & FH_Float'Image(PNum / Expanded));
     declare
       FPE: constant FH_Float := FH_Float'Floor((PNum / Expanded) + EPSILON);
       F: constant FH_Float := FH_Float'Floor(PNum / Expanded);
     begin
+      --Ada.Text_IO.Put_Line("FPE: " & FH_Float'Image(FPE) & " F: " & FH_Float'Image(F));
       if FPE > F then
-       PNum := PNum + EPSILON;
+       PNum := PNum + (EPSILON * Expanded);
        Integer_Part_F := FPE;
       else
        Integer_Part_F := F;
       end if;
     end;
+    --Integer_Part_F := FH_Float'Floor(PNum / Expanded);
     --Ada.Text_IO.Put_Line("((PNum / Expanded) - Integer_Part_F) * Expanded = " & FH_Float'Image(((PNum / Expanded) - Integer_Part_F) * Expanded));
     Decimal_Part := ((PNum / Expanded) - Integer_Part_F) * ExpDecimals;
     if Decimal_Part < 1.0 then
@@ -104,7 +108,8 @@ package body Formatting_Helpers is
     if Negative then
       Integer_Part := Integer_Part * (-1);
     end if;
-    --Ada.Text_IO.Put_Line(FH_Float'Image(Num) & " --- " & Integer'Image(Integer_Part) & "," & FH_Float'Image(Decimal_Part) & "e" & Integer'Image(Exponent_Part));
+    --Ada.Text_IO.Put_Line(FH_Float'Image(Num) & " --- " & Integer'Image(Integer_Part) & "," & FH_Float'Image(Decimal_Part) & "e " & Integer'Image(Exponent_Part));
+    --Ada.Text_IO.Put_Line("---");
   end Split_Integer_Decimal_Exponent_Nums;
 
   procedure Split_Integer_Decimal_Exponent_Strs(Num: in FH_Float; Decimals: in Natural; Integer_Part: out UB_Text; Decimal_Part: out UB_Text;
@@ -123,17 +128,17 @@ package body Formatting_Helpers is
 
     Integer_Part := To_UB_Text(Ada.Strings.Fixed.Trim(Source => Integer'Image(Integer_Part_I), Side => Ada.Strings.Left));
     Decimal_Part := To_UB_Text(Ada.Strings.Fixed.Trim(Source => Integer'Image(Decimal_Part_I), Side => Ada.Strings.Left));
-    Prepend_Zeros_To_Text(Decimal_Part_F, ExpDecimals, Decimal_Part);
+    Prepend_Zeros_To_Text(Decimal_Part_F, Decimals, Decimal_Part);
     Exponent_Part := To_UB_Text(Ada.Strings.Fixed.Trim(Source => Integer'Image(Exponent_Part_I), Side => Ada.Strings.Left));
   end Split_Integer_Decimal_Exponent_Strs;
 
   -- BEGIN: Private functions
 
-  procedure Prepend_Zeros_To_Text(Num: in FH_Float; Decimals: in FH_Float; Text: in out UB_Text) is
+  procedure Prepend_Zeros_To_Text(Num: in FH_Float; Decimals: in Natural; Text: in out UB_Text) is
     package FHEF is new Ada.Numerics.Generic_Elementary_Functions(FH_Float);
     use FHEF;
 
-    Log_Dec: constant FH_Float := Log(Base => 10.0, X => Decimals);
+    EPSILON: constant FH_Float := 10.0 ** (-(FH_Float'Digits - 1));
     Log_Num: FH_Float;
     Diff: Integer;
     Zero_String: UB_Text;
@@ -142,9 +147,15 @@ package body Formatting_Helpers is
       Log_Num := 1.0;
     else
       Log_Num := FH_Float'Ceiling(Log(Base => 10.0, X => Num));
+      --Ada.Text_IO.Put_Line("Log_Num: " & FH_Float'Image(Log_Num));
+      --Ada.Text_IO.Put_Line("NRem: " & FH_Float'Image(FH_Float'Remainder(Num, 10.0)));
+      if Abs(FH_Float'Remainder(Num, 100.0)) <= EPSILON then
+       Log_Num := Log_Num + 1.0;
+      end if;
     end if;
-    Diff := Integer(Log_Dec - Log_Num);
-    if Diff = 0 then
+    Diff := Decimals - Integer(Log_Num);
+    --Ada.Text_IO.Put_Line("Diff: " & Integer'Image(Diff));
+    if Diff <= 0 then
       return;
     end if;
 
index 0f6bb26f712668aa60a173a66fbd40b491227b3b..12bed86be5735859541d36a29ee6c6bee9ca5d05 100644 (file)
@@ -14,6 +14,6 @@ package Formatting_Helpers is
                                                Exponent_Part: out UB_Text);
 
 private
-  procedure Prepend_Zeros_To_Text(Num: in FH_Float; Decimals: in FH_Float; Text: in out UB_Text);
+  procedure Prepend_Zeros_To_Text(Num: in FH_Float; Decimals: in Natural; Text: in out UB_Text);
 
 end Formatting_Helpers;