1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . I M G _ D E C --
9 -- Copyright (C) 1992-2015, 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
.Img_Int
; use System
.Img_Int
;
34 package body System
.Img_Dec
is
40 procedure Image_Decimal
46 pragma Assert
(S
'First = 1);
49 -- Add space at start for non-negative numbers
58 Set_Image_Decimal
(V
, S
, P
, Scale
, 1, Integer'Max (1, Scale
), 0);
61 ------------------------
62 -- Set_Decimal_Digits --
63 ------------------------
65 procedure Set_Decimal_Digits
66 (Digs
: in out String;
75 Minus
: constant Boolean := (Digs
(Digs
'First) = '-');
76 -- Set True if input is negative
78 Zero
: Boolean := (Digs
(Digs
'First + 1) = '0');
79 -- Set True if input is exactly zero (only case when a leading zero
80 -- is permitted in the input string given to this procedure). This
81 -- flag can get set later if rounding causes the value to become zero.
84 -- First digit position of digits remaining to be processed
86 LD
: Natural := NDigs
;
87 -- Last digit position of digits remaining to be processed
89 ND
: Natural := NDigs
- 1;
90 -- Number of digits remaining to be processed (LD - FD + 1)
92 Digits_Before_Point
: Integer := ND
- Scale
;
93 -- Number of digits before decimal point in the input value. This
94 -- value can be negative if the input value is less than 0.1, so
95 -- it is an indication of the current exponent. Digits_Before_Point
96 -- is adjusted if the rounding step generates an extra digit.
98 Digits_After_Point
: constant Natural := Integer'Max (1, Aft
);
99 -- Digit positions after decimal point in result string
102 -- Integer value of exponent
104 procedure Round
(N
: Integer);
105 -- Round the number in Digs. N is the position of the last digit to be
106 -- retained in the rounded position (rounding is based on Digs (N + 1)
107 -- FD, LD, ND are reset as necessary if required. Note that if the
108 -- result value rounds up (e.g. 9.99 => 10.0), an extra digit can be
109 -- placed in the sign position as a result of the rounding, this is
110 -- the case in which FD is adjusted. The call to Round has no effect
111 -- if N is outside the range FD .. LD.
113 procedure Set
(C
: Character);
115 -- Sets character C in output buffer
117 procedure Set_Blanks_And_Sign
(N
: Integer);
118 -- Sets leading blanks and minus sign if needed. N is the number of
119 -- positions to be filled (a minus sign is output even if N is zero
120 -- or negative, For a positive value, if N is non-positive, then
121 -- a leading blank is filled.
123 procedure Set_Digits
(S
, E
: Natural);
124 pragma Inline
(Set_Digits
);
125 -- Set digits S through E from Digs, no effect if S > E
127 procedure Set_Zeroes
(N
: Integer);
128 pragma Inline
(Set_Zeroes
);
129 -- Set N zeroes, no effect if N is negative
135 procedure Round
(N
: Integer) is
139 -- Nothing to do if rounding past the last digit we have
144 -- Cases of rounding before the initial digit
148 -- The result is zero, unless we are rounding just before
149 -- the first digit, and the first digit is five or more.
151 if N
= 1 and then Digs
(Digs
'First + 1) >= '5' then
152 Digs
(Digs
'First) := '1';
154 Digs
(Digs
'First) := '0';
158 Digits_Before_Point
:= Digits_Before_Point
+ 1;
163 -- Normal case of rounding an existing digit
169 if Digs
(N
+ 1) >= '5' then
170 for J
in reverse 2 .. N
loop
171 D
:= Character'Succ (Digs
(J
));
181 -- Here the rounding overflows into the sign position. That's
182 -- OK, because we already captured the value of the sign and
183 -- we are in any case destroying the value in the Digs buffer
185 Digs
(Digs
'First) := '1';
188 Digits_Before_Point
:= Digits_Before_Point
+ 1;
197 procedure Set
(C
: Character) is
203 -------------------------
204 -- Set_Blanks_And_Sign --
205 -------------------------
207 procedure Set_Blanks_And_Sign
(N
: Integer) is
225 end Set_Blanks_And_Sign
;
231 procedure Set_Digits
(S
, E
: Natural) is
242 procedure Set_Zeroes
(N
: Integer) is
249 -- Start of processing for Set_Decimal_Digits
252 -- Case of exponent given
255 Set_Blanks_And_Sign
(Fore
- 1);
256 Round
(Digits_After_Point
+ 2);
262 if ND
>= Digits_After_Point
then
263 Set_Digits
(FD
, FD
+ Digits_After_Point
- 1);
266 Set_Zeroes
(Digits_After_Point
- ND
);
269 -- Calculate exponent. The number of digits before the decimal point
270 -- in the input is Digits_Before_Point, and the number of digits
271 -- before the decimal point in the output is 1, so we can get the
272 -- exponent as the difference between these two values. The one
273 -- exception is for the value zero, which by convention has an
276 Expon
:= (if Zero
then 0 else Digits_Before_Point
- 1);
282 Set_Image_Integer
(Expon
, Digs
, ND
);
285 Set_Image_Integer
(-Expon
, Digs
, ND
);
288 Set_Zeroes
(Exp
- ND
- 1);
292 -- Case of no exponent given. To make these cases clear, we use
293 -- examples. For all the examples, we assume Fore = 2, Aft = 3.
294 -- A P in the example input string is an implied zero position,
295 -- not included in the input string.
298 -- Round at correct position
299 -- Input: 4PP => unchanged
300 -- Input: 400.03 => unchanged
301 -- Input 3.4567 => 3.457
302 -- Input: 9.9999 => 10.000
303 -- Input: 0.PPP5 => 0.001
304 -- Input: 0.PPP4 => 0
305 -- Input: 0.00003 => 0
307 Round
(LD
- (Scale
- Digits_After_Point
));
309 -- No digits before point in input
310 -- Input: .123 Output: 0.123
311 -- Input: .PP3 Output: 0.003
313 if Digits_Before_Point
<= 0 then
314 Set_Blanks_And_Sign
(Fore
- 1);
319 DA
: Natural := Digits_After_Point
;
320 -- Digits remaining to output after point
322 LZ
: constant Integer := Integer'Min (DA
, -Digits_Before_Point
);
323 -- Number of leading zeroes after point. Note: there used to be
324 -- a Max of this result with zero, but that's redundant, since
325 -- we know DA is positive, and because of the test above, we
326 -- know that -Digits_Before_Point >= 0.
334 -- Note: it is definitely possible for the above condition
335 -- to be True, for example:
337 -- V => 1234, Scale => 5, Fore => 0, After => 1, Exp => 0
339 -- but in this case DA = 0, ND = 1, FD = 1, FD + DA-1 = 0
340 -- so the arguments in the call are (1, 0) meaning that no
341 -- digits are output.
343 -- No obvious example exists where the following call to
344 -- Set_Digits actually outputs some digits, but we lack a
345 -- proof that no such example exists.
347 -- So it is safer to retain this call, even though as a
348 -- result it is hard (or perhaps impossible) to create a
349 -- coverage test for the inlined code of the call.
351 Set_Digits
(FD
, FD
+ DA
- 1);
355 Set_Zeroes
(DA
- ND
);
359 -- At least one digit before point in input
362 -- Less digits in input than are needed before point
363 -- Input: 1PP Output: 100.000
365 if ND
< Digits_Before_Point
then
367 -- Special case, if the input is the single digit 0, then we
368 -- do not want 000.000, but instead 0.000.
370 if ND
= 1 and then Digs
(FD
) = '0' then
371 Set_Blanks_And_Sign
(Fore
- 1);
374 -- Normal case where we need to output scaling zeroes
377 Set_Blanks_And_Sign
(Fore
- Digits_Before_Point
);
379 Set_Zeroes
(Digits_Before_Point
- ND
);
382 -- Set period and zeroes after the period
385 Set_Zeroes
(Digits_After_Point
);
387 -- Input has full amount of digits before decimal point
390 Set_Blanks_And_Sign
(Fore
- Digits_Before_Point
);
391 Set_Digits
(FD
, FD
+ Digits_Before_Point
- 1);
393 Set_Digits
(FD
+ Digits_Before_Point
, LD
);
394 Set_Zeroes
(Digits_After_Point
- (ND
- Digits_Before_Point
));
398 end Set_Decimal_Digits
;
400 -----------------------
401 -- Set_Image_Decimal --
402 -----------------------
404 procedure Set_Image_Decimal
413 Digs
: String := Integer'Image (V
);
414 -- Sign and digits of decimal value
417 Set_Decimal_Digits
(Digs
, Digs
'Length, S
, P
, Scale
, Fore
, Aft
, Exp
);
418 end Set_Image_Decimal
;