1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L _ R E A L --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with System.Powten_Table; use System.Powten_Table;
33 with System.Val_Util; use System.Val_Util;
34 with System.Float_Control;
36 package body System.Val_Real is
44 Ptr : not null access Integer;
45 Max : Integer) return Long_Long_Float
48 -- Local copy of string pointer
50 Base : Long_Long_Float;
53 Uval : Long_Long_Float;
54 -- Accumulated float result
56 subtype Digs is Character range '0' .. '9';
57 -- Used to check for decimal digit
60 -- Power of Base to multiply result by
63 -- Position of starting non-blank character
66 -- Set to True if minus sign is present, otherwise to False
68 Bad_Base : Boolean := False;
69 -- Set True if Base out of range or if out of range digit
71 After_Point : Natural := 0;
72 -- Set to 1 after the point
74 Num_Saved_Zeroes : Natural := 0;
75 -- This counts zeroes after the decimal point. A non-zero value means
76 -- that this number of previously scanned digits are zero. If the end
77 -- of the number is reached, these zeroes are simply discarded, which
78 -- ensures that trailing zeroes after the point never affect the value
79 -- (which might otherwise happen as a result of rounding). With this
80 -- processing in place, we can ensure that, for example, we get the
81 -- same exact result from 1.0E+49 and 1.0000000E+49. This is not
82 -- necessarily required in a case like this where the result is not
83 -- a machine number, but it is certainly a desirable behavior.
85 procedure Bad_Based_Value;
86 pragma No_Return (Bad_Based_Value);
87 -- Raise exception for bad based value
90 -- Scans integer literal value starting at current character position.
91 -- For each digit encountered, Uval is multiplied by 10.0, and the new
92 -- digit value is incremented. In addition Scale is decremented for each
93 -- digit encountered if we are after the point (After_Point = 1). The
94 -- longest possible syntactically valid numeral is scanned out, and on
95 -- return P points past the last character. On entry, the current
96 -- character is known to be a digit, so a numeral is definitely present.
100 ---------------------
102 procedure Bad_Based_Value is
104 raise Constraint_Error with
105 "invalid based literal for 'Value";
117 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
120 -- Save up trailing zeroes after the decimal point
122 if Digit = 0 and then After_Point = 1 then
123 Num_Saved_Zeroes := Num_Saved_Zeroes + 1;
125 -- Here for a non-zero digit
128 -- First deal with any previously saved zeroes
130 if Num_Saved_Zeroes /= 0 then
131 while Num_Saved_Zeroes > Maxpow loop
132 Uval := Uval * Powten (Maxpow);
133 Num_Saved_Zeroes := Num_Saved_Zeroes - Maxpow;
134 Scale := Scale - Maxpow;
137 Uval := Uval * Powten (Num_Saved_Zeroes);
138 Scale := Scale - Num_Saved_Zeroes;
140 Num_Saved_Zeroes := 0;
143 -- Accumulate new digit
145 Uval := Uval * 10.0 + Long_Long_Float (Digit);
146 Scale := Scale - After_Point;
149 -- Done if end of input field
154 -- Check next character
156 elsif Str (P) not in Digs then
157 if Str (P) = '_' then
158 Scan_Underscore (Str, P, Ptr, Max, False);
166 -- Start of processing for System.Scan_Real
169 -- We call the floating-point processor reset routine so that we can
170 -- be sure the floating-point processor is properly set for conversion
171 -- calls. This is notably need on Windows, where calls to the operating
172 -- system randomly reset the processor into 64-bit mode.
174 System.Float_Control.Reset;
176 Scan_Sign (Str, Ptr, Max, Minus, Start);
180 -- If digit, scan numeral before point
182 if Str (P) in Digs then
186 -- Initial point, allowed only if followed by digit (RM 3.5(47))
190 and then Str (P + 1) in Digs
194 -- Any other initial character is an error
197 raise Constraint_Error with
198 "invalid character in 'Value string";
201 -- Deal with based case
203 if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
205 Base_Char : constant Character := Str (P);
207 Fdigit : Long_Long_Float;
210 -- Set bad base if out of range, and use safe base of 16.0,
211 -- to guard against division by zero in the loop below.
213 if Uval < 2.0 or else Uval > 16.0 then
222 -- Special check to allow initial point (RM 3.5(49))
224 if Str (P) = '.' then
229 -- Loop to scan digits of based number. On entry to the loop we
230 -- must have a valid digit. If we don't, then we have an illegal
231 -- floating-point value, and we raise Constraint_Error, note that
232 -- Ptr at this stage was reset to the proper (Start) value.
238 elsif Str (P) in Digs then
239 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
241 elsif Str (P) in 'A' .. 'F' then
243 Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
245 elsif Str (P) in 'a' .. 'f' then
247 Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
253 -- Save up trailing zeroes after the decimal point
255 if Digit = 0 and then After_Point = 1 then
256 Num_Saved_Zeroes := Num_Saved_Zeroes + 1;
258 -- Here for a non-zero digit
261 -- First deal with any previously saved zeroes
263 if Num_Saved_Zeroes /= 0 then
264 Uval := Uval * Base ** Num_Saved_Zeroes;
265 Scale := Scale - Num_Saved_Zeroes;
266 Num_Saved_Zeroes := 0;
269 -- Now accumulate the new digit
271 Fdigit := Long_Long_Float (Digit);
273 if Fdigit >= Base then
276 Scale := Scale - After_Point;
277 Uval := Uval * Base + Fdigit;
286 elsif Str (P) = '_' then
287 Scan_Underscore (Str, P, Ptr, Max, True);
290 -- Skip past period after digit. Note that the processing
291 -- here will permit either a digit after the period, or the
292 -- terminating base character, as allowed in (RM 3.5(48))
294 if Str (P) = '.' and then After_Point = 0 then
303 exit when Str (P) = Base_Char;
307 -- Based number successfully scanned out (point was found)
312 -- Non-based case, check for being at decimal point now. Note that
313 -- in Ada 95, we do not insist on a decimal point being present
319 if P <= Max and then Str (P) = '.' then
322 -- Scan digits after point if any are present (RM 3.5(46))
324 if P <= Max and then Str (P) in Digs then
332 -- At this point, we have Uval containing the digits of the value as
333 -- an integer, and Scale indicates the negative of the number of digits
334 -- after the point. Base contains the base value (an integral value in
335 -- the range 2.0 .. 16.0). Test for exponent, must be at least one
336 -- character after the E for the exponent to be valid.
338 Scale := Scale + Scan_Exponent (Str, Ptr, Max, Real => True);
340 -- At this point the exponent has been scanned if one is present and
341 -- Scale is adjusted to include the exponent value. Uval contains the
342 -- the integral value which is to be multiplied by Base ** Scale.
344 -- If base is not 10, use exponentiation for scaling
347 Uval := Uval * Base ** Scale;
349 -- For base 10, use power of ten table, repeatedly if necessary
352 while Scale > Maxpow loop
353 Uval := Uval * Powten (Maxpow);
354 Scale := Scale - Maxpow;
358 Uval := Uval * Powten (Scale);
362 while (-Scale) > Maxpow loop
363 Uval := Uval / Powten (Maxpow);
364 Scale := Scale + Maxpow;
368 Uval := Uval / Powten (-Scale);
372 -- Here is where we check for a bad based number
377 -- If OK, then deal with initial minus sign, note that this processing
378 -- is done even if Uval is zero, so that -0.0 is correctly interpreted.
393 function Value_Real (Str : String) return Long_Long_Float is
395 P : aliased Integer := Str'First;
397 V := Scan_Real (Str, P'Access, Str'Last);
398 Scan_Trailing_Blanks (Str, P);