From: Michal MalĂ˝ Date: Wed, 24 Dec 2014 01:10:19 +0000 (+0100) Subject: Improve rounding in Split_Int_Dec_Exp_Nums to hopefully avoid weird rounding errors X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=9caae65c123a21fb55d7de9e76d2d727d87ae636;p=Nine-Q.git Improve rounding in Split_Int_Dec_Exp_Nums to hopefully avoid weird rounding errors --- diff --git a/bin/nine_q_config.ini b/bin/nine_q_config.ini index ec8a478..f5aa0ef 100644 --- a/bin/nine_q_config.ini +++ b/bin/nine_q_config.ini @@ -4,4 +4,3 @@ Max_Connection 1000 Session True Session_Lifetime 1800.0 Session_Cleanup_Interval 3600.0 - diff --git a/src/formatting_helpers.adb b/src/formatting_helpers.adb index 63a8050..8b04719 100644 --- a/src/formatting_helpers.adb +++ b/src/formatting_helpers.adb @@ -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);