| 1 |
@comment{ $Source: e:\\cvsroot/ARM/Source/pre_math.mss,v $ } |
| 2 |
@comment{ $Revision: 1.43 $ $Date: 2012/02/19 01:58:37 $ $Author: randy $ } |
| 3 |
@Part(predefmath, Root="ada.mss") |
| 4 |
|
| 5 |
@Comment{$Date: 2012/02/19 01:58:37 $} |
| 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 |
| 478 |
needs finalization@PDefn2{Term=<needs finalization>,Sec=<language-defined type>} |
| 479 |
(see @RefSecNum{Assignment and Finalization}).]} |
| 480 |
|
| 481 |
The generic library package Numerics.Discrete_Random has the following |
| 482 |
declaration: |
| 483 |
@begin{Example} |
| 484 |
@ChildUnit{Parent=[Ada.Numerics],Child=[Discrete_@!Random]} |
| 485 |
@key[generic] |
| 486 |
@key[type] Result_Subtype @key[is] (<>); |
| 487 |
@key[package] Ada.Numerics.Discrete_Random @key[is] |
| 488 |
|
| 489 |
-- @RI{Basic facilities} |
| 490 |
|
| 491 |
@key[type] @AdaTypeDefn{Generator} @key[is] @key[limited] @key[private]; |
| 492 |
|
| 493 |
|
| 494 |
@key[function] @AdaSubDefn{Random} (Gen : Generator) @key[return] Result_Subtype; |
| 495 |
|
| 496 |
|
| 497 |
@key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator; |
| 498 |
Initiator : @key[in] Integer); |
| 499 |
@key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator); |
| 500 |
|
| 501 |
|
| 502 |
-- @RI{Advanced facilities} |
| 503 |
|
| 504 |
@key[type] @AdaTypeDefn{State} @key[is] @key[private]; |
| 505 |
|
| 506 |
|
| 507 |
@key[procedure] @AdaSubDefn{Save} (Gen : @key[in] Generator; |
| 508 |
To_State : @key[out] State); |
| 509 |
@key[procedure] @AdaSubDefn{Reset} (Gen : @key[in] Generator; |
| 510 |
From_State : @key[in] State); |
| 511 |
|
| 512 |
|
| 513 |
@AdaObjDefn{Max_Image_Width} : @key[constant] := @RI{implementation-defined integer value}; |
| 514 |
|
| 515 |
|
| 516 |
@key[function] @AdaSubDefn{Image} (Of_State : State) @key[return] String; |
| 517 |
@key[function] @AdaSubDefn{Value} (Coded_State : String) @key[return] State; |
| 518 |
|
| 519 |
|
| 520 |
@key[private] |
| 521 |
... -- @RI{not specified by the language} |
| 522 |
@key[end] Ada.Numerics.Discrete_Random; |
| 523 |
@end{Example} |
| 524 |
@ImplDef{The value of Numerics.Float_Random.Max_Image_Width.} |
| 525 |
@ImplDef{The value of Numerics.Discrete_Random.Max_Image_Width.} |
| 526 |
@begin{ImplNote} |
| 527 |
@ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0097],ARef=[AI95-00115-01]} |
| 528 |
@Leading@; |
| 529 |
The following is a possible implementation of the private part of |
| 530 |
@Chg{New=[Numerics.Float_Random], Old=[each package]} (assuming the presence |
| 531 |
of @lquotes@;@key[with] Ada.Finalization;@rquotes@; as a context clause): |
| 532 |
@begin{example} |
| 533 |
@key[type] State @key[is] ...; |
| 534 |
@key[type] Access_State @key[is] @key[access] State; |
| 535 |
@key[type] Generator @key[is] @key[new] Finalization.Limited_Controlled @key[with] |
| 536 |
@key[record] |
| 537 |
S : Access_State := @key[new] State'(...); |
| 538 |
@key[end] @key[record]; |
| 539 |
@key[procedure] Finalize (G : @key[in] @key[out] Generator); |
| 540 |
@end{Example} |
| 541 |
|
| 542 |
@ChgRef{Version=[1],Kind=[Added],Ref=[8652/0097],ARef=[AI95-00115-01]} |
| 543 |
@ChgRef{Version=[2],Kind=[RevisedAdded],ARef=[AI95-00344-01]} |
| 544 |
@ChgAdded{Version=[1],Text=[@Chg{Version=[2],New=[], |
| 545 |
Old=[Unfortunately, ]}Numerics.Discrete_Random.Generator @Chg{Version=[2],New=[also can],Old=[cannot]} be |
| 546 |
implemented this way@Chg{Version=[2],New=[],Old=[, as Numerics.Discrete_Random can be instantiated at any |
| 547 |
nesting depth. However, Generator could have a component of a controlled type, |
| 548 |
as long as that type is declared in some other (nongeneric) package. One |
| 549 |
possible solution would be to implement Numerics.@!Discrete_@!Random in terms |
| 550 |
of Numerics.@!Float_@!Random, using a component of Numerics.@!Float_@!Random.Generator |
| 551 |
to implement Numerics.@!Float_@!Random.@!Generator]}.]} |
| 552 |
|
| 553 |
Clearly some level of indirection is required in the implementation of a |
| 554 |
Generator, since the parameter mode is @key(in) for all operations on a |
| 555 |
Generator. For this reason, Numerics.Float_Random and Numerics.Discrete_Random |
| 556 |
cannot be declared pure. |
| 557 |
@end{ImplNote} |
| 558 |
|
| 559 |
@ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00360-01]} |
| 560 |
@ChgAdded{Version=[2],Text=[The type Generator |
| 561 |
needs finalization@PDefn2{Term=<needs finalization>,Sec=<language-defined type>} |
| 562 |
(see @RefSecNum{Assignment and Finalization}) |
| 563 |
in every instantiation of Numerics.Discrete_Random.]} |
| 564 |
|
| 565 |
An object of the limited private type Generator is associated with a sequence |
| 566 |
of random numbers. Each generator has a hidden (internal) state, which the |
| 567 |
operations on generators use to determine the position in the associated |
| 568 |
sequence. |
| 569 |
@PDefn{unspecified} |
| 570 |
All generators are implicitly initialized to an unspecified state |
| 571 |
that does not vary from one program execution to another; |
| 572 |
they may also be explicitly initialized, or reinitialized, to a time-dependent |
| 573 |
state, to a previously saved state, or to a state uniquely denoted by an |
| 574 |
integer value. |
| 575 |
@begin{Discussion} |
| 576 |
The repeatability provided by the implicit initialization may be exploited for |
| 577 |
testing or debugging purposes. |
| 578 |
@end{Discussion} |
| 579 |
|
| 580 |
@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0280-1]} |
| 581 |
An object of the private type State can be used to hold the internal state of a |
| 582 |
generator. Such objects are only needed if the application is designed to |
| 583 |
save and restore generator states or to examine or manufacture |
| 584 |
them.@Chg{Version=[3],New=[ The implicit initial value of type State |
| 585 |
corresponds to the implicit initial value of all generators.],Old=[]} |
| 586 |
|
| 587 |
@begin{Discussion} |
| 588 |
@ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0280-1]} |
| 589 |
@ChgAdded{Version=[3],Text=[All generators are implicitly initialized to |
| 590 |
the same unchanging value, and using Reset on a default initialized object |
| 591 |
of type State will produce a generator with that same value.]} |
| 592 |
@end{Discussion} |
| 593 |
|
| 594 |
@Trailing@;The operations on generators affect the state and therefore the future values |
| 595 |
of the associated sequence. The semantics of the operations on generators and |
| 596 |
states are defined below. |
| 597 |
@begin{DescribeCode} |
| 598 |
@begin{Example} |
| 599 |
@key[function] Random (Gen : Generator) @key[return] Uniformly_Distributed; |
| 600 |
@key[function] Random (Gen : Generator) @key[return] Result_Subtype; |
| 601 |
@end{Example} |
| 602 |
@Trailing@;Obtains the @lquotes@;next@rquotes@; random number from the given generator, |
| 603 |
relative to its current state, according to an implementation-defined algorithm. |
| 604 |
The result of the function in Numerics.Float_Random is delivered as a value of |
| 605 |
the subtype Uniformly_Distributed, which is a subtype of the predefined type |
| 606 |
Float having a range of |
| 607 |
0.0 .. 1.0. |
| 608 |
The result of the function in an |
| 609 |
instantiation of Numerics.Discrete_Random is delivered as a value of the |
| 610 |
generic formal subtype Result_Subtype. |
| 611 |
@ChgImplDef{Version=[2],Kind=[Deleted],InitialVersion=[0], |
| 612 |
Text=[@ChgDeleted{Version=[2],Text=[The |
| 613 |
algorithms for random number generation.]}]} |
| 614 |
@begin{Discussion} |
| 615 |
@ChgRef{Version=[2],Kind=[Added]} |
| 616 |
@ChgAdded{Version=[2],Text=[The algorithm is the subject of a @DocReqName@;, |
| 617 |
so we don't separately summarize this implementation-defined item.]} |
| 618 |
@end{Discussion} |
| 619 |
@begin{Reason} |
| 620 |
The requirement for a level of indirection in accessing the internal state |
| 621 |
of a generator arises from the desire to make Random a function, rather than |
| 622 |
a procedure. |
| 623 |
@end{Reason} |
| 624 |
|
| 625 |
@begin{Example} |
| 626 |
@key[procedure] Reset (Gen : @key[in] Generator; |
| 627 |
Initiator : @key[in] Integer); |
| 628 |
@key[procedure] Reset (Gen : @key[in] Generator); |
| 629 |
@end{Example} |
| 630 |
@Trailing@PDefn{unspecified} |
| 631 |
Sets the state of the specified generator to one that is an unspecified |
| 632 |
function of the value of the parameter Initiator (or to a time-dependent state, |
| 633 |
if only a generator parameter is |
| 634 |
specified). |
| 635 |
@Defn2{Term=[Time-dependent Reset procedure],Sec=(of the random number generator)} |
| 636 |
The latter form of the procedure is known as the @i{time-dependent Reset |
| 637 |
procedure}. |
| 638 |
@begin{ImplNote} |
| 639 |
The time-dependent Reset procedure can be implemented by mapping the current |
| 640 |
time and date as determined by the system clock into a state, but other |
| 641 |
implementations are possible. For example, a white-noise generator or |
| 642 |
a radioactive source can be used to generate time-dependent states. |
| 643 |
@end{ImplNote} |
| 644 |
|
| 645 |
@begin{Example} |
| 646 |
@key[procedure] Save (Gen : @key[in] Generator; |
| 647 |
To_State : @key[out] State); |
| 648 |
@key[procedure] Reset (Gen : @key[in] Generator; |
| 649 |
From_State : @key[in] State); |
| 650 |
@end{Example} |
| 651 |
@Trailing@;Save obtains the current state of a generator. Reset gives a |
| 652 |
generator the specified state. A generator that is reset to a state previously |
| 653 |
obtained by invoking Save is restored to the state it had when Save was |
| 654 |
invoked. |
| 655 |
|
| 656 |
@begin{Example} |
| 657 |
@key[function] Image (Of_State : State) @key[return] String; |
| 658 |
@key[function] Value (Coded_State : String) @key[return] State; |
| 659 |
@end{Example} |
| 660 |
Image provides a representation of a state coded (in an implementation-defined |
| 661 |
way) as a string whose length is bounded by the value of Max_Image_Width. |
| 662 |
Value is the inverse of Image: Value(Image(S)) = S for each state S that can be |
| 663 |
obtained from a generator by invoking Save. |
| 664 |
@ImplDef{The string representation of a random number generator's state.} |
| 665 |
@end{DescribeCode} |
| 666 |
@end{StaticSem} |
| 667 |
|
| 668 |
@begin{RunTime} |
| 669 |
@IndexCheck{Range_Check} |
| 670 |
@Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)} |
| 671 |
Instantiation of Numerics.Discrete_Random with a subtype having a null range |
| 672 |
raises Constraint_Error. |
| 673 |
|
| 674 |
@ChgRef{Version=[1],Kind=[Deleted],Ref=[8652/0050],ARef=[AI95-00089]} |
| 675 |
@ChgDeleted{Version=[1],Text=[@IndexCheck{Range_Check} |
| 676 |
@Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)} |
| 677 |
Invoking Value with a string that is not the image of any generator |
| 678 |
state raises Constraint_Error.]} |
| 679 |
@end{RunTime} |
| 680 |
|
| 681 |
@begin{Bounded} |
| 682 |
@ChgRef{Version=[1],Kind=[Added],Ref=[8652/0050],ARef=[AI95-00089]} |
| 683 |
@ChgAdded{Version=[1],Text=[It is a bounded error to invoke Value with a |
| 684 |
string that is not the image of any generator state. |
| 685 |
@Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)} |
| 686 |
@Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)} |
| 687 |
If the error is detected, Constraint_Error or |
| 688 |
Program_Error is raised. Otherwise, a call to Reset with the resulting state |
| 689 |
will produce a generator such that calls to Random with this generator will |
| 690 |
produce a sequence of values of the appropriate subtype, but which might not |
| 691 |
be random in character. That is, the sequence of values might not fulfill the |
| 692 |
implementation requirements of this subclause.]} |
| 693 |
@end{Bounded} |
| 694 |
|
| 695 |
@begin{ImplReq} |
| 696 |
A sufficiently long sequence of random numbers obtained by successive calls to |
| 697 |
Random is approximately uniformly distributed over the range of the result |
| 698 |
subtype. |
| 699 |
|
| 700 |
The Random function in an instantiation of Numerics.Discrete_Random is |
| 701 |
guaranteed to yield each value in its result subtype in a finite number of |
| 702 |
calls, provided that the number of such values does not exceed |
| 703 |
2 @+[15]. |
| 704 |
|
| 705 |
Other performance requirements for the random number generator, which apply |
| 706 |
only in implementations conforming to the Numerics Annex, and then only in the |
| 707 |
@lquotes@;strict@rquotes@; mode defined there (see @RefSecNum{Numeric Performance Requirements}), |
| 708 |
are given in @RefSecNum{Performance Requirements for Random Number Generation}. |
| 709 |
@end{ImplReq} |
| 710 |
|
| 711 |
@begin{DocReq} |
| 712 |
No one algorithm for random number generation is best for all applications. To |
| 713 |
enable the user to determine the suitability of the random number generators |
| 714 |
for the intended application, the implementation shall describe the algorithm |
| 715 |
used and shall give its period, if known exactly, or a lower bound on the |
| 716 |
period, if the exact period is unknown. Periods that are so long that the |
| 717 |
periodicity is unobservable in practice can be described in such terms, without |
| 718 |
giving a numerical bound. |
| 719 |
@ChgDocReq{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],Text=[The |
| 720 |
algorithm used for random number generation, including a description of its |
| 721 |
period.]}]} |
| 722 |
|
| 723 |
The implementation also shall document the minimum time interval between calls |
| 724 |
to the time-dependent Reset procedure that are guaranteed to initiate |
| 725 |
different sequences, and it shall document the nature of the strings that |
| 726 |
Value will accept without raising Constraint_Error. |
| 727 |
@ChgImplDef{Version=[2],Kind=[Deleted],InitialVersion=[0], |
| 728 |
Text=[@ChgDeleted{Version=[2],Text=[The |
| 729 |
minimum time interval between |
| 730 |
calls to the time-dependent Reset procedure that are guaranteed to initiate |
| 731 |
different random number sequences.]}]} |
| 732 |
@ChgDocReq{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],Text=[The |
| 733 |
minimum time interval between |
| 734 |
calls to the time-dependent Reset procedure that is guaranteed to initiate |
| 735 |
different random number sequences.]}]} |
| 736 |
@end{DocReq} |
| 737 |
|
| 738 |
@begin{ImplAdvice} |
| 739 |
Any storage associated with an object of type Generator should be |
| 740 |
reclaimed on exit from the scope of the object. |
| 741 |
@ChgImplAdvice{Version=[2],Kind=[Added],Text=[@ChgAdded{Version=[2],Text=[Any |
| 742 |
storage associated with an object of type Generator of the random |
| 743 |
number packages should be reclaimed on exit from the scope of the object.]}]} |
| 744 |
@begin{Ramification} |
| 745 |
A level of indirection is implicit in the semantics of the |
| 746 |
operations, given that they all take parameters of mode @key(in). |
| 747 |
This implies that the full type of Generator probably should be a controlled |
| 748 |
type, with appropriate finalization to reclaim any heap-allocated storage. |
| 749 |
@end{Ramification} |
| 750 |
|
| 751 |
If the generator period is sufficiently long in relation to the number of |
| 752 |
distinct initiator values, then each possible value of Initiator passed to |
| 753 |
Reset should initiate a sequence of random numbers that does not, in a |
| 754 |
practical sense, overlap the sequence initiated by any other value. If this |
| 755 |
is not possible, then the mapping between initiator values and generator states |
| 756 |
should be a rapidly varying function of the initiator value. |
| 757 |
@ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],Text=[Each |
| 758 |
value of Initiator passed to Reset for the random number packages should |
| 759 |
initiate a distinct sequence of random numbers, or, if that is not possible, |
| 760 |
be at least a rapidly varying function of the initiator value.]}]} |
| 761 |
@end{ImplAdvice} |
| 762 |
|
| 763 |
@begin{Notes} |
| 764 |
If two or more tasks are to share the same generator, then the tasks have to |
| 765 |
synchronize their access to the generator as for any shared variable |
| 766 |
(see @RefSecNum(Shared Variables)). |
| 767 |
|
| 768 |
Within a given implementation, a repeatable random number sequence can be |
| 769 |
obtained by relying on the implicit initialization of generators or by |
| 770 |
explicitly initializing a generator with a repeatable initiator value. |
| 771 |
Different sequences of random numbers can be obtained from a given generator in |
| 772 |
different program executions by explicitly initializing the generator to a |
| 773 |
time-dependent state. |
| 774 |
|
| 775 |
A given implementation of the Random function in Numerics.Float_Random may or |
| 776 |
may not be capable of delivering the values 0.0 or 1.0. Portable applications |
| 777 |
should assume that these values, or values sufficiently close to them to behave |
| 778 |
indistinguishably from them, can occur. If a sequence of random integers from |
| 779 |
some fixed range is needed, the application should use the Random function in |
| 780 |
an appropriate instantiation of Numerics.Discrete_Random, rather than |
| 781 |
transforming the result of the Random function in Numerics.Float_Random. |
| 782 |
However, some applications with unusual requirements, such as for a sequence of |
| 783 |
random integers each drawn from a different range, will find it more convenient |
| 784 |
to transform the result of the floating point Random function. For |
| 785 |
@R[M] @geq 1, the expression |
| 786 |
@begin{Example} |
| 787 |
Integer(Float(M) * Random(G)) mod M |
| 788 |
@end{Example} |
| 789 |
|
| 790 |
@NoPrefix@;transforms the result of Random(G) to an integer uniformly distributed over the |
| 791 |
range 0 .. @R[M]@en@;1; it is valid even if Random delivers 0.0 or 1.0. |
| 792 |
Each value of the result range is possible, provided that M is not too large. |
| 793 |
Exponentially distributed (floating point) random numbers with mean and |
| 794 |
standard deviation 1.0 can be obtained by the transformation |
| 795 |
@begin{Example} |
| 796 |
@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00434-01]} |
| 797 |
-Log(Random(G) + Float'Model_Small)@Chg{Version=[2],New=[],Old=[)]} |
| 798 |
@end{Example} |
| 799 |
|
| 800 |
@NoPrefix@;where Log comes from Numerics.Elementary_Functions |
| 801 |
(see @RefSecNum{Elementary Functions}); |
| 802 |
in this expression, the addition of Float'Model_Small avoids the |
| 803 |
exception that would be raised were Log to be given the value zero, without |
| 804 |
affecting the result (in most implementations) when Random returns a nonzero |
| 805 |
value. |
| 806 |
@end{Notes} |
| 807 |
|
| 808 |
@begin{Examples} |
| 809 |
@Leading@Keepnext@i{Example of a program that plays a simulated dice game:} |
| 810 |
@begin{Example}@Trailing |
| 811 |
@key[with] Ada.Numerics.Discrete_Random; |
| 812 |
@key[procedure] Dice_Game @key[is]@Softpage |
| 813 |
@key[subtype] Die @key[is] Integer @key[range] 1 .. 6; |
| 814 |
@key[subtype] Dice @key[is] Integer @key[range] 2*Die'First .. 2*Die'Last; |
| 815 |
@key[package] Random_Die @key[is] @key[new] Ada.Numerics.Discrete_Random (Die); |
| 816 |
@key[use] Random_Die; |
| 817 |
G : Generator; |
| 818 |
D : Dice;@Softpage |
| 819 |
@key[begin]@Softpage |
| 820 |
Reset (G); -- @RI{Start the generator in a unique state in each run} |
| 821 |
@key[loop] |
| 822 |
-- @RI{Roll a pair of dice; sum and process the results} |
| 823 |
D := Random(G) + Random(G); |
| 824 |
... |
| 825 |
@key[end] @key[loop];@Softpage |
| 826 |
@key[end] Dice_Game; |
| 827 |
@end{Example} |
| 828 |
|
| 829 |
@Leading@Keepnext@i{Example of a program that simulates coin tosses:} |
| 830 |
@begin{Example}@Trailing |
| 831 |
@key[with] Ada.Numerics.Discrete_Random; |
| 832 |
@key[procedure] Flip_A_Coin @key[is]@Softpage |
| 833 |
@key[type] Coin @key[is] (Heads, Tails); |
| 834 |
@key[package] Random_Coin @key[is] @key[new] Ada.Numerics.Discrete_Random (Coin); |
| 835 |
@key[use] Random_Coin; |
| 836 |
G : Generator;@Softpage |
| 837 |
@key[begin]@Softpage |
| 838 |
Reset (G); -- @RI{Start the generator in a unique state in each run} |
| 839 |
@key[loop] |
| 840 |
-- @RI{Toss a coin and process the result} |
| 841 |
@key[case] Random(G) @key[is] |
| 842 |
@key[when] Heads => |
| 843 |
... |
| 844 |
@key[when] Tails => |
| 845 |
... |
| 846 |
@key[end] @key[case]; |
| 847 |
... |
| 848 |
@key[end] @key[loop];@Softpage |
| 849 |
@key[end] Flip_A_Coin; |
| 850 |
@end{Example} |
| 851 |
|
| 852 |
@Leading@Keepnext@i{Example of a parallel simulation of a physical system, |
| 853 |
with a separate generator of event probabilities in each task:} |
| 854 |
@begin{Example} |
| 855 |
@key[with] Ada.Numerics.Float_Random; |
| 856 |
@key[procedure] Parallel_Simulation @key[is]@Softpage |
| 857 |
@key[use] Ada.Numerics.Float_Random; |
| 858 |
@key[task] @key[type] Worker @key[is] |
| 859 |
@key[entry] Initialize_Generator (Initiator : @key[in] Integer); |
| 860 |
... |
| 861 |
@key[end] Worker; |
| 862 |
W : @key[array] (1 .. 10) @key[of] Worker;@Softpage |
| 863 |
@key[task] @key[body] Worker @key[is] |
| 864 |
G : Generator; |
| 865 |
Probability_Of_Event : Uniformly_Distributed; |
| 866 |
@key[begin]@Softpage |
| 867 |
@key[accept] Initialize_Generator (Initiator : @key[in] Integer) @key[do] |
| 868 |
Reset (G, Initiator); |
| 869 |
@key[end] Initialize_Generator; |
| 870 |
@key[loop] |
| 871 |
... |
| 872 |
Probability_Of_Event := Random(G); |
| 873 |
... |
| 874 |
@key[end] @key[loop]; |
| 875 |
@key[end] Worker;@Softpage |
| 876 |
@key[begin]@Softpage |
| 877 |
-- @RI{Initialize the generators in the Worker tasks to different states} |
| 878 |
@key[for] I @key[in] W'Range @key[loop] |
| 879 |
W(I).Initialize_Generator (I); |
| 880 |
@key[end] @key[loop]; |
| 881 |
... -- @RI{Wait for the Worker tasks to terminate}@Softpage |
| 882 |
@key[end] Parallel_Simulation; |
| 883 |
@end{Example} |
| 884 |
@end{Examples} |
| 885 |
|
| 886 |
@begin{Notes} |
| 887 |
@i{Notes on the last example:} |
| 888 |
Although each Worker task initializes its generator to a different state, those |
| 889 |
states will be the same in every execution of the program. The generator |
| 890 |
states can be initialized uniquely in each program execution by instantiating |
| 891 |
Ada.Numerics.Discrete_Random for the type Integer in the main procedure, |
| 892 |
resetting the generator obtained from that instance to a time-dependent state, |
| 893 |
and then using random integers obtained from that generator to initialize the |
| 894 |
generators in each Worker task. |
| 895 |
@end{Notes} |
| 896 |
|
| 897 |
@begin{Incompatible95} |
| 898 |
@ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00360-01]} |
| 899 |
@ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95} |
| 900 |
@b[Amendment Correction:] Type Generator in Numerics.Float_Random and in an |
| 901 |
instance of Numerics.Discrete_Random is defined to need finalization. If the |
| 902 |
restriction No_Nested_Finalization (see @RefSecNum{Tasking Restrictions}) |
| 903 |
applies to the partition, and Generator does not have a controlled part, it |
| 904 |
will not be allowed in local objects in Ada 2005 whereas it would be allowed |
| 905 |
in original Ada 95. Such code is not portable, as another Ada compiler may |
| 906 |
have a controlled part in Generator, and thus would be illegal.]} |
| 907 |
@end{Incompatible95} |
| 908 |
|
| 909 |
@begin{DiffWord95} |
| 910 |
@ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0050],ARef=[AI95-00089-01]} |
| 911 |
@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]} |
| 912 |
@ChgAdded{Version=[2],Text=[@b<Corrigendum:> Made the passing of an incorrect |
| 913 |
Image of a generator a bounded error, as it @Chg{Version=[3],New=[might],Old=[may]} |
| 914 |
not be practical to check |
| 915 |
for problems (if a generator consists of several related values).]} |
| 916 |
@end{DiffWord95} |
| 917 |
|
| 918 |
@begin{DiffWord2005} |
| 919 |
@ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0280-1]} |
| 920 |
@ChgAdded{Version=[3],Text=[@b<Correction:> Specified the implicit initial |
| 921 |
value for (sub)type State. This was unspecified in Ada 95 and Ada 2005, |
| 922 |
so a program depending on some other initial value is very unlikely |
| 923 |
and certainly was not portable. An implementation can use default expressions, |
| 924 |
aspect Default_Value, or aspect Default_Component_Value to keep the |
| 925 |
representation of the type unchanged while meeting this new requirement.]} |
| 926 |
@end{DiffWord2005} |
| 927 |
|