public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marc Poulhi?s <dkm@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-5025] ada: Do not use decimal approximation in -gnatRj output
Date: Thu,  5 Jan 2023 14:39:11 +0000 (GMT)	[thread overview]
Message-ID: <20230105143911.5EEE4385B526@sourceware.org> (raw)

https://gcc.gnu.org/g:3f7776ea33a6ba9dac28bff8a521de04a728e19f

commit r13-5025-g3f7776ea33a6ba9dac28bff8a521de04a728e19f
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Sat Dec 17 13:16:35 2022 +0100

    ada: Do not use decimal approximation in -gnatRj output
    
    This avoids an unnecessary loss of precision for real values.
    
    gcc/ada/
    
            * repinfo.ads (The JSON output format): Document change.
            * urealp.adb (UR_Write_To_JSON): Output a fraction instead of a
            decimal approximation.

Diff:
---
 gcc/ada/repinfo.ads |  4 ++--
 gcc/ada/urealp.adb  | 39 +++++++++++++--------------------------
 2 files changed, 15 insertions(+), 28 deletions(-)

diff --git a/gcc/ada/repinfo.ads b/gcc/ada/repinfo.ads
index 64624077cec..f9200ee04e4 100644
--- a/gcc/ada/repinfo.ads
+++ b/gcc/ada/repinfo.ads
@@ -196,8 +196,8 @@ package Repinfo is
    --    "Object_Size"          :  numerical expression
    --    "Value_Size"           :  numerical expression
    --    "Component_Size"       :  numerical expression
-   --    "Range"                :  array of numbers
-   --    "Small"                :  number
+   --    "Range"                :  array of numerical expressions
+   --    "Small"                :  numerical expression
    --    "Alignment"            :  number
    --    "Convention"           :  string
    --    "Linker_Section"       :  string
diff --git a/gcc/ada/urealp.adb b/gcc/ada/urealp.adb
index 830520788d9..37bf3a7cb79 100644
--- a/gcc/ada/urealp.adb
+++ b/gcc/ada/urealp.adb
@@ -1518,14 +1518,11 @@ package body Urealp is
    -- UR_Write_To_JSON --
    ----------------------
 
-   --  We defer to the implementation of UR_Write in all cases, either directly
-   --  for values that are naturally written in a JSON compatible format, or by
-   --  first computing a decimal approximation for other values.
+   --  We defer to the implementation of UR_Write for values that are naturally
+   --  written in a JSON compatible format and write a fraction for the others.
 
    procedure UR_Write_To_JSON (Real : Ureal) is
-      Val  : constant Ureal_Entry      := Ureals.Table (Real);
-      Imrk : constant Uintp.Save_Mark  := Mark;
-      Rmrk : constant Urealp.Save_Mark := Mark;
+      Val : constant Ureal_Entry := Ureals.Table (Real);
 
       T : Ureal;
 
@@ -1561,31 +1558,21 @@ package body Urealp is
       elsif Val.Rbase = 0 and then Val.Num mod Val.Den = 0 then
          T := Real;
 
-      --  For other constants, compute an approximation in base 10
+      --  Other non-based (rational) constants are written in num/den style
 
       else
-         declare
-            A : constant Ureal := UR_Abs (Real);
-            --  The absolute value
-
-            E : constant Uint  :=
-                  (if A < Ureal_1
-                   then UI_From_Int (3 - Decimal_Exponent_Lo (Real))
-                   else Uint_3);
-            --  The exponent for at least 3 digits after the decimal point
-
-            Num : constant Uint :=
-                    UR_To_Uint (UR_Mul (A, UR_Exponentiate (Ureal_10, E)));
-            --  The numerator appropriately rounded
-
-         begin
-            T := UR_From_Components (Num, E, 10, Val.Negative);
-         end;
+         Write_Str ("{ ""code"": ""/"", ""operands"": [ ");
+         if Val.Negative then
+            Write_Char ('-');
+         end if;
+         UI_Write (Val.Num, Decimal);
+         Write_Str (".0, ");
+         UI_Write (Val.Den, Decimal);
+         Write_Str (".0 ] }");
+         return;
       end if;
 
       UR_Write (T);
-      Release (Imrk);
-      Release (Rmrk);
    end UR_Write_To_JSON;
 
    -------------

                 reply	other threads:[~2023-01-05 14:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230105143911.5EEE4385B526@sourceware.org \
    --to=dkm@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).