wl_lib_0.1.3_1c94dc7c/src/wl-generic_quantities.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
with Ada.Strings.Fixed;

package body WL.Generic_Quantities is

   Local_Random_Unit_Real : Random_Unit_Real;

   function Significant_Digits_Image
     (Item : Real;
      Sig  : Positive)
      return String;

   Real_To_Quantity_Scale : constant Real := 10.0 ** Decimal_Places;
   Quantity_To_Real_Scale : constant Real := 1.0 / Real_To_Quantity_Scale;

   ---------
   -- "*" --
   ---------

   overriding function "*"
     (Left, Right : Quantity_Type)
      return Quantity_Type
   is
   begin
      return To_Quantity (To_Real (Left) * To_Real (Right));
   end "*";

   ---------
   -- "/" --
   ---------

   overriding function "/"
     (Left, Right : Quantity_Type)
      return Quantity_Type
   is
   begin
      return To_Quantity (To_Real (Left) / To_Real (Right));
   end "/";

   ------------
   -- Around --
   ------------

   function Around (X          : Quantity_Type;
                    Inflection : Unit_Real := 0.1;
                    Shape      : Distribution_Type := Linear)
                    return Quantity_Type
   is
      Factor : Unit_Real := 1.0;
   begin
      if Local_Random_Unit_Real = null then
         return X;
      end if;

      case Shape is
         when Linear =>
            Factor :=
              1.0 - Inflection + Local_Random_Unit_Real.all * 2.0 * Factor;
         when Quadratic =>
            Factor :=
              1.0 - Inflection + Local_Random_Unit_Real.all * 2.0 * Factor;
         when Normal =>
            Factor :=
              1.0 - Inflection + Local_Random_Unit_Real.all * 2.0 * Factor;
      end case;

      return To_Quantity (To_Real (X) * Factor);
   end Around;

   -----------
   -- Image --
   -----------

   function Image (Item : Quantity_Type) return String is
   begin
      return Ada.Strings.Fixed.Trim
        (Quantity_Type'Image (Item), Ada.Strings.Left);
   end Image;

   ---------
   -- Max --
   ---------

   function Max (Left, Right : Quantity_Type) return Quantity_Type is
   begin
      return Quantity_Type'Max (Left, Right);
   end Max;

   ---------
   -- Min --
   ---------

   function Min (Left, Right : Quantity_Type) return Quantity_Type is
   begin
      return Quantity_Type'Min (Left, Right);
   end Min;

   -----------
   -- Scale --
   -----------

   function Scale
     (X      : Quantity_Type;
      Factor : Real)
      return Quantity_Type
   is
   begin
      return To_Quantity (To_Real (X) * Factor);
   end Scale;

   ----------------
   -- Scale_Down --
   ----------------

   function Scale_Down
     (Value       : Quantity_Type;
      Numerator   : Quantity_Type;
      Denominator : Quantity_Type)
      return Quantity_Type
   is
   begin
      return Value * Numerator / Denominator;
   end Scale_Down;

   ---------------------------
   -- Set_Random_Unit_Real --
   ---------------------------

   procedure Set_Random_Unit_Real
     (Fn : Random_Unit_Real)
   is
   begin
      Local_Random_Unit_Real := Fn;
   end Set_Random_Unit_Real;

   ----------
   -- Show --
   ----------

   function Show (Item : Quantity_Type) return String is

      Factors    : constant array (1 .. 3) of Real :=
                     (1.0E9, 1.0E6, 1.0E3);
      Extensions : constant String := "GMK";
      R          : constant Real := To_Real (Item);
   begin

      if R = 0.0 then
         return "0";
      elsif R < 1.0 then
         declare
            Img : constant String :=
                    Natural'Image
                      (Natural
                         (Real_To_Quantity_Scale * R)
                       + Natural (Real_To_Quantity_Scale * 10.0));
            Last : Natural := Img'Last;
         begin
            while Last > 0 and then Img (Last) = '0' loop
               Last := Last - 1;
            end loop;
            if Last = 0 or else Img (Last) not in '0' .. '9' then
               return "0.0";
            else
               return "0." & Img (Img'First + 3 .. Last);
            end if;
         end;
      else
         for I in Factors'Range loop
            if R > Factors (I) then
               return Significant_Digits_Image (R / Factors (I), 3) &
               (1 => Extensions (I));
            end if;
         end loop;

         return Ada.Strings.Fixed.Trim
           (Natural'Image (Natural (R)), Ada.Strings.Left);
      end if;
   end Show;

   ------------------------------
   -- Significant_Digits_Image --
   ------------------------------

   function Significant_Digits_Image (Item : Real;
                                      Sig  : Positive)
                                     return String
   is
      Result    : String (1 .. Sig);
      Point     : Natural := 0;
      Acc       : Real := Item;
      Boundary  : constant Real := 10.0**Sig;
   begin
      if Item < 1.0 / Boundary then
         return "0.00";
      end if;

      if abs Item >= Boundary then
         return Ada.Strings.Fixed.Trim (Integer'Image (Integer (Item)),
                                        Ada.Strings.Left);
      else
         while abs Acc * 10.0 < Boundary loop
            Acc := Acc * 10.0;
            Point := Point + 1;
         end loop;

         Result :=
           Ada.Strings.Fixed.Trim (Integer'Image (Integer (Acc - 0.5)),
                                   Ada.Strings.Left);
         if Point < Sig then
            if Point = 0 then
               return Result;
            else
               declare
                  Before : constant String :=
                             Result (1 .. Result'Last - Point);
                  After  : constant String :=
                             Result (Result'Last - Point + 1 .. Result'Last);
               begin
                  if (for all Ch of After => Ch = '0') then
                     return Before;
                  else
                     return Before & "." & After;
                  end if;
               end;
            end if;
         else
            declare
               Zeroes : constant String (1 .. Point - Sig) :=
                 (others => '0');
            begin
               return "0." & Zeroes & Result;
            end;
         end if;
      end if;
   end Significant_Digits_Image;

   ----------------
   -- To_Natural --
   ----------------

   function To_Natural (Value : Quantity_Type) return Natural is
   begin
      return Natural (To_Real (Value));
   end To_Natural;

   -----------------
   -- To_Quantity --
   -----------------

   function To_Quantity (Value : Real) return Quantity_Type is
   begin
      return Quantity_Type (Real (Value * Real_To_Quantity_Scale));
   end To_Quantity;

   -----------------
   -- To_Quantity --
   -----------------

   function To_Quantity (Value : Natural) return Quantity_Type is
   begin
      return To_Quantity (Real (Value));
   end To_Quantity;

   --------------
   -- To_Real --
   --------------

   function To_Real (Value : Quantity_Type) return Real is
   begin
      return Real (Value) * Quantity_To_Real_Scale;
   end To_Real;

   ----------
   -- Unit --
   ----------

   function Unit return Quantity_Type is
   begin
      return To_Quantity (1.0);
   end Unit;

   -----------
   -- Value --
   -----------

   function Value (Image : String) return Quantity_Type is
      R : Real;
   begin
      if Image = "" then
         R := 0.0;
      elsif Image (Image'First) = '~' then
         return Around (Value (Image (Image'First + 1 .. Image'Last)));
      elsif Image (Image'Last) = 'K' then
         R := Real'Value (Image (Image'First .. Image'Last - 1)) * 1.0E3;
      elsif Image (Image'Last) = 'M' then
         R := Real'Value (Image (Image'First .. Image'Last - 1)) * 1.0E6;
      elsif Image (Image'Last) = 'G' then
         R := Real'Value (Image (Image'First .. Image'Last - 1)) * 1.0E9;
      else
         R := Real'Value (Image);
      end if;
      return To_Quantity (R);
   end Value;

   ----------
   -- Zero --
   ----------

   function Zero return Quantity_Type is
   begin
      return 0;
   end Zero;

end WL.Generic_Quantities;