diff mbox series

[Ada] Ada.Numerics.Big_Numbers.Big_Reals.To_Big_Real looses precision

Message ID 20201126084101.GA20712@adacore.com
State New
Headers show
Series [Ada] Ada.Numerics.Big_Numbers.Big_Reals.To_Big_Real looses precision | expand

Commit Message

Pierre-Marie de Rodat Nov. 26, 2020, 8:41 a.m. UTC
The initial implementation was simply using 'Image which will loose
precision in some cases. Fixed by always using a sufficient number of
digits.

To_Big_Real could be made more efficient by not going through strings,
this is left for a later step.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* libgnat/a-nbnbre.adb (To_Big_Real): Do not loose precision.
diff mbox series

Patch

diff --git a/gcc/ada/libgnat/a-nbnbre.adb b/gcc/ada/libgnat/a-nbnbre.adb
--- a/gcc/ada/libgnat/a-nbnbre.adb
+++ b/gcc/ada/libgnat/a-nbnbre.adb
@@ -32,6 +32,7 @@ 
 --  This is the default version of this package, based on Big_Integers only.
 
 with Ada.Strings.Text_Output.Utils;
+with System.Img_Real; use System.Img_Real;
 
 package body Ada.Numerics.Big_Numbers.Big_Reals is
 
@@ -122,8 +123,15 @@  package body Ada.Numerics.Big_Numbers.Big_Reals is
       -----------------
 
       function To_Big_Real (Arg : Num) return Valid_Big_Real is
+         S : String (1 .. Max_Real_Image_Length);
+         P : Natural := 0;
       begin
-         return From_String (Arg'Image);
+         --  Use Long_Long_Unsigned'Width - 1 digits = 20 which is sufficient
+         --  for the largest floating point format.
+
+         Set_Image_Real
+           (Long_Long_Float (Arg), S, P, Fore => 1, Aft => 20, Exp => 5);
+         return From_String (S (1 .. P));
       end To_Big_Real;
 
       -------------------