]> Devoid-pointer.net GitWeb - Nine-Q.git/commitdiff
Improve rounding in Split_Int_Dec_Exp_Nums to hopefully avoid weird rounding errors
authorMichal Malý <madcatxster@devoid-pointer.net>
Wed, 24 Dec 2014 01:10:19 +0000 (02:10 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Wed, 24 Dec 2014 01:10:19 +0000 (02:10 +0100)
bin/nine_q_config.ini
src/formatting_helpers.adb

index ec8a47882afeb3bcf7749679defe1f5623560702..f5aa0ef41c2e1f7c51839ef3e124e1613cbaf4f0 100644 (file)
@@ -4,4 +4,3 @@ Max_Connection 1000
 Session True
 Session_Lifetime 1800.0
 Session_Cleanup_Interval 3600.0
-
index 63a80509649242ca59f34b5e4bec3fee968caefe..8b04719d6cf0519470d8e6e7111cdd683937b6e9 100644 (file)
@@ -55,6 +55,7 @@ package body Formatting_Helpers is
     package FHEF is new Ada.Numerics.Generic_Elementary_Functions(FH_Float);
     use FHEF;
 
+    EPSILON: constant FH_Float := 10.0 ** (-(Decimals + 1));
     ExpDecimals: constant FH_Float := 10.0 ** Decimals;
     PNum: FH_Float;
     Expanded: FH_Float;
@@ -80,8 +81,22 @@ package body Formatting_Helpers is
     Log_Arg_Floored := FH_Float'Floor(Log(Base => 10.0, X => PNum));
     Expanded := 10.0 ** Log_Arg_Floored;
 
-    Integer_Part_F := FH_Float'Floor(PNum / Expanded);
-    Decimal_Part := ((PNum - (Integer_Part_F * Expanded)) / Expanded) * ExpDecimals;
+    declare
+      FPE: constant FH_Float := FH_Float'Floor((PNum / Expanded) + EPSILON);
+      F: constant FH_Float := FH_Float'Floor(PNum / Expanded);
+    begin
+      if FPE > F then
+       PNum := PNum + EPSILON;
+       Integer_Part_F := FPE;
+      else
+       Integer_Part_F := F;
+      end if;
+    end;
+    --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
+      Decimal_Part := 0.0;
+    end if;
 
     Exponent_Part := Integer(Log_Arg_Floored);
     Integer_Part := Integer(Integer_Part_F);