[gtkada] Glade (Gate) bug when generating code for large powers of 10.

Marc A. Criley m.criley at earthlink.net
Tue Dec 2 00:52:09 CET 2003


Glade represents numbers that are powers of 10 that are greater than or
equal to 1_000_000 using exponential notation; for example one million
is depicted as "1e+06".

However, in Ada this is an integer literal, not a floating-point one.

The implementation of To_Float in Glib.Glade does not handle this, and
outputs that original representation with only a ".0" appended, thereby
preventing the generated code from compiling.

For example: code generated for a Spin_Button adjustment:

Gtk_New (Spinbutton1_Adj, 1.0, 0.0, 1e+06.0, 1.0, 10.0, 10.0);
                                    ^^^^^^^ Not compilable.


The attached patch modifies To_Float to check for this case of power of
10 exponentiation and generate a comparable floating point literal
instead:

Gtk_New (Spinbutton1_Adj, 1.0, 0.0, 1.0e+06, 1.0, 10.0, 10.0);
                                    ^^^^^^^ Compilable and correct


The modification to To_Float handles the case for positive and negative
values, as well as where the "1e" portion of the number is part of the
fractional part of the mantissa, e.g., 1.1e+06.

I have a .glade file that exercises the patch with these variations (and
which causes the original version to generate invalid code) for anyone
that wants it.  (I don't want to clutter the list with it.)

Marc A. Criley
mc at mckae.com
McKae Technologies  "The Efficient Production of High Quality Software"
www.mckae.com

-------------- next part --------------
--- glib-glade.adb.old	2003-12-01 18:33:24.000000000 -0500
+++ glib-glade.adb	2003-12-01 18:35:39.000000000 -0500
@@ -367,11 +367,38 @@
    --------------
 
    function To_Float (S : String) return String is
+      Exponentiated  : Boolean := Index (S, "e") /= 0;
+      Mantissa_Start : Natural := 0;
    begin
-      if Index (S, ".") /= 0 then
-         return S;
+      -- If the value is a power of 10 and >= 1_000_000 (which means it's being
+      -- represented as "1e+06" or +07 or +08, etc.), insert a decimal point after the
+      -- 1 to make it conform to Ada floating point syntax.
+      --
+      -- NOTE: This is _very_ Glade-specific, depending on exactly how Glade represents
+      -- large (> 1_000_000) numbers.  When it uses exponential notation, it always
+      -- normalizes the number, ie., makes it a single digit followed by a decimal point,
+      -- and correspondingly adjusts the exponent.  However, powers of 10 are not
+      -- provided with a decimal point, so their string representation will start off
+      -- with "1e" or "-1e".  Meaning that if the value is exponentiated, and the "1e"
+      -- substring starts in the first or second character, it must be a power of ten.
+      -- If that string is not present (such as in 2.3e+09) or does not start at index
+      -- S'First or S'First + 1 (as in 1.1e+06), then it is a valid number as is.
+      --
+      if Exponentiated then
+         Mantissa_Start := Index (S, "1e");
+         if Mantissa_Start not in S'First..S'First + 1 then
+            -- This is not a power of ten.
+            return S;
+         else
+            return Replace_Slice(S, Mantissa_Start, Mantissa_Start + 1, "1.0e");
+         end if;
+
       else
-         return S & ".0";
+         if Index (S, ".") /= 0 then
+            return S;
+         else
+            return S & ".0";
+         end if;
       end if;
    end To_Float;
 


More information about the gtkada mailing list