simple_components_4.42.0_21c00530/strings_edit-float_edit.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
--                                                                    --
--  package Strings_Edit            Copyright (c)  Dmitry A. Kazakov  --
--  Implementation                                 Luebeck            --
--  Strings_Edit.Float_Edit                        Spring, 2000       --
--                                                                    --
--                                Last revision :  18:40 01 Aug 2019  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.IO_Exceptions;
with Ada.Numerics.Elementary_Functions;
with Strings_Edit.Integers;
pragma Elaborate_All (Strings_Edit.Integers);

use Ada.IO_Exceptions;
use Ada.Numerics.Elementary_Functions;
use Strings_Edit.Integers;

package body Strings_Edit.Float_Edit is
   --
   -- Base-radix logarithms of the number of  valid  digits  in  machine
   -- radix, i.e. how many base-radix digits  one  machine  radix  digit
   -- represents.
   --
   Logs : constant array (NumberBase) of Float :=
            (  2 => Log (Float (Number'Machine_Radix), 2.0),
               3 => Log (Float (Number'Machine_Radix), 3.0),
               4 => Log (Float (Number'Machine_Radix), 4.0),
               5 => Log (Float (Number'Machine_Radix), 5.0),
               6 => Log (Float (Number'Machine_Radix), 6.0),
               7 => Log (Float (Number'Machine_Radix), 7.0),
               8 => Log (Float (Number'Machine_Radix), 8.0),
               9 => Log (Float (Number'Machine_Radix), 9.0),
              10 => Log (Float (Number'Machine_Radix), 10.0),
              11 => Log (Float (Number'Machine_Radix), 11.0),
              12 => Log (Float (Number'Machine_Radix), 12.0),
              13 => Log (Float (Number'Machine_Radix), 13.0),
              14 => Log (Float (Number'Machine_Radix), 14.0),
              15 => Log (Float (Number'Machine_Radix), 15.0),
              16 => Log (Float (Number'Machine_Radix), 16.0)
            );
   --
   -- GetExponentField -- Calculate the length of the exponent
   --
   --	 Base	- Of the number
   --
   -- This  function  calculates  how  many  digits  are  necessary   to
   -- represent any Base-radix exponent in decimal the format.
   --
   -- Returns :
   --
   --	 The exponent length
   --
   function GetExponentField (Base : NumberBase) return Natural is
   begin
      if Number'Machine_Emax > -Number'Machine_Emin then
         return
            Natural
            (  Log (Float (Number'Machine_Emax) * Logs (Base), 10.0)
            +  0.5
            );
      else
         return
            Natural
            (  Log (Float (-Number'Machine_Emin) * Logs (Base), 10.0)
            +  0.5
            );
      end if;
   end GetExponentField;
   --
   -- The exponent lengths for different Bases
   --
   ExponentField : constant array (NumberBase) of Natural :=
                      (  GetExponentField (2),  GetExponentField (3),
                         GetExponentField (4),  GetExponentField (5),
                         GetExponentField (6),  GetExponentField (7),
                         GetExponentField (8),  GetExponentField (9),
                         GetExponentField (10), GetExponentField (11),
                         GetExponentField (12), GetExponentField (13),
                         GetExponentField (14), GetExponentField (15),
                         GetExponentField (16)
                      );
   --
   -- GetExponent -- Get the exponent part from the string
   --
   --	    Source  - The string
   --	    Pointer - Within the string
   --	    Value   - Of the exponent (output)
   --
   -- This procedure scans the Source string starting from  the  Pointer
   -- position  for  the  exponent  part of a number. Spaces are skipped
   -- first. The exponent can be introduced by either E or e. Its  value
   -- is  a decimal number returned via Value. If no valid exponent part
   -- present Value is set to zero (the Pointer remains intact).
   --
   procedure GetExponent
             (  Source   : in String;
                Pointer  : in out Integer;
                Value    : out Integer
             )  is
      Index : Integer := Pointer;
   begin
      Get (Source, Index);
      if (  Index > Source'Last
         or else
            (  Source (Index) /= 'E'
            and
               Source (Index) /= 'e'
         )  )
      then
         Value := 0;
      else
         Index := Index + 1;
         Get (Source, Index);
         Get (Source, Index, Value, 10, -1000, +1000, True, True);
         Pointer := Index;
      end if;
   exception
      when End_Error | Data_Error | Layout_Error | Constraint_Error =>
         Value := 0;
   end GetExponent;

   procedure Get
             (  Source   : in String;
                Pointer  : in out Integer;
                Value    : out Number'Base;
                Base     : in NumberBase := 10;
                First    : in Number'Base := Number'Base'First;
                Last     : in Number'Base := Number'Base'Last;
                ToFirst  : in Boolean := False;
                ToLast   : in Boolean := False
             )  is
      Radix        : constant Number'Base := Number'Base (Base);
      Digit        : Natural;
      Exponent     : Integer;
      ExponentPart : Integer;
      Mantissa     : Number'Base := 0.0;
      Multiplicand : Number'Base := 1.0;
      Sign         : Integer := 0;
      Index        : Integer := Pointer;
   begin
      if (  Pointer < Source'First
         or else
            (  Pointer > Source'Last
            and then
               Pointer - 1 > Source'Last
         )  )
      then
         raise Layout_Error;
      end if;
      if Pointer <= Source'Last then
         case Source (Index) is
            when '-' =>
               Index := Index + 1;
               Sign := -1;
               Get (Source, Index);
            when '+' =>
               Index := Index + 1;
               Sign := 1;
               Get (Source, Index);
            when others =>
               null;
         end case;
      end if;
      ExponentPart := Index;
      Exponent := Index;
      while Index <= Source'Last loop
         Digit := GetDigit (Source (Index));
         exit when Digit >= Base;
         Mantissa := Mantissa + Number'Base (Digit) * Multiplicand;
         Multiplicand := Multiplicand / Radix;
         Index := Index + 1;
      end loop;
      Exponent := Index - Exponent - 1;
      if Index + 1 <= Source'Last and then Source (Index) = '.' then
         Index := Index + 1;
         Digit := GetDigit (Source (Index));
         if Digit < Base then
            loop
               Mantissa :=
                  Mantissa + Number'Base (Digit) * Multiplicand;
               Multiplicand := Multiplicand / Radix;
               Index := Index + 1;
               exit when Index > Source'Last;
               Digit := GetDigit (Source (Index));
               exit when Digit >= Base;
            end loop;
         else
            Index := Index - 1;
         end if;
      end if;
      if 0 = Sign then
         if Pointer = Index then
            raise End_Error; -- No fraction part - no number
         end if;
         Sign := 1;
      else
         if Pointer = Index - 1 then
            raise Data_Error; -- Only a sign
         end if;
      end if;
      if (  ExponentPart = Index - 1
         and then
            Source (ExponentPart) = '.'
         )
      then
         raise Data_Error; -- Only decimal point is present
      end if;
      GetExponent (Source, Index, ExponentPart);
      if Mantissa > 0.0 then
         begin
            Mantissa := Number'Base (Sign) * Mantissa;
            Exponent := ExponentPart + Exponent;
            ExponentPart :=
               Integer (Float (Number'Machine_Emax) * Logs (Base) * 0.7);
            if Exponent >= ExponentPart then
               Multiplicand := Radix ** ExponentPart;
               loop
                  Mantissa := Mantissa * Multiplicand;
                  Exponent := Exponent - ExponentPart;
                  exit when Exponent < ExponentPart;
               end loop;
            elsif Exponent <= -ExponentPart then
               ExponentPart := -ExponentPart;
               Multiplicand := Radix ** ExponentPart;
               loop
                  Mantissa := Mantissa * Multiplicand;
                  Exponent := Exponent - ExponentPart;
                  exit when Exponent > ExponentPart;
               end loop;
            end if;
            Mantissa := Mantissa * Radix ** Exponent;
            if not Mantissa'Valid then
               if Sign > 0 and ToLast then
                  Value   := Last;
                  Pointer := Index;
                  return;
               elsif Sign < 0 and ToFirst then
                  Value   := First;
                  Pointer := Index;
                  return;
               end if;
               raise Constraint_Error;
            end if;
         exception
            when Numeric_Error | Constraint_Error =>
               if Sign > 0 and ToLast then
                  Value   := Last;
                  Pointer := Index;
                  return;
               elsif Sign < 0 and ToFirst then
                  Value   := First;
                  Pointer := Index;
                  return;
               end if;
               raise Constraint_Error;
         end;
      end if;
      if Mantissa < First then
         if ToFirst then
            Mantissa := First;
         else
            raise Constraint_Error;
         end if;
      elsif Mantissa > Last then
         if ToLast then
            Mantissa := Last;
         else
            raise Constraint_Error;
         end if;
      end if;
      Value := Mantissa;
      Pointer := Index;
   end Get;

   function Value
            (  Source  : in String;
               Base    : in NumberBase := 10;
               First   : in Number'Base := Number'First;
               Last    : in Number'Base := Number'Last;
               ToFirst : in Boolean := False;
               ToLast  : in Boolean := False
            )  return Number'Base is
      Result  : Number'Base;
      Pointer : Integer := Source'First;
   begin
      Get (Source, Pointer, SpaceAndTab);
      Get (Source, Pointer, Result, Base, First, Last, ToFirst, ToLast);
      Get (Source, Pointer, SpaceAndTab);
      if Pointer /= Source'Last + 1 then
         raise Data_Error;
      end if;
      return Result;
   end Value;

   procedure Put
             (  Destination : in out String;
                Pointer     : in out Integer;
                Value       : in Number'Base;
                Base        : in NumberBase := 10;
                PutPlus     : in Boolean := False;
                RelSmall    : in Positive := MaxSmall;
                AbsSmall    : in Integer := -MaxSmall;
                Field       : in Natural := 0;
                Justify     : in Alignment := Left;
                Fill        : in Character := ' '
             )  is
      --
      -- Sign -- Make the sign of a number
      --
      --    Value   - Of the number
      --    PutPlus - Always put sign, if true
      --
      -- Returns :
      --
      --    The string to be used as the sign
      --
      function Sign
               (  Value   : Number'Base;
                  PutPlus : Boolean
               )  return String is
      begin
         if Value < 0.0 then
            return "-";
         elsif PutPlus then
            return "+";
         else
            return "";
         end if;
      end Sign;
      --
      -- PutExponent -- Make the exponent part
      --
      --    Value  - Of the exponent part
      --    Base   - Of the mantissa
      --
      -- The  exponent  part  is  written in the fixed length format. It
      -- always  has  sign.  Leading  zeros are added to make the length
      -- fixed. The space is added in front of  the  exponent  part  for
      -- bases 15 and 16.
      --
      -- Returns :
      --
      --    The string containing the exponent part
      --
      function PutExponent
               (  Value : Integer;
                  Base  : NumberBase
               )  return String is
         Result   : String (1..32);
         Exponent : Integer := Value;
         Pointer  : Integer := Result'First;
         Sign     : Character := '+';
      begin
         if Exponent < 0 then
            Sign := '-';
            Exponent := -Value;
         end if;
         Put
         (  Result,
            Pointer,
            Exponent,
            Field   => ExponentField (Base),
            PutPlus => False,
            Justify => Right,
            Fill    => '0'
         );
         if Base >= 15 then
            return " e" & Sign & Result (Result'First..Pointer - 1);
         else
            return 'E' & Sign & Result (Result'First..Pointer - 1);
         end if;
      end PutExponent;

      Precision : Integer :=   -- The maximal available precision
                     Integer
                     (  Float (Number'Machine_Mantissa)
                     *  Logs (Base)
                     );
      Mantissa  : Number'Base := abs Value;
      Exponent  : Integer := 0;
      Increment : Integer := 0;
      Radix     : constant Number'Base := Number'Base (Base);
   begin
      if Mantissa <= 0.0 then
         Put (Destination, Pointer, "0", Field, Justify, Fill);
         return;
      end if;
      if RelSmall < Precision then
         Precision := RelSmall;
      end if;
      --
      -- Normalization. The goal is to bring the mantissa into the range
      -- ]1..1/Base]
      --
      while Mantissa < 1.0 / Radix loop
         Mantissa := Mantissa * Radix;
         Exponent := Exponent - 1;
      end loop;
      while Mantissa >= 1.0 loop
         Mantissa := Mantissa / Radix;
         Exponent := Exponent + 1;
      end loop;
      if Exponent - AbsSmall < Precision then
         Precision := Exponent - AbsSmall;
         Increment := 1;
      end if;
      --
      -- Rounding
      --
      Mantissa := Mantissa + 0.5 / Radix ** Precision;
      if Mantissa >= 1.0 then
         Mantissa  := Mantissa / Radix;
         Exponent  := Exponent + 1;
	 Precision := Precision + Increment;
      end if;
      if Precision > 0 then
         declare
            ExponentPart : constant String :=
                           PutExponent (Exponent - 1, Base);
            MantissaPart : String (1..Precision);
            Digit        : Natural;
         begin
            --
            -- Convert the Mantissa to character string
            --
            for Index in MantissaPart'range loop
               Mantissa := Mantissa * Radix;
               Digit := Integer (Mantissa);
               if Number'Base (Digit) > Mantissa then
                  Digit := Digit - 1;
               end if;
               Mantissa := Mantissa - Number'Base (Digit);
               MantissaPart (Index) := Figures (Digit + Figures'First);
            end loop;
            if (  Exponent < - ExponentPart'Length
               or Exponent > Precision
               )
            then
               --
               -- Format X.XXXXXeYYY
               --
               Put
               (  Destination,
	            Pointer,
                  (  Sign (Value, PutPlus)
                  &  MantissaPart (MantissaPart'First)
                  &  '.'
                  &  MantissaPart
                     (  MantissaPart'First + 1
                     .. MantissaPart'Last
                     )
                  &  ExponentPart
                  ),
                  Field,
                  Justify,
                  Fill
               );
            elsif Exponent = Precision then
               --
               -- Format: XXXXXXX
               --
               Put
               (  Destination,
                  Pointer,
                  Sign (Value, PutPlus) & MantissaPart,
                  Field,
                  Justify,
                  Fill
	         );
            elsif Exponent > 0 then
               --
               -- Format: XXXX.XXX
               --
               Put
               (  Destination,
                  Pointer,
                  (  Sign (Value, PutPlus)
                  &  MantissaPart
                     (  MantissaPart'First
                     .. MantissaPart'First + Exponent - 1
                     )
                  &  '.'
                  &  MantissaPart
                     (  MantissaPart'First + Exponent
                     .. MantissaPart'Last
                  )  ),
                  Field,
                  Justify,
                  Fill
               );
            else
               --
               -- Format: 0.000XXXXX
               --
               Put
               (  Destination,
                  Pointer,
                  (  Sign (Value, PutPlus)
                  &  "0."
                  &  String'(1..-Exponent => '0')
                  &  MantissaPart
                  ),
                  Field,
                  Justify,
                  Fill
               );
            end if;
         end;
      else
         --
         -- Zero
         --
         Put (Destination, Pointer, "0", Field, Justify, Fill);
      end if;
   end Put;

   function Image
            (  Value    : in Number'Base;
               Base     : in NumberBase := 10;
               PutPlus  : in Boolean    := False;
               RelSmall : in Positive   := MaxSmall;
               AbsSmall : in Integer    := -MaxSmall
            )  return String is
      Text    : String (1..80);
      Pointer : Integer := Text'First;
   begin
      Put (Text, Pointer, Value, Base, PutPlus, RelSmall, AbsSmall);
      return Text (Text'First..Pointer - 1);
   end Image;

end Strings_Edit.Float_Edit;