numbers.texi (23492B)
1 @c -*-texinfo-*- 2 @setfilename ../src/numbers 3 4 @node Numbers, Strings, Keyed Variables, Top 5 @comment node-name, next, previous, up 6 7 @chapter Numbers 8 @cindex numbers 9 10 All numbers are immutable, and @code{equal?} iff @code{eq?}. The 11 number type is encapsulated. 12 13 @c TODO add more content on numbers 14 15 The external representation of an undefined number is 16 @code{#undefined}. The external representation of a real with no 17 primary value is @code{#real} (but this may change in the future, the 18 report is missing the output representation for reals with no primary 19 values). All other rules for externally representing numbers pertain 20 only to defined numbers with primary values. 21 22 An external representation of a real number consists of optional 23 radix and/or exactness prefixes, optional sign (@code{+} or @code{-}), 24 and magnitude. The radix prefixes are @code{#b} (binary), @code{#o} 25 (octal), @code{#d} (decimal), and @code{#x} (hexadecimal); the default 26 is decimal. The exactness prefixes are @code{#e} (exact) and 27 @code{#i} (inexact); by default, the number is inexact iff the 28 magnitude representation uses floating point. If both kinds of 29 prefixes are used, they may occur in either order. The magnitude is 30 either @code{infinity}; an unsigned integer (nonempty sequence of 31 digits); a ratio of unsigned integers (two unsigned integers with a 32 @code{/} between, of which the second is non-zero); or a floating 33 point representation. If the magnitude is @code{infinity}, there must 34 be an exactness prefix and a sign, and no radix prefix. Floating 35 point representation can only be used with decimal radix; it consists 36 of nonempty integer part, point (@code{.}), nonempty fraction part, 37 and optional exponent part. The optional exponent part consists of an 38 exponent letter, and an (optionally signed) integer indicating a power 39 of ten by which to multiply the magnitude. The choice of exponent 40 letter makes no difference in what mathematical number is indicated by 41 the external representation, but does indicate internal representation 42 precision. Exponent letters @code{s}, @code{f}, @code{d}, @code{f} 43 indicate preference for successively higher internal precision - 44 short, float, double, long. When reading an inexact real number, 45 exponent letter @code{e} accepts the default internal precision, which 46 must be at least double. When writeing an inexact real number, 47 exponent letter @code{e} may be used for the default internal 48 precision, and must be used for any internal number format not 49 indicated by any of the other exponent letters. Float and double must 50 provide, respectively, at least as much precision as IEEE 32-bit and 51 64-bit floating point standards [IE85]. For example, @code{#i#xa/c} 52 represents an inexact number using hexadecimal notation, with signed 53 magnitude positive five sixths (ten over twelve). @code{-3.5l-2} 54 represents an inexact number using decimal notation, with signed 55 magnitude negative thirty five thousandths, and requested long 56 precision (which must be at least IEEE 64-bit floating point). When 57 reading an external representation of an inexact real, the bounds on 58 the resulting inexact number are chosen in accordance with the 59 @c TODO add xref 60 narrow-arithmetic keyed dynamic variable. 61 62 NOTE: in klisp, all inexact numbers are stored as IEEE 64-bit floating 63 point. No bounding or robustness info is kept. 64 65 @deffn Applicative number? (number? . objects) 66 The primitive type predicate for type number. @code{number?} 67 returns true iff all the objects in @code{objects} are of type number. 68 @end deffn 69 70 @deffn Applicative integer? (integer? . objects) 71 The primitive type predicate for number subtype integer. 72 @code{integer?} returns true iff all the objects in @code{objects} 73 are of type integer. 74 @end deffn 75 76 @deffn Applicative exact-integer? (exact-integer? . objects) 77 The primitive type predicate for number subtype exact integer. 78 @code{exact-integer?} returns true iff all the objects in 79 @code{objects} are of type integer and exact. 80 81 SOURCE NOTE: this is from r7rs. 82 @end deffn 83 84 @deffn Applicative u8? (u8? . objects) 85 The primitive type predicate for number subtype exact integer between 86 0 and 255. This is the subtype used in bytevectors. @code{u8?} 87 returns true iff all the objects in @code{objects} are of type 88 integer, are exact, and lie between 0 and 255 inclusive. 89 90 SOURCE NOTE: this is handy for use with bytevectors. 91 @end deffn 92 93 @deffn Applicative rational? (rational? . objects) 94 The primitive type predicate for number subtype rational. 95 @code{rational?} returns true iff all the objects in @code{objects} 96 are of type rational. 97 @end deffn 98 99 @deffn Applicative real? (real? . objects) 100 The primitive type predicate for number subtype real. 101 @code{real?} returns true iff all the objects in @code{objects} 102 are of type real. 103 @end deffn 104 105 @deffn Applicative finite? (finite? . numbers) 106 Predicate @code{finite?} returns true iff all the numbers in 107 @code{numbers} are finite. 108 @end deffn 109 110 @deffn Applicative exact? (exact? . numbers) 111 Predicate @code{exact?} returns true iff all the numbers in 112 @code{numbers} are exact. 113 @end deffn 114 115 @deffn Applicative inexact? (inexact? . numbers) 116 Predicate @code{inexact?} returns true iff all the numbers in 117 @code{numbers} are inexact. 118 @end deffn 119 120 @deffn Applicative robust? (robust? . numbers) 121 Predicate @code{robust?} returns true iff all the numbers in 122 @code{numbers} are robust. 123 @end deffn 124 125 @deffn Applicative undefined? (undefined? . numbers) 126 Predicate @code{undefined?} returns true iff all the numbers in 127 @code{numbers} are undefined. 128 @end deffn 129 130 @deffn Applicative =? (=? . numbers) 131 Applicative @code{=?} is a predicate that returns true iff all its 132 arguments are numerically equal to each other. If any of its 133 arguments has no primary value, an error is signaled. 134 @end deffn 135 136 @deffn Applicative <? (<? . reals) 137 @deffnx Applicative <=? (<=? . reals) 138 @deffnx Applicative >? (>? . reals) 139 @deffnx Applicative >=? (>=? . reals) 140 Each of these applicatives is a predicate that returns true iff 141 every two consecutive elements of @code{reals} have primary values in 142 the order indicated by the name of the applicative. If any element of 143 @code{reals} has no primary value, an error is signaled. 144 @end deffn 145 146 @deffn Applicative + (+ . numbers) 147 Applicative @code{+} returns the sum of the elements of numbers. If 148 numbers is empty, the sum of its elements is exact zero. If a 149 positive infinity is added to a negative infinity, the result has no 150 primary value. If all the elements of a cycle are zero, the sum of 151 the cycle is zero. If the acyclic sum of the elements of a cycle 152 (i.e., the sum of an acyclic list containing just those elements) is 153 non-zero, the sum of the cycle is positive infinity times the acyclic 154 sum of the elements. If the acyclic sum of the elements of a cycle is 155 zero, but some of the elements of the cycle are non-zero, the sum of 156 the cycle has no primary value. 157 @end deffn 158 159 @deffn Applicative * (* . numbers) 160 Applicative @code{*} returns the product of the elements of numbers. 161 If numbers is empty, the product of its elements is exact one. If an 162 infinity is multiplied by zero, the result has no primary value. If 163 the acyclic product of the elements of a cycle is real greater than 164 one, the product of the cycle is positive infinity. If all the 165 elements of a cycle are positive one, the product of the cycle is 166 positive one. If the acyclic product of the elements of a cycle is 167 positive one, but some of the elements of the cycle are not positive 168 one, the product of the cycle has no primary value. If the acyclic 169 product of the elements of a cycle has magnitude less than one, the 170 product of the cycle is zero. If the acyclic product of the elements 171 of a cycle has magnitude greater than or equal to one, and is not 172 positive real, the product of the cycle has no primary value. 173 @end deffn 174 175 @deffn Applicative - (- number . numbers) 176 @code{numbers} should be a nonempty list of numbers. 177 178 Applicative @code{-} returns the sum of @code{number} with the 179 negation of the sum of @code{numbers}. 180 @end deffn 181 182 @deffn Applicative zero? (zero? . numbers) 183 Applicative @code{zero?} is a predicate that returns true iff every 184 element of @code{numbers} is zero. For this purpose, a real number is 185 zero if its primary value is zero. If any element of numbers has no 186 primary value an error is signaled. 187 @end deffn 188 189 @deffn Applicative div (div real1 real2) 190 @deffnx Applicative mod (mod real1 real2) 191 @deffnx Applicative div-and-mod (div-and-mod real1 real2) 192 For all three applicatives, if @code{real1} is infinite or 193 @code{real2} is zero, an error is signaled. 194 195 Let @code{n} be the greatest integer such that @code{real2 * n <= 196 real1}. Applicative @code{div} returns @code{n}. Applicative 197 @code{mod} returns @code{real1 - (real2 * n)}. Applicative 198 @code{div-and-mod} returns a freshly allocated list of length two, 199 whose first element is @code{n} and whose second element is 200 @code{real1 - (real2 * n)}. 201 202 NOTE: I'm not really sure about this description... 203 @end deffn 204 205 @deffn Applicative div0 (div0 real1 real2) 206 @deffnx Applicative mod0 (mod0 real1 real2) 207 @deffnx Applicative div0-and-mod0 (div0-and-mod0 real1 real2) 208 For all three applicatives, if @code{real1} is infinite or 209 @code{real2} is zero, an error is signaled. 210 211 Let @code{n} be the greatest integer such that @code{real2 * n <= 212 real1 + |real2/2|}. Applicative @code{div0} returns @code{n}. 213 Applicative @code{mod0} returns @code{real1 - (real2 * n)}. 214 Applicative @code{div0-and-mod0} returns a freshly allocated list of 215 length two, whose first element is @code{n} and whose second element 216 is @code{real1 - (real2 * n)}. 217 218 NOTE: I'm not really sure about this description... 219 @end deffn 220 221 @deffn Applicative positive? (positive? . reals) 222 @deffnx Applicative negative? (negative? . reals) 223 Applicative @code{positive?} is a predicate that returns true iff 224 every element of @code{reals} is greater than zero. Applicative 225 @code{negative?} is a predicate that returns true iff every element of 226 @code{reals} is less than zero. If any argument to either applicative 227 has no primary value an error is signaled. 228 @end deffn 229 230 @deffn Applicative odd? (odd? . integers) 231 @deffnx Applicative even? (even? . integers) 232 Applicative @code{odd?} is a predicate that returns true iff every 233 element of @code{integers} is odd. Applicative @code{even?} is a 234 predicate that returns true iff every element of @code{integers} is 235 even. If any argument to either applicative has no primary value an 236 error is signaled. 237 @end deffn 238 239 @deffn Applicative abs (abs real) 240 Applicative @code{abs} returns the nonnegative real number with the 241 same magnitude as @code{real}; that is, if @code{real} is nonnegative 242 it returns @code{real}, otherwise it returns the negation of 243 @code{real}. 244 @end deffn 245 246 @deffn Applicative max (max . reals) 247 @deffnx Applicative min (min . reals) 248 If @code{reals} is nil, applicative @code{max} returns exact 249 negative infinity, and applicative @code{min} returns exact positive 250 infinity. If @code{reals} is non-nil, applicative @code{max} returns 251 the largest number in @code{reals}, and applicative @code{min} returns 252 the smallest number in @code{reals}. 253 @end deffn 254 255 256 @deffn Applicative lcm (lcm . impints) 257 @deffnx Applicative gcd (gcd . impints) 258 @code{impints} should be a list of improper integers, that is, real 259 numbers each of which is either an integer or an infinity. 260 261 Applicative @code{lcm} returns the smallest positive improper 262 integer that is an improper0integer multiple of every element of 263 @code{impints} (that is, smallest @code{n >= 1} such that for every 264 argument @code{nk} there exists @code{n'k} with @code{nk * n'k = n}). 265 If any of the arguments is zero, the result of @code{lcm} has no 266 primary value. According to these rules, @code{lcm} with nil argument 267 list returns @code{1}, and @code{lcm} with any infinite argument 268 returns positive infinity. 269 270 Applicative @code{gcd} returns the largest positive improper integer 271 such that every element of @code{impints} is an improper-integer 272 multiple of it (that is, largest @code{n >= 1} such that for every 273 argument @code{nk} there exists @code{n'k} with @code{n * n'k = nk}). 274 @code{gcd} with nil argument list returns exact positive infinity. If 275 @code{gcd} is called with one or more arguments, and at least one of 276 the arguments is zero, but none of the arguments is a non-zero finite 277 integer, its result has no primary value. According to these rules, 278 if @code{gcd} is called with at least one finite non-zero argument, 279 its result is the same as if all zero and infinite arguments were 280 deleted. 281 @end deffn 282 283 @deffn Applicative get-real-internal-bounds (get-real-internal-bounds real) 284 @deffnx Applicative get-real-exact-bounds (get-real-exact-bounds real) 285 Applicative @code{get-real-internal-bounds} returns a freshly 286 allocated list of reals @code{(x1 x2)}, where the primary value of 287 @code{x1} is the lower bound of @code{real}, using the same internal 288 representation as the primary value of @code{real}, and the primary 289 value of @code{x2} is the upper bound of @code{real}, using the same 290 internal representation as the primary value of @code{real}. The 291 @code{xk} are inexact iff real is inexact. The @code{xk} are robust 292 (i.e., tagged if the implementation supports such), and the bounds of 293 each @code{xk} are only required to contain its primary value (i.e., 294 the implementation is allowed to make the bounds equal to the primary 295 value). 296 297 Applicative @code{get-real-exact-bounds} returns a freshly allocated 298 list of exact reals @code{(x1 x2)}, where @code{x1} is not greater 299 than the lower bound of @code{real}, and @code{x2} is not less than 300 the upper bound of @code{real}. 301 @end deffn 302 303 @deffn Applicative get-real-internal-primary (get-real-internal-primary real) 304 @deffnx Applicative get-real-exact-primary (get-real-exact-primary real) 305 If @code{real} is exact, both applicatives return @code{real}. If 306 @code{real} has no primary value, both applicatives signal an error. 307 308 If @code{real} is inexact with a primary value, applicative 309 @code{get-real-internal-primary} returns a real number @code{x0} whose 310 primary value is the same as, and has the same internal format as, the 311 primary value of @code{real}. @code{x0} is robust, and its bounds are 312 only required to contain its primary value. 313 314 @c TODO add xref to get-real-exact-bounds 315 If @code{real} is inexact with a primary value, applicative 316 @code{get-real-exact-primary} returns an exact real number @code{x0} 317 within the exact bounds that would be returned for @code{real} by 318 applicative @code{get-real-exact-bounds}. Preferably, @code{x0} 319 should be as close to the primary value of @code{real} as the 320 implementation can reasonably arrange. If the implementation does not 321 support any exact @code{real} that reasonably approximates 322 @code{real}, an error may be signaled. 323 @end deffn 324 325 @deffn Applicative make-inexact (make-inexact real1 real2 real3) 326 Applicative @code{make-inexact} returns an inexact real number, as 327 follows. If @code{real2} is inexact, the result has the same primary 328 value as @code{real2}; and if @code{real2} has no primary value, the 329 result has no primary value. The result has the same robustness as 330 @code{real2}. If possible, the result uses the same internal 331 representation as @code{real2}. If @code{real2} is exact, the primary 332 value of the result is as close to @code{real2} as the implementation 333 can reasonably arrange; overflow and underflow are handled as 334 @c TODO add xref to overflow 335 described in .... The lower bound of the result is no greater than 336 the lower bound of @code{real1}, the primary value of @code{real2}, 337 and the primary value of the result. The upper bound of the result is 338 no less than the upper bound of @code{real3}, the primary value of 339 @code{real2}, and the primary value of the result. 340 @end deffn 341 342 @deffn Applicative real->inexact (real->inexact real) 343 @deffnx Applicative real->exact (real->exact real) 344 @c TODO add xref to get-real-exact-primary 345 Applicative @code{real->exact} behaves just as 346 @code{get-real-exact-primary}. 347 348 If @code{real} is inexact, applicative @code{real->inexact} returns 349 @code{real}. If @code{real} is exact, applicative 350 @code{real->inexact} returns an inexact real @code{x0} such that 351 @code{real} would be a permissible result of passing @code{x0} to 352 @code{real->exact}. If the implementation does not support any such 353 @code{x0}, an error may be signaled. Otherwise, @code{x0} is robust, 354 and its bounds are only required to contain its primary value and 355 @code{real}. 356 @end deffn 357 358 @deffn Applicative with-strict-arithmetic (with-strict-arithmetic boolean combiner) 359 @deffnx Applicative get-string-arithmetic (get-strict-arithmetic?) 360 @c TODO add xref to dynamic keys and under/over flow, no prim value 361 These applicatives are the binder and accessor of the 362 @code{strict-arithmetic} keyed dynamic variable. When this keyed 363 variable is true, various survivable but dubious arithmetic events 364 signal an error - notably, operation results with no primary value, 365 and over- and underflows. 366 @end deffn 367 368 @deffn Applicative / (/ number . numbers) 369 @code{numbers} should be a nonempty list of numbers. 370 371 Applicative @code{/} returns @code{number} divided by the product of 372 @code{numbers}. If the product of @code{numbers} is zero, an error is 373 signaled. If @code{number} is infinite and the product of @code{numbers} is 374 infinite, an error is signaled. 375 @end deffn 376 377 @deffn Applicative numerator (numerator rational) 378 @deffnx Applicative denominator (denominator rational) 379 These applicatives return the numerator and denominator of 380 @code{rational}, in least terms (i.e., chosen for the least positive 381 denominator). Note that if @code{rational} is inexact, and either of 382 its bounds is not its primary value, the denominator has upper bound 383 positive infinity, and the numerator must have at least one infinite 384 bound (two infinite bounds if the bounds of rational allow values of 385 both signs). 386 @end deffn 387 388 389 @deffn Applicative floor (floor real) 390 @deffnx Applicative ceiling (ceiling real) 391 @deffnx Applicative truncate (truncate real) 392 @deffnx Applicative round (round real) 393 Applicative @code{floor} returns the largest integer not greater 394 than @code{real}. 395 396 Applicative @code{ceiling} returns the smallest integer not less 397 than @code{real}. 398 399 Applicative @code{truncate} returns the integer closest to 400 @code{real} whose absolute value is not greater than that of 401 @code{real}. 402 403 Applicative @code{round} returns the closest integer to @code{real}, 404 rounding to even when @code{real} is halfway between two integers. 405 @end deffn 406 407 @deffn Applicative rationalize (rationalize real1 real2) 408 @deffnx Applicative simplest-rational (simplest-rational real1 real2) 409 A rational number @code{r1} is simpler than another rational 410 @code{r2} if @code{r1 = p1 / q1} and @code{r2 = p2 / q2}, both in 411 lowest terms, and @code{|p1| <= |p2|} and @code{|q1| <= |q2|}. Thus 412 @code{3/5} is simpler than @code{4/7}. Not all rationals are 413 comparable in this ordering, as for example @code{2/7} and @code{3/5}. 414 However, any interval (that contains rational numbers) contains a 415 rational number that is simpler than every other rational number in 416 that interval. Note that @code{0 = 0/1} is simpler than any other 417 rational (so that one never has to choose between @code{p/q} and 418 @code{−p/q}). 419 420 For applicative @code{simplest-rational}, let @code{x0} be the 421 simplest rational mathematically not less than the primary value of 422 @code{real1} and not greater than the primary value of @code{real2}. 423 If no such @code{x0} exists (because the primary value of @code{real1} 424 is greater, or because the primary values of the arguments are equal 425 and irrational), or if either argument does not have a primary value, 426 an error is signaled. 427 428 For applicative @code{rationalize}, let @code{x0} be the simplest 429 rational mathematical number within the interval bounded by the 430 primary value of @code{real1} plus and minus the primary value of 431 @code{real2}. If no such @code{x0} exists (because the primary value 432 of @code{real1} is irrational and the primary value @code{real2} is 433 zero), or if either argument does not have a primary value, an error 434 is signaled. 435 436 @c TODO add xref to real->inexact 437 If @code{real1} and @code{real2} are exact, the applicative 438 (whichever it is) returns exact @code{x0}. If one or both of 439 @code{real1} and @code{real2} are inexact, the applicative returns an 440 inexact rational approximating @code{x0} (as by @code{real->inexact}. 441 Note that an inexact result returned is not necessarily bounded by the 442 primary values of the arguments; but the result is an approximation of 443 @code{x0}, which is so bounded, and the bounds of the result include 444 @code{x0}. 445 @end deffn 446 447 @deffn Applicative sqrt (sqrt number) 448 If @code{number} is negative, the result is undefined. 449 450 Applicative @code{sqrt} returns the positive square root of number. 451 The result may be inexact even if @code{number} is exact and the 452 square root is rational. 453 @end deffn 454 455 @deffn Applicative expt (expt number1 number2) 456 Applicative @code{expt} returns @code{number1} to the power of 457 @code{number2}. If @code{number1} is zero, then the result is 1 if 458 @code{number2} is zero and 0 otherwise. 459 @end deffn 460 461 @deffn Applicative exp (exp number) 462 @deffnx Applicative log (log number) 463 @deffnx Applicative sin (sin number) 464 @deffnx Applicative cos (cos number) 465 @deffnx Applicative tan (tan number) 466 @deffnx Applicative asin (asin number) 467 @deffnx Applicative acos (acos number) 468 @deffnx Applicative atan (atan number1 [number2]) 469 These applicatives compute the usual transcendental functions. 470 @code{log} computes the natural logarithm (not the base-10 logarithm). 471 The two argument version of @code{atan} computes @code{(angle 472 (make-recutangular number1 number2))} even thou klisp doesn't support 473 complex numbers. 474 475 All results may be inexact even if @code{number} is exact and the 476 result of the transcendental function is rational. 477 TODO add intervals returned for multidefined functions (inverses and log) 478 @end deffn 479 480 @deffn Applicative string->number (string->number string [radix]) 481 @code{radix} should be an exact integer, either 2, 8, 10, or 16. 482 @code{string} should be a string describing a number in the specified 483 radix, but may contain a radix prefix to override it. The default 484 @code{radix}, if not supplied, is 10. 485 486 Applicative @code{string->number} returns the best approximation of 487 the number represented by @code{string}. If @code{string} is not a 488 valid representation of a number in the given @code{radix} an error is 489 signaled. 490 491 Examples: 492 @example 493 (string->number "100") @result{} 100 494 (string->number "100" 16) @result{} 256 495 (string->number "#o100" 2) @result{} 64 496 (string->number "1.0") @result{} 1.0 497 @end example 498 499 SOURCE NOTE: this is taken from r7rs. 500 @end deffn 501 502 @deffn Applicative number->string (number->string number [radix]) 503 @code{radix} should be an exact integer, either 2, 8, 10, or 16. The 504 default @code{radix}, if not supplied, is 10. 505 506 Applicative @code{number->string} returns a string representing 507 @code{number} in the given @code{radix}. No radix prefix is present 508 in the returned string. If an inexact number is passed together with 509 a radix other from 10, an error is signaled. 510 511 The returned string is such that 512 @example 513 (string->number (number->string number radix) radix) @equiv{} number 514 @end example 515 516 Examples: 517 @example 518 (number->string 100) @result{} "100" 519 (number->string 256 16) @result{} "100" 520 (number->string 1.0) @result{} "1.0" 521 @end example 522 523 SOURCE NOTE: this is taken from r7rs. 524 @end deffn