Debian Codegraph @

source_2005/pre_math.mss

raw
1 @comment{ $Source: e:\\cvsroot/ARM/Source/pre_math.mss,v $ }
2 @comment{ $Revision: 1.37 $ $Date: 2006/02/16 06:49:06 $ $Author: Randy $ }
3 @Part(predefmath, Root="ada.mss")
4
5 @Comment{$Date: 2006/02/16 06:49:06 $}
6
7 @LabeledClause{The Numerics Packages}
8
9 @begin{Intro}
10 The library package Numerics is the parent of several child units that provide
11 facilities for mathematical computation. One child, the generic package
12 Generic_Elementary_Functions, is defined in @RefSecNum{Elementary Functions},
13 together with nongeneric equivalents;
14 two others,
15 the package Float_Random and the generic package Discrete_Random,
16 are defined in @RefSecNum{Random Number Generation}.
17 Additional (optional)
18 children are defined in @RefSec{Numerics}.
19 @end{Intro}
20
21 @begin{StaticSem}
22 @ChgRef{Version=[1], Kind=[Deleted]}
23 @ChgDeleted[Version=[1],Text=<@ @;@comment{Empty paragraph to hang junk paragraph number from original RM}>]
24 @begin{Example}
25 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00388-01]}
26 @key[package] Ada.Numerics @key[is]@ChildUnit{Parent=[Ada],Child=[Numerics]}
27 @key[pragma] Pure(Numerics);
28 @AdaExcDefn{Argument_Error} : @key[exception];
29 @AdaObjDefn{Pi} : @key[constant] :=
30 3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;@Chg{Version=[2],New=[
31 @pi : @key[constant] := Pi;],Old=[]}
32 @AdaObjDefn{e} : @key[constant] :=
33 2.71828_18284_59045_23536_02874_71352_66249_77572_47093_69996;
34 @key[end] Ada.Numerics;
35 @end{Example}
36
37 The Argument_Error exception is raised by a subprogram in
38 a child unit of Numerics
39 to signal that one or more of the actual subprogram parameters are outside the
40 domain of the corresponding mathematical function.
41
42 @end{StaticSem}
43
44 @begin{ImplPerm}
45 The implementation may specify the values of Pi and e to a larger number of
46 significant digits.
47 @begin{Reason}
48 51 digits seem more than adequate for all present computers; converted to
49 binary, the values given above are accurate to more than 160 bits.
50 Nevertheless, the permission allows implementations to accommodate
51 unforeseen hardware advances.
52 @end{Reason}
53 @end{ImplPerm}
54
55 @begin{Extend83}
56 @Defn{extensions to Ada 83}
57 Numerics and its children were not predefined in Ada 83.
58 @end{Extend83}
59
60 @begin{Extend95}
61 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00388-01]}
62 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
63 The alternative declaration of @pi is new.]}
64 @end{Extend95}
65
66
67 @LabeledSubClause{Elementary Functions}
68
69 @begin{Intro}
70 Implementation-defined
71 approximations to the mathematical functions known as the @lquotes@;elementary
72 functions@rquotes@; are provided by the subprograms in
73 Numerics.@!Generic_@!Elementary_@!Functions. Nongeneric equivalents of this generic
74 package for each of the predefined floating point types are also provided as
75 children of Numerics.
76 @ImplDef{The accuracy actually achieved by the elementary functions.}
77 @end{Intro}
78
79 @begin{StaticSem}
80 @Leading@;The generic library package
81 Numerics.Generic_Elementary_Functions has the following declaration:
82 @begin{Example}
83 @key{generic}
84 @key{type} Float_Type @key{is} @key{digits} <>;
85 @ChildUnit{Parent=[Ada.Numerics],Child=[Generic_@!Elementary_@!Functions]}
86 @key{package} Ada.Numerics.Generic_Elementary_Functions @key{is}
87 @key[pragma] Pure(Generic_Elementary_Functions);
88
89 @key{function} @AdaSubDefn{Sqrt} (X : Float_Type'Base) @key{return} Float_Type'Base;
90 @key{function} @AdaSubDefn{Log} (X : Float_Type'Base) @key{return} Float_Type'Base;
91 @key{function} @AdaSubDefn{Log} (X, Base : Float_Type'Base) @key{return} Float_Type'Base;
92 @key{function} @AdaSubDefn{Exp} (X : Float_Type'Base) @key{return} Float_Type'Base;
93 @key{function} "**" (Left, Right : Float_Type'Base) @key{return} Float_Type'Base;
94
95 @key{function} @AdaSubDefn{Sin} (X : Float_Type'Base) @key{return} Float_Type'Base;
96 @key{function} @AdaSubDefn{Sin} (X, Cycle : Float_Type'Base) @key{return} Float_Type'Base;
97 @key{function} @AdaSubDefn{Cos} (X : Float_Type'Base) @key{return} Float_Type'Base;
98 @key{function} @AdaSubDefn{Cos} (X, Cycle : Float_Type'Base) @key{return} Float_Type'Base;
99 @key{function} @AdaSubDefn{Tan} (X : Float_Type'Base) @key{return} Float_Type'Base;
100 @key{function} @AdaSubDefn{Tan} (X, Cycle : Float_Type'Base) @key{return} Float_Type'Base;
101 @key{function} @AdaSubDefn{Cot} (X : Float_Type'Base) @key{return} Float_Type'Base;
102 @key{function} @AdaSubDefn{Cot} (X, Cycle : Float_Type'Base) @key{return} Float_Type'Base;
103
104 @key{function} @AdaSubDefn{Arcsin} (X : Float_Type'Base) @key{return} Float_Type'Base;
105 @key{function} @AdaSubDefn{Arcsin} (X, Cycle : Float_Type'Base) @key{return} Float_Type'Base;
106 @key{function} @AdaSubDefn{Arccos} (X : Float_Type'Base) @key{return} Float_Type'Base;
107 @key{function} @AdaSubDefn{Arccos} (X, Cycle : Float_Type'Base) @key{return} Float_Type'Base;
108 @key{function} @AdaSubDefn{Arctan} (Y : Float_Type'Base;
109 X : Float_Type'Base := 1.0)
110 @key{return} Float_Type'Base;
111 @key{function} @AdaSubDefn{Arctan} (Y : Float_Type'Base;
112 X : Float_Type'Base := 1.0;
113 Cycle : Float_Type'Base) @key{return} Float_Type'Base;
114 @key{function} @AdaSubDefn{Arccot} (X : Float_Type'Base;
115 Y : Float_Type'Base := 1.0)
116 @key{return} Float_Type'Base;
117 @key{function} @AdaSubDefn{Arccot} (X : Float_Type'Base;
118 Y : Float_Type'Base := 1.0;
119 Cycle : Float_Type'Base) @key{return} Float_Type'Base;
120
121 @key{function} @AdaSubDefn{Sinh} (X : Float_Type'Base) @key{return} Float_Type'Base;
122 @key{function} @AdaSubDefn{Cosh} (X : Float_Type'Base) @key{return} Float_Type'Base;
123 @key{function} @AdaSubDefn{Tanh} (X : Float_Type'Base) @key{return} Float_Type'Base;
124 @key{function} @AdaSubDefn{Coth} (X : Float_Type'Base) @key{return} Float_Type'Base;
125 @key{function} @AdaSubDefn{Arcsinh} (X : Float_Type'Base) @key{return} Float_Type'Base;
126 @key{function} @AdaSubDefn{Arccosh} (X : Float_Type'Base) @key{return} Float_Type'Base;
127 @key{function} @AdaSubDefn{Arctanh} (X : Float_Type'Base) @key{return} Float_Type'Base;
128 @key{function} @AdaSubDefn{Arccoth} (X : Float_Type'Base) @key{return} Float_Type'Base;
129
130 @key{end} Ada.Numerics.Generic_Elementary_Functions;
131 @end{Example}
132
133 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0020],ARef=[AI95-00126-01]}
134 @ChildUnit{Parent=[Ada.Numerics],Child=[Elementary_@!Functions]}
135 The library package Numerics.Elementary_Functions
136 @Chg{New=[is declared pure and ],Old=[]}defines the same subprograms as
137 Numerics.@!Generic_@!Elementary_@!Functions,
138 except that the predefined type Float is systematically substituted for
139 Float_Type'Base throughout. Nongeneric equivalents of
140 Numerics.@!Generic_@!Elementary_@!Functions for each of the other predefined
141 floating point types are defined similarly, with the names
142 Numerics.@!Short_@!Elementary_@!Functions, Numerics.@!Long_@!Elementary_@!Functions, etc.
143 @begin{Reason}
144 The nongeneric equivalents are provided to allow the programmer to
145 construct simple mathematical applications without being required to
146 understand and use generics.
147 @end{Reason}
148
149 The functions have their usual mathematical meanings. When the Base parameter
150 is specified, the Log function computes the logarithm to the given base;
151 otherwise, it computes the natural logarithm. When the Cycle parameter is
152 specified, the parameter X of the forward trigonometric functions (Sin, Cos,
153 Tan, and Cot) and the results of the inverse trigonometric functions (Arcsin,
154 Arccos, Arctan, and Arccot) are measured in units such that a full cycle of
155 revolution has the given value; otherwise, they are measured in radians.
156
157 @Leading@;The computed results of the mathematically multivalued functions are
158 rendered single-valued by the following conventions, which are meant to imply
159 the principal branch:
160 @begin{Itemize}
161 The results of the Sqrt and Arccosh functions and that of the exponentiation
162 operator are nonnegative.
163
164 The result of the Arcsin function is in the quadrant containing the point
165 (1.0, @i[x]), where @i[x] is the value of the parameter X. This quadrant is
166 I or IV; thus, the range of the Arcsin function is approximately
167 @en@Pi/2.0 to @Pi/2.0
168 (@en@R[Cycle]/4.0 to @R[Cycle]/4.0,
169 if the parameter Cycle is specified).
170
171 The result of the Arccos function is in the quadrant containing the point
172 (@i{x}, 1.0), where @i[x] is the value of the parameter X. This quadrant is
173 I or II; thus, the Arccos function ranges from 0.0 to approximately
174 @Pi (@R[Cycle]/2.0, if the parameter Cycle is specified).
175
176 The results of the Arctan and Arccot functions are in the quadrant
177 containing the point (@i[x], @i[y]), where @i[x] and @i[y] are the values of
178 the parameters X and Y, respectively. This may be any quadrant (I through
179 IV) when the parameter X (resp., Y) of Arctan (resp., Arccot) is specified,
180 but it is restricted to quadrants I and IV (resp., I and II) when that
181 parameter is omitted. Thus, the range when that parameter is specified is
182 approximately @en@Pi to @Pi
183 (@en@R[Cycle]/2.0 to @R[Cycle]/2.0,
184 if the parameter Cycle is specified); when omitted, the range of Arctan
185 (resp., Arccot) is that of Arcsin (resp., Arccos), as given above. When the
186 point (@i[x], @i[y]) lies on the negative x-axis, the result approximates
187 @begin{Itemize}
188 @Pi (resp., @en@Pi) when the sign of the parameter Y is
189 positive (resp., negative), if Float_Type'Signed_Zeros is True;
190
191 @Pi, if Float_Type'Signed_Zeros is False.
192 @end{Itemize}
193 @end{Itemize}
194
195 (In the case of the inverse trigonometric functions, in which a result lying on
196 or near one of the axes may not be exactly representable, the approximation
197 inherent in computing the result may place it in an adjacent quadrant, close to
198 but on the wrong side of the axis.)
199 @end{StaticSem}
200
201 @begin{RunTime}
202 @Leading@;The exception Numerics.Argument_Error is raised, signaling a
203 parameter value outside the domain of the corresponding mathematical function,
204 in the following cases:
205 @begin{Itemize}
206 by any forward or inverse trigonometric function with specified cycle, when
207 the value of the parameter Cycle is zero or negative;
208
209 by the Log function with specified base, when the value of the parameter
210 Base is zero, one, or negative;
211
212 by the Sqrt and Log functions, when the value of the parameter X is
213 negative;
214
215 by the exponentiation operator, when the value of the left operand is
216 negative or when both operands have the value zero;
217
218 by the Arcsin, Arccos, and Arctanh functions, when the absolute value of the
219 parameter X exceeds one;
220
221 by the Arctan and Arccot functions, when the parameters X and Y both have
222 the value zero;
223
224 by the Arccosh function, when the value
225 of the parameter X is less than one; and
226
227 by the Arccoth function, when the absolute value
228 of the parameter X is less than one.
229 @end{Itemize}
230
231 @Leading@IndexCheck{Division_Check}
232 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
233 The exception Constraint_Error is raised, signaling a pole of the mathematical
234 function (analogous to dividing by zero), in the following cases, provided that
235 Float_Type'Machine_Overflows is True:
236 @begin{Itemize}
237 by the Log, Cot, and Coth functions, when the value of the parameter X is
238 zero;
239
240 by the exponentiation operator, when the value of the left operand is zero
241 and the value of the exponent is negative;
242
243 by the Tan function with specified cycle, when the value of the parameter X
244 is an odd multiple of the quarter cycle;
245
246 by the Cot function with specified cycle, when the value of the parameter X
247 is zero or a multiple of the half cycle; and
248
249 by the Arctanh and Arccoth functions, when the absolute value of the
250 parameter X is one.
251 @end{Itemize}
252
253 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
254 @redundant[Constraint_Error can also be raised
255 when a finite result overflows
256 (see @RefSecNum{Accuracy Requirements for the Elementary Functions}); this may
257 occur for parameter values sufficiently @i{near} poles, and, in the case of
258 some of the functions, for parameter values with sufficiently large
259 magnitudes.]@PDefn{unspecified} When Float_Type'Machine_Overflows is False,
260 the result at poles is unspecified.
261 @begin{Reason}
262 The purpose of raising Constraint_Error (rather than
263 Numerics.Argument_Error) at the poles of a function, when
264 Float_Type'Machine_Overflows is True, is to provide continuous behavior as
265 the actual parameters of the function approach the pole and finally reach
266 it.
267 @end{Reason}
268 @begin{Discussion}
269 It is anticipated that an Ada binding to IEC 559:1989 will be developed
270 in the future. As part of such a binding, the Machine_Overflows attribute
271 of a conformant floating point type will be specified to yield False, which
272 will permit both the predefined arithmetic operations and implementations of
273 the elementary functions to deliver signed infinities (and set the overflow
274 flag defined by the binding) instead of raising Constraint_Error in overflow
275 situations, when traps are disabled. Similarly, it is appropriate for the
276 elementary functions to deliver signed infinities (and set the zero-divide
277 flag defined by the binding) instead of raising Constraint_Error at poles,
278 when traps are disabled. Finally, such a binding should also specify the
279 behavior of the elementary functions, when sensible, given parameters with
280 infinite values.
281 @end{Discussion}
282
283 When one parameter of a function with multiple parameters represents a pole and
284 another is outside the function's domain, the latter takes precedence (i.e.,
285 Numerics.Argument_Error is raised).
286 @end{RunTime}
287
288 @begin{ImplReq}
289 In the implementation of Numerics.Generic_Elementary_Functions,
290 the range of intermediate values allowed during the calculation
291 of a final result shall not be affected by any
292 range constraint of the subtype Float_Type.
293 @begin{ImplNote}
294 Implementations of Numerics.Generic_Elementary_Functions written in Ada
295 should therefore avoid declaring local variables of subtype Float_Type; the
296 subtype Float_Type'Base should be used instead.
297 @end{ImplNote}
298
299 @Leading@Defn2{Term=[prescribed result],
300 Sec=[for the evaluation of an elementary function]}
301 In the following cases, evaluation of an elementary function shall yield the
302 @i{prescribed result},
303 provided that the preceding rules do not call for an exception to be
304 raised:
305 @begin{Itemize}
306 When the parameter X has the value zero, the Sqrt, Sin, Arcsin, Tan, Sinh,
307 Arcsinh, Tanh, and Arctanh functions yield a result of zero, and the Exp,
308 Cos, and Cosh functions yield a result of one.
309
310 When the parameter X has the value one, the Sqrt function yields a result
311 of one, and the Log, Arccos, and Arccosh functions yield a result of zero.
312
313 When the parameter Y has the value zero and the parameter X has a positive
314 value, the Arctan and Arccot functions yield a result of zero.
315
316 The results of the Sin, Cos, Tan, and Cot functions with specified cycle are
317 exact when the mathematical result is zero; those of the first two are also
318 exact when the mathematical result is @PorM 1.0.
319
320 Exponentiation by a zero exponent yields the value one. Exponentiation by
321 a unit exponent yields the value of the left operand. Exponentiation of
322 the value one yields the value one. Exponentiation of the value zero
323 yields the value zero.
324 @end{Itemize}
325
326 Other accuracy requirements for the elementary functions, which apply only in
327 implementations conforming to the Numerics Annex, and then only in the
328 @lquotes@;strict@rquotes@; mode defined there (see @RefSecNum{Numeric Performance Requirements}),
329 are given in @RefSecNum{Accuracy Requirements for the Elementary Functions}.
330
331 @Leading@;When Float_Type'Signed_Zeros is True, the sign of a zero result
332 shall be as follows:
333 @begin{itemize}
334 A prescribed zero result delivered
335 @i{at the origin} by one of the odd functions (Sin, Arcsin, Sinh,
336 Arcsinh, Tan, Arctan or Arccot as a function of Y when X is fixed and
337 positive, Tanh, and Arctanh) has the sign of the parameter X (Y, in the case
338 of Arctan or Arccot).
339
340 A prescribed zero result delivered by one of the odd functions @i{away from
341 the origin}, or by some other elementary function, has
342 an implementation-defined sign.
343 @ImplDef{The sign of a zero result from some of the operators or functions
344 in Numerics.Generic_Elementary_Functions, when Float_Type'Signed_Zeros is
345 True.}
346
347 @redundant[A zero result that is not a prescribed result
348 (i.e., one that results from
349 rounding or underflow) has the correct mathematical sign.]
350 @begin{Reason}
351 This is a consequence of the rules specified in IEC 559:1989 as they
352 apply to underflow situations with traps disabled.
353 @end{Reason}
354 @end{itemize}
355 @end{ImplReq}
356
357 @begin{ImplPerm}
358 The nongeneric equivalent packages may, but need not, be actual
359 instantiations of the generic package for the appropriate predefined type.
360 @end{ImplPerm}
361
362 @begin{DiffWord83}
363 @Leading@;The semantics of Numerics.Generic_Elementary_Functions differs from
364 Generic_Elementary_Functions as defined in ISO/IEC DIS 11430 (for Ada 83)
365 in the following ways:
366 @begin{itemize}
367 The generic package is a child unit of the package defining the
368 Argument_Error exception.
369
370 DIS 11430 specified names for the nongeneric equivalents, if provided.
371 Here, those nongeneric equivalents are required.
372
373 Implementations are not allowed to impose an optional restriction that the
374 generic actual parameter associated with Float_Type be unconstrained.
375 (In view of the ability to declare variables of subtype Float_Type'Base in
376 implementations of Numerics.Generic_Elementary_Functions, this flexibility
377 is no longer needed.)
378
379 The sign of a prescribed zero result at the origin of the odd functions is
380 specified, when Float_Type'Signed_Zeros is True. This conforms with
381 recommendations of Kahan and other numerical analysts.
382
383 The dependence of Arctan and Arccot on the sign of a parameter value of zero
384 is tied to the value of Float_Type'Signed_Zeros.
385
386 Sqrt is prescribed to yield a result of one when its parameter has the
387 value one. This guarantee makes it easier to achieve certain prescribed
388 results of the complex elementary functions
389 (see @RefSec{Complex Elementary Functions}).
390
391 Conformance to accuracy requirements is conditional.
392 @end{itemize}
393 @end{DiffWord83}
394
395 @begin{DiffWord95}
396 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0020],ARef=[AI95-00126-01]}
397 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Explicitly stated that the
398 nongeneric equivalents of Generic_Elementary_Functions are pure.]}
399 @end{DiffWord95}
400
401
402 @LabeledSubClause{Random Number Generation}
403
404 @begin{Intro}
405 @redundant[Facilities for the generation of pseudo-random floating point numbers are
406 provided in the package Numerics.Float_Random; the generic package
407 Numerics.Discrete_Random provides similar facilities for the generation of
408 pseudo-random integers and pseudo-random values of enumeration
409 types.
410 @Defn{random number}
411 For brevity, pseudo-random values of any of these types are called @i{random
412 numbers}.
413
414 Some of the facilities provided are basic to all applications of random
415 numbers. These include a limited private type each of whose objects serves as
416 the generator of a (possibly distinct) sequence of random numbers; a function
417 to obtain the @lquotes@;next@rquotes@; random number from a given sequence of random numbers
418 (that is, from its generator); and subprograms to initialize or reinitialize a
419 given generator to a time-dependent state or a state denoted by a single
420 integer.
421
422 Other facilities are provided specifically for advanced applications. These
423 include subprograms to save and restore the state of a given generator; a
424 private type whose objects can be used to hold the saved state of a
425 generator;
426 and subprograms to obtain a string representation of a given generator state,
427 or, given such a string representation, the corresponding state.]
428 @begin{Discussion}
429 These facilities support a variety of requirements ranging from repeatable
430 sequences (for debugging) to unique sequences in each execution of a program.
431 @end{Discussion}
432 @end{Intro}
433
434 @begin{StaticSem}
435 @Leading@;The library package Numerics.Float_Random has the following declaration:
436 @begin{Example}
437 @key[package] Ada.Numerics.Float_Random @key[is]@ChildUnit{Parent=[Ada.Numerics],Child=[Float_@!Random]}
438
439 -- @RI{Basic facilities}
440
441 @key[type] @AdaTypeDefn{Generator} @key[is] @key[limited] @key[private];
442
443
444 @key[subtype] @AdaSubtypeDefn{Name=[Uniformly_Distributed],Of=[Float]} @key[is] Float @key[range] 0.0 .. 1.0;
445 @key[function] @AdaSubDefn{Random} (Gen : Generator) @key[return] Uniformly_Distributed;
446
447
448 @key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator;
449 Initiator : @key[in] Integer);
450 @key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator);
451
452
453 -- @RI{Advanced facilities}
454
455 @key[type] @AdaTypeDefn{State} @key[is] @key[private];
456
457
458 @key[procedure] @AdaSubDefn{Save} (Gen : @key[in] Generator;
459 To_State : @key[out] State);
460 @key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator;
461 From_State : @key[in] State);
462
463
464 @AdaObjDefn{Max_Image_Width} : @key[constant] := @RI{implementation-defined integer value};
465
466
467 @key[function] @AdaSubDefn{Image} (Of_State : State) @key[return] String;
468 @key[function] @AdaSubDefn{Value} (Coded_State : String) @key[return] State;
469
470
471 @key[private]
472 ... -- @RI{not specified by the language}
473 @key[end] Ada.Numerics.Float_Random;
474 @end{Example}
475
476 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00360-01]}
477 @ChgAdded{Version=[2],Text=[The type Generator needs finalization
478 (see @RefSecNum{User-Defined Assignment and Finalization}).]}
479
480 The generic library package Numerics.Discrete_Random has the following
481 declaration:
482 @begin{Example}
483 @ChildUnit{Parent=[Ada.Numerics],Child=[Discrete_@!Random]}
484 @key[generic]
485 @key[type] Result_Subtype @key[is] (<>);
486 @key[package] Ada.Numerics.Discrete_Random @key[is]
487
488 -- @RI{Basic facilities}
489
490 @key[type] @AdaTypeDefn{Generator} @key[is] @key[limited] @key[private];
491
492
493 @key[function] @AdaSubDefn{Random} (Gen : Generator) @key[return] Result_Subtype;
494
495
496 @key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator;
497 Initiator : @key[in] Integer);
498 @key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator);
499
500
501 -- @RI{Advanced facilities}
502
503 @key[type] @AdaTypeDefn{State} @key[is] @key[private];
504
505
506 @key[procedure] @AdaSubDefn{Save} (Gen : @key[in] Generator;
507 To_State : @key[out] State);
508 @key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator;
509 From_State : @key[in] State);
510
511
512 @AdaObjDefn{Max_Image_Width} : @key[constant] := @RI{implementation-defined integer value};
513
514
515 @key[function] @AdaSubDefn{Image} (Of_State : State) @key[return] String;
516 @key[function] @AdaSubDefn{Value} (Coded_State : String) @key[return] State;
517
518
519 @key[private]
520 ... -- @RI{not specified by the language}
521 @key[end] Ada.Numerics.Discrete_Random;
522 @end{Example}
523 @ImplDef{The value of Numerics.Float_Random.Max_Image_Width.}
524 @ImplDef{The value of Numerics.Discrete_Random.Max_Image_Width.}
525 @begin{ImplNote}
526 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0097],ARef=[AI95-00115-01]}
527 @Leading@;
528 The following is a possible implementation of the private part of
529 @Chg{New=[Numerics.Float_Random], Old=[each package]} (assuming the presence
530 of @lquotes@;@key[with] Ada.Finalization;@rquotes@; as a context clause):
531 @begin{example}
532 @key[type] State @key[is] ...;
533 @key[type] Access_State @key[is] @key[access] State;
534 @key[type] Generator @key[is] @key[new] Finalization.Limited_Controlled @key[with]
535 @key[record]
536 S : Access_State := @key[new] State'(...);
537 @key[end] @key[record];
538 @key[procedure] Finalize (G : @key[in] @key[out] Generator);
539 @end{Example}
540
541 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0097],ARef=[AI95-00115-01]}
542 @ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00344-01]}
543 @ChgAdded{Version=[1],Text=[@Chg{Version=[2],New=[],
544 Old=[Unfortunately, ]}Numerics.Discrete_Random.Generator @Chg{Version=[2],New=[also can],Old=[cannot]} be
545 implemented this way@Chg{Version=[2],New=[],Old=[, as Numerics.Discrete_Random can be instantiated at any
546 nesting depth. However, Generator could have a component of a controlled type,
547 as long as that type is declared in some other (non-generic) package. One
548 possible solution would be to implement Numerics.@!Discrete_@!Random in terms
549 of Numerics.@!Float_@!Random, using a component of Numerics.@!Float_@!Random.Generator
550 to implement Numerics.@!Float_@!Random.@!Generator]}.]}
551
552 Clearly some level of indirection is required in the implementation of a
553 Generator, since the parameter mode is @key(in) for all operations on a
554 Generator. For this reason, Numerics.Float_Random and Numerics.Discrete_Random
555 cannot be declared pure.
556 @end{ImplNote}
557
558 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00360-01]}
559 @ChgAdded{Version=[2],Text=[The type Generator needs finalization
560 (see @RefSecNum{User-Defined Assignment and Finalization})
561 in every instantiation of Numerics.Discrete_Random.]}
562
563 An object of the limited private type Generator is associated with a sequence
564 of random numbers. Each generator has a hidden (internal) state, which the
565 operations on generators use to determine the position in the associated
566 sequence.
567 @PDefn{unspecified}
568 All generators are implicitly initialized to an unspecified state
569 that does not vary from one program execution to another;
570 they may also be explicitly initialized, or reinitialized, to a time-dependent
571 state, to a previously saved state, or to a state uniquely denoted by an
572 integer value.
573 @begin{Discussion}
574 The repeatability provided by the implicit initialization may be exploited for
575 testing or debugging purposes.
576 @end{Discussion}
577
578 An object of the private type State can be used to hold the internal state of a
579 generator. Such objects are only needed if the application is designed to
580 save and restore generator states or to examine or manufacture them.
581
582 @Trailing@;The operations on generators affect the state and therefore the future values
583 of the associated sequence. The semantics of the operations on generators and
584 states are defined below.
585 @begin{DescribeCode}
586 @begin{Example}
587 @key[function] Random (Gen : Generator) @key[return] Uniformly_Distributed;
588 @key[function] Random (Gen : Generator) @key[return] Result_Subtype;
589 @end{Example}
590 @Trailing@;Obtains the @lquotes@;next@rquotes@; random number from the given generator,
591 relative to its current state, according to an implementation-defined algorithm.
592 The result of the function in Numerics.Float_Random is delivered as a value of
593 the subtype Uniformly_Distributed, which is a subtype of the predefined type
594 Float having a range of
595 0.0 .. 1.0.
596 The result of the function in an
597 instantiation of Numerics.Discrete_Random is delivered as a value of the
598 generic formal subtype Result_Subtype.
599 @ChgImplDef{Version=[2],Kind=[Deleted],Text=[@ChgDeleted{Version=[2],Text=[The
600 algorithms for random number generation.]}]}
601 @begin{Discussion}
602 @ChgRef{Version=[2],Kind=[Added]}
603 @ChgAdded{Version=[2],Text=[The algorithm is the subject of a @DocReqName@;,
604 so we don't separately summarize this implementation-defined item.]}
605 @end{Discussion}
606 @begin{Reason}
607 The requirement for a level of indirection in accessing the internal state
608 of a generator arises from the desire to make Random a function, rather than
609 a procedure.
610 @end{Reason}
611
612 @begin{Example}
613 @key[procedure] Reset (Gen : @key[in] Generator;
614 Initiator : @key[in] Integer);
615 @key[procedure] Reset (Gen : @key[in] Generator);
616 @end{Example}
617 @Trailing@PDefn{unspecified}
618 Sets the state of the specified generator to one that is an unspecified
619 function of the value of the parameter Initiator (or to a time-dependent state,
620 if only a generator parameter is
621 specified).
622 @Defn2{Term=[Time-dependent Reset procedure],Sec=(of the random number generator)}
623 The latter form of the procedure is known as the @i{time-dependent Reset
624 procedure}.
625 @begin{ImplNote}
626 The time-dependent Reset procedure can be implemented by mapping the current
627 time and date as determined by the system clock into a state, but other
628 implementations are possible. For example, a white-noise generator or
629 a radioactive source can be used to generate time-dependent states.
630 @end{ImplNote}
631
632 @begin{Example}
633 @key[procedure] Save (Gen : @key[in] Generator;
634 To_State : @key[out] State);
635 @key[procedure] Reset (Gen : @key[in] Generator;
636 From_State : @key[in] State);
637 @end{Example}
638 @Trailing@;Save obtains the current state of a generator. Reset gives a
639 generator the specified state. A generator that is reset to a state previously
640 obtained by invoking Save is restored to the state it had when Save was
641 invoked.
642
643 @begin{Example}
644 @key[function] Image (Of_State : State) @key[return] String;
645 @key[function] Value (Coded_State : String) @key[return] State;
646 @end{Example}
647 Image provides a representation of a state coded (in an implementation-defined
648 way) as a string whose length is bounded by the value of Max_Image_Width.
649 Value is the inverse of Image: Value(Image(S)) = S for each state S that can be
650 obtained from a generator by invoking Save.
651 @ImplDef{The string representation of a random number generator's state.}
652 @end{DescribeCode}
653 @end{StaticSem}
654
655 @begin{RunTime}
656 @IndexCheck{Range_Check}
657 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
658 Instantiation of Numerics.Discrete_Random with a subtype having a null range
659 raises Constraint_Error.
660
661 @ChgRef{Version=[1],Kind=[Deleted],Ref=[8652/0050],ARef=[AI95-00089]}
662 @ChgDeleted{Version=[1],Text=[@IndexCheck{Range_Check}
663 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
664 Invoking Value with a string that is not the image of any generator
665 state raises Constraint_Error.]}
666 @end{RunTime}
667
668 @begin{Bounded}
669 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0050],ARef=[AI95-00089]}
670 @ChgAdded{Version=[1],Text=[It is a bounded error to invoke Value with a
671 string that is not the image of any generator state.
672 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
673 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
674 If the error is detected, Constraint_Error or
675 Program_Error is raised. Otherwise, a call to Reset with the resulting state
676 will produce a generator such that calls to Random with this generator will
677 produce a sequence of values of the appropriate subtype, but which might not
678 be random in character. That is, the sequence of values might not fulfill the
679 implementation requirements of this subclause.]}
680 @end{Bounded}
681
682 @begin{ImplReq}
683 A sufficiently long sequence of random numbers obtained by successive calls to
684 Random is approximately uniformly distributed over the range of the result
685 subtype.
686
687 The Random function in an instantiation of Numerics.Discrete_Random is
688 guaranteed to yield each value in its result subtype in a finite number of
689 calls, provided that the number of such values does not exceed
690 2 @+[15].
691
692 Other performance requirements for the random number generator, which apply
693 only in implementations conforming to the Numerics Annex, and then only in the
694 @lquotes@;strict@rquotes@; mode defined there (see @RefSecNum{Numeric Performance Requirements}),
695 are given in @RefSecNum{Performance Requirements for Random Number Generation}.
696 @end{ImplReq}
697
698 @begin{DocReq}
699 No one algorithm for random number generation is best for all applications. To
700 enable the user to determine the suitability of the random number generators
701 for the intended application, the implementation shall describe the algorithm
702 used and shall give its period, if known exactly, or a lower bound on the
703 period, if the exact period is unknown. Periods that are so long that the
704 periodicity is unobservable in practice can be described in such terms, without
705 giving a numerical bound.
706 @ChgDocReq{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],Text=[The
707 algorithm used for random number generation, including a description of its
708 period.]}]}
709
710 The implementation also shall document the minimum time interval between calls
711 to the time-dependent Reset procedure that are guaranteed to initiate
712 different sequences, and it shall document the nature of the strings that
713 Value will accept without raising Constraint_Error.
714 @ChgImplDef{Version=[2],Kind=[Deleted],Text=[@ChgDeleted{Version=[2],Text=[The
715 minimum time interval between
716 calls to the time-dependent Reset procedure that are guaranteed to initiate
717 different random number sequences.]}]}
718 @ChgDocReq{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],Text=[The
719 minimum time interval between
720 calls to the time-dependent Reset procedure that is guaranteed to initiate
721 different random number sequences.]}]}
722 @end{DocReq}
723
724 @begin{ImplAdvice}
725 Any storage associated with an object of type Generator should be
726 reclaimed on exit from the scope of the object.
727 @ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],Text=[Any
728 storage associated with an object of type Generator of the random
729 number packages should be reclaimed on exit from the scope of the object.]}]}
730 @begin{Ramification}
731 A level of indirection is implicit in the semantics of the
732 operations, given that they all take parameters of mode @key(in).
733 This implies that the full type of Generator probably should be a controlled
734 type, with appropriate finalization to reclaim any heap-allocated storage.
735 @end{Ramification}
736
737 If the generator period is sufficiently long in relation to the number of
738 distinct initiator values, then each possible value of Initiator passed to
739 Reset should initiate a sequence of random numbers that does not, in a
740 practical sense, overlap the sequence initiated by any other value. If this
741 is not possible, then the mapping between initiator values and generator states
742 should be a rapidly varying function of the initiator value.
743 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],Text=[Each
744 value of Initiator passed to Reset for the random number packages should
745 initiate a distinct sequence of random numbers, or, if that is not possible,
746 be at least a rapidly varying function of the initiator value.]}]}
747 @end{ImplAdvice}
748
749 @begin{Notes}
750 If two or more tasks are to share the same generator, then the tasks have to
751 synchronize their access to the generator as for any shared variable
752 (see @RefSecNum(Shared Variables)).
753
754 Within a given implementation, a repeatable random number sequence can be
755 obtained by relying on the implicit initialization of generators or by
756 explicitly initializing a generator with a repeatable initiator value.
757 Different sequences of random numbers can be obtained from a given generator in
758 different program executions by explicitly initializing the generator to a
759 time-dependent state.
760
761 A given implementation of the Random function in Numerics.Float_Random may or
762 may not be capable of delivering the values 0.0 or 1.0. Portable applications
763 should assume that these values, or values sufficiently close to them to behave
764 indistinguishably from them, can occur. If a sequence of random integers from
765 some fixed range is needed, the application should use the Random function in
766 an appropriate instantiation of Numerics.Discrete_Random, rather than
767 transforming the result of the Random function in Numerics.Float_Random.
768 However, some applications with unusual requirements, such as for a sequence of
769 random integers each drawn from a different range, will find it more convenient
770 to transform the result of the floating point Random function. For
771 @R[M] @geq 1, the expression
772 @begin{Example}
773 Integer(Float(M) * Random(G)) mod M
774 @end{Example}
775
776 @NoPrefix@;transforms the result of Random(G) to an integer uniformly distributed over the
777 range 0 .. @R[M]@en@;1; it is valid even if Random delivers 0.0 or 1.0.
778 Each value of the result range is possible, provided that M is not too large.
779 Exponentially distributed (floating point) random numbers with mean and
780 standard deviation 1.0 can be obtained by the transformation
781 @begin{Example}
782 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00434-01]}
783 -Log(Random(G) + Float'Model_Small)@Chg{Version=[2],New=[],Old=[)]}
784 @end{Example}
785
786 @NoPrefix@;where Log comes from Numerics.Elementary_Functions
787 (see @RefSecNum{Elementary Functions});
788 in this expression, the addition of Float'Model_Small avoids the
789 exception that would be raised were Log to be given the value zero, without
790 affecting the result (in most implementations) when Random returns a nonzero
791 value.
792 @end{Notes}
793
794 @begin{Examples}
795 @Leading@Keepnext@i{Example of a program that plays a simulated dice game:}
796 @begin{Example}@Trailing
797 @key[with] Ada.Numerics.Discrete_Random;
798 @key[procedure] Dice_Game @key[is]@Softpage
799 @key[subtype] Die @key[is] Integer @key[range] 1 .. 6;
800 @key[subtype] Dice @key[is] Integer @key[range] 2*Die'First .. 2*Die'Last;
801 @key[package] Random_Die @key[is] @key[new] Ada.Numerics.Discrete_Random (Die);
802 @key[use] Random_Die;
803 G : Generator;
804 D : Dice;@Softpage
805 @key[begin]@Softpage
806 Reset (G); -- @RI{Start the generator in a unique state in each run}
807 @key[loop]
808 -- @RI{Roll a pair of dice; sum and process the results}
809 D := Random(G) + Random(G);
810 ...
811 @key[end] @key[loop];@Softpage
812 @key[end] Dice_Game;
813 @end{Example}
814
815 @Leading@Keepnext@i{Example of a program that simulates coin tosses:}
816 @begin{Example}@Trailing
817 @key[with] Ada.Numerics.Discrete_Random;
818 @key[procedure] Flip_A_Coin @key[is]@Softpage
819 @key[type] Coin @key[is] (Heads, Tails);
820 @key[package] Random_Coin @key[is] @key[new] Ada.Numerics.Discrete_Random (Coin);
821 @key[use] Random_Coin;
822 G : Generator;@Softpage
823 @key[begin]@Softpage
824 Reset (G); -- @RI{Start the generator in a unique state in each run}
825 @key[loop]
826 -- @RI{Toss a coin and process the result}
827 @key[case] Random(G) @key[is]
828 @key[when] Heads =>
829 ...
830 @key[when] Tails =>
831 ...
832 @key[end] @key[case];
833 ...
834 @key[end] @key[loop];@Softpage
835 @key[end] Flip_A_Coin;
836 @end{Example}
837
838 @Leading@Keepnext@i{Example of a parallel simulation of a physical system,
839 with a separate generator of event probabilities in each task:}
840 @begin{Example}
841 @key[with] Ada.Numerics.Float_Random;
842 @key[procedure] Parallel_Simulation @key[is]@Softpage
843 @key[use] Ada.Numerics.Float_Random;
844 @key[task] @key[type] Worker @key[is]
845 @key[entry] Initialize_Generator (Initiator : @key[in] Integer);
846 ...
847 @key[end] Worker;
848 W : @key[array] (1 .. 10) @key[of] Worker;@Softpage
849 @key[task] @key[body] Worker @key[is]
850 G : Generator;
851 Probability_Of_Event : Uniformly_Distributed;
852 @key[begin]@Softpage
853 @key[accept] Initialize_Generator (Initiator : @key[in] Integer) @key[do]
854 Reset (G, Initiator);
855 @key[end] Initialize_Generator;
856 @key[loop]
857 ...
858 Probability_Of_Event := Random(G);
859 ...
860 @key[end] @key[loop];
861 @key[end] Worker;@Softpage
862 @key[begin]@Softpage
863 -- @RI{Initialize the generators in the Worker tasks to different states}
864 @key[for] I @key[in] W'Range @key[loop]
865 W(I).Initialize_Generator (I);
866 @key[end] @key[loop];
867 ... -- @RI{Wait for the Worker tasks to terminate}@Softpage
868 @key[end] Parallel_Simulation;
869 @end{Example}
870 @end{Examples}
871
872 @begin{Notes}
873 @i{Notes on the last example:}
874 Although each Worker task initializes its generator to a different state, those
875 states will be the same in every execution of the program. The generator
876 states can be initialized uniquely in each program execution by instantiating
877 Ada.Numerics.Discrete_Random for the type Integer in the main procedure,
878 resetting the generator obtained from that instance to a time-dependent state,
879 and then using random integers obtained from that generator to initialize the
880 generators in each Worker task.
881 @end{Notes}
882
883 @begin{Incompatible95}
884 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00360-01]}
885 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
886 @b[Amendment Correction:] Type Generator in Numerics.Float_Random and in an
887 instance of Numerics.Discrete_Random is defined to need finalization. If the
888 restriction No_Nested_Finalization (see @RefSecNum{Tasking Restrictions})
889 applies to the partition, and Generator does not have a controlled part, it
890 will not be allowed in local objects in Ada 2005 whereas it would be allowed
891 in original Ada 95. Such code is not portable, as another Ada compiler may
892 have a controlled part in Generator, and thus would be illegal.]}
893 @end{Incompatible95}
894
895 @begin{DiffWord95}
896 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0050],ARef=[AI95-00089-01]}
897 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Made the passing of an incorrect
898 Image of a generator a bounded error, as it may not be practical to check
899 for problems (if a generator consists of several related values).]}
900 @end{DiffWord95}