14 Numbers
All numbers are immutable, and equal? iff eq?. The number type is encapsulated.
The external representation of an undefined number is #undefined. The external representation of a real with no primary value is #real (but this may change in the future, the report is missing the output representation for reals with no primary values). All other rules for externally representing numbers pertain only to defined numbers with primary values.
An external representation of a real number consists of optional radix and/or exactness prefixes, optional sign (+ or -), and magnitude. The radix prefixes are #b (binary), #o (octal), #d (decimal), and #x (hexadecimal); the default is decimal. The exactness prefixes are #e (exact) and #i (inexact); by default, the number is inexact iff the magnitude representation uses floating point. If both kinds of prefixes are used, they may occur in either order. The magnitude is either infinity; an unsigned integer (nonempty sequence of digits); a ratio of unsigned integers (two unsigned integers with a / between, of which the second is non-zero); or a floating point representation. If the magnitude is infinity, there must be an exactness prefix and a sign, and no radix prefix. Floating point representation can only be used with decimal radix; it consists of nonempty integer part, point (.), nonempty fraction part, and optional exponent part. The optional exponent part consists of an exponent letter, and an (optionally signed) integer indicating a power of ten by which to multiply the magnitude. The choice of exponent letter makes no difference in what mathematical number is indicated by the external representation, but does indicate internal representation precision. Exponent letters s, f, d, f indicate preference for successively higher internal precision – short, float, double, long. When reading an inexact real number, exponent letter e accepts the default internal precision, which must be at least double. When writeing an inexact real number, exponent letter e may be used for the default internal precision, and must be used for any internal number format not indicated by any of the other exponent letters. Float and double must provide, respectively, at least as much precision as IEEE 32-bit and 64-bit floating point standards [IE85]. For example, #i#xa/c represents an inexact number using hexadecimal notation, with signed magnitude positive five sixths (ten over twelve). -3.5l-2 represents an inexact number using decimal notation, with signed magnitude negative thirty five thousandths, and requested long precision (which must be at least IEEE 64-bit floating point). When reading an external representation of an inexact real, the bounds on the resulting inexact number are chosen in accordance with the narrow-arithmetic keyed dynamic variable.
NOTE: in klisp, all inexact numbers are stored as IEEE 64-bit floating point. No bounding or robustness info is kept.
— Applicative: number? (number? . objects)
The primitive type predicate for type number.
number?returns true iff all the objects inobjectsare of type number.
— Applicative: integer? (integer? . objects)
The primitive type predicate for number subtype integer.
integer?returns true iff all the objects inobjectsare of type integer.
— Applicative: exact-integer? (exact-integer? . objects)
The primitive type predicate for number subtype exact integer.
exact-integer?returns true iff all the objects inobjectsare of type integer and exact.SOURCE NOTE: this is from r7rs.
— Applicative: u8? (u8? . objects)
The primitive type predicate for number subtype exact integer between 0 and 255. This is the subtype used in bytevectors.
u8?returns true iff all the objects inobjectsare of type integer, are exact, and lie between 0 and 255 inclusive.SOURCE NOTE: this is handy for use with bytevectors.
— Applicative: rational? (rational? . objects)
The primitive type predicate for number subtype rational.
rational?returns true iff all the objects inobjectsare of type rational.
— Applicative: real? (real? . objects)
The primitive type predicate for number subtype real.
real?returns true iff all the objects inobjectsare of type real.
— Applicative: finite? (finite? . numbers)
Predicate
finite?returns true iff all the numbers innumbersare finite.
— Applicative: exact? (exact? . numbers)
Predicate
exact?returns true iff all the numbers innumbersare exact.
— Applicative: inexact? (inexact? . numbers)
Predicate
inexact?returns true iff all the numbers innumbersare inexact.
— Applicative: robust? (robust? . numbers)
Predicate
robust?returns true iff all the numbers innumbersare robust.
— Applicative: undefined? (undefined? . numbers)
Predicate
undefined?returns true iff all the numbers innumbersare undefined.
— Applicative: =? (=? . numbers)
Applicative
=?is a predicate that returns true iff all its arguments are numerically equal to each other. If any of its arguments has no primary value, an error is signaled.
— Applicative: <=? (<=? . reals)
— Applicative: >? (>? . reals)
— Applicative: >=? (>=? . reals)
Each of these applicatives is a predicate that returns true iff every two consecutive elements of
realshave primary values in the order indicated by the name of the applicative. If any element ofrealshas no primary value, an error is signaled.
— Applicative: + (+ . numbers)
Applicative
+returns the sum of the elements of numbers. If numbers is empty, the sum of its elements is exact zero. If a positive infinity is added to a negative infinity, the result has no primary value. If all the elements of a cycle are zero, the sum of the cycle is zero. If the acyclic sum of the elements of a cycle (i.e., the sum of an acyclic list containing just those elements) is non-zero, the sum of the cycle is positive infinity times the acyclic sum of the elements. If the acyclic sum of the elements of a cycle is zero, but some of the elements of the cycle are non-zero, the sum of the cycle has no primary value.
— Applicative: * (* . numbers)
Applicative
*returns the product of the elements of numbers. If numbers is empty, the product of its elements is exact one. If an infinity is multiplied by zero, the result has no primary value. If the acyclic product of the elements of a cycle is real greater than one, the product of the cycle is positive infinity. If all the elements of a cycle are positive one, the product of the cycle is positive one. If the acyclic product of the elements of a cycle is positive one, but some of the elements of the cycle are not positive one, the product of the cycle has no primary value. If the acyclic product of the elements of a cycle has magnitude less than one, the product of the cycle is zero. If the acyclic product of the elements of a cycle has magnitude greater than or equal to one, and is not positive real, the product of the cycle has no primary value.
— Applicative: – (– number . numbers)
numbersshould be a nonempty list of numbers.Applicative
-returns the sum ofnumberwith the negation of the sum ofnumbers.
— Applicative: zero? (zero? . numbers)
Applicative
zero?is a predicate that returns true iff every element ofnumbersis zero. For this purpose, a real number is zero if its primary value is zero. If any element of numbers has no primary value an error is signaled.
— Applicative: mod (mod real1 real2)
— Applicative: div-and-mod (div-and-mod real1 real2)
For all three applicatives, if
real1is infinite orreal2is zero, an error is signaled.Let
nbe the greatest integer such thatreal2 * n <= real1. Applicativedivreturnsn. Applicativemodreturnsreal1 - (real2 * n). Applicativediv-and-modreturns a freshly allocated list of length two, whose first element isnand whose second element isreal1 - (real2 * n).NOTE: I’m not really sure about this description…
— Applicative: mod0 (mod0 real1 real2)
— Applicative: div0-and-mod0 (div0-and-mod0 real1 real2)
For all three applicatives, if
real1is infinite orreal2is zero, an error is signaled.Let
nbe the greatest integer such thatreal2 * n <= real1 + |real2/2|. Applicativediv0returnsn. Applicativemod0returnsreal1 - (real2 * n). Applicativediv0-and-mod0returns a freshly allocated list of length two, whose first element isnand whose second element isreal1 - (real2 * n).NOTE: I’m not really sure about this description…
— Applicative: negative? (negative? . reals)
Applicative
positive?is a predicate that returns true iff every element ofrealsis greater than zero. Applicativenegative?is a predicate that returns true iff every element ofrealsis less than zero. If any argument to either applicative has no primary value an error is signaled.
— Applicative: even? (even? . integers)
Applicative
odd?is a predicate that returns true iff every element ofintegersis odd. Applicativeeven?is a predicate that returns true iff every element ofintegersis even. If any argument to either applicative has no primary value an error is signaled.
Applicative
absreturns the nonnegative real number with the same magnitude asreal; that is, ifrealis nonnegative it returnsreal, otherwise it returns the negation ofreal.
— Applicative: min (min . reals)
If
realsis nil, applicativemaxreturns exact negative infinity, and applicativeminreturns exact positive infinity. Ifrealsis non-nil, applicativemaxreturns the largest number inreals, and applicativeminreturns the smallest number inreals.
— Applicative: gcd (gcd . impints)
impintsshould be a list of improper integers, that is, real numbers each of which is either an integer or an infinity.Applicative
lcmreturns the smallest positive improper integer that is an improper0integer multiple of every element ofimpints(that is, smallestn >= 1such that for every argumentnkthere existsn'kwithnk * n'k = n). If any of the arguments is zero, the result oflcmhas no primary value. According to these rules,lcmwith nil argument list returns1, andlcmwith any infinite argument returns positive infinity.Applicative
gcdreturns the largest positive improper integer such that every element ofimpintsis an improper-integer multiple of it (that is, largestn >= 1such that for every argumentnkthere existsn'kwithn * n'k = nk).gcdwith nil argument list returns exact positive infinity. Ifgcdis called with one or more arguments, and at least one of the arguments is zero, but none of the arguments is a non-zero finite integer, its result has no primary value. According to these rules, ifgcdis called with at least one finite non-zero argument, its result is the same as if all zero and infinite arguments were deleted.
— Applicative: get-real-exact-bounds (get-real-exact-bounds real)
Applicative
get-real-internal-boundsreturns a freshly allocated list of reals(x1 x2), where the primary value ofx1is the lower bound ofreal, using the same internal representation as the primary value ofreal, and the primary value ofx2is the upper bound ofreal, using the same internal representation as the primary value ofreal. Thexkare inexact iff real is inexact. Thexkare robust (i.e., tagged if the implementation supports such), and the bounds of eachxkare only required to contain its primary value (i.e., the implementation is allowed to make the bounds equal to the primary value).Applicative
get-real-exact-boundsreturns a freshly allocated list of exact reals(x1 x2), wherex1is not greater than the lower bound ofreal, andx2is not less than the upper bound ofreal.
— Applicative: get-real-exact-primary (get-real-exact-primary real)
If
realis exact, both applicatives returnreal. Ifrealhas no primary value, both applicatives signal an error.If
realis inexact with a primary value, applicativeget-real-internal-primaryreturns a real numberx0whose primary value is the same as, and has the same internal format as, the primary value ofreal.x0is robust, and its bounds are only required to contain its primary value.If
realis inexact with a primary value, applicativeget-real-exact-primaryreturns an exact real numberx0within the exact bounds that would be returned forrealby applicativeget-real-exact-bounds. Preferably,x0should be as close to the primary value ofrealas the implementation can reasonably arrange. If the implementation does not support any exactrealthat reasonably approximatesreal, an error may be signaled.
— Applicative: make-inexact (make-inexact real1 real2 real3)
Applicative
make-inexactreturns an inexact real number, as follows. Ifreal2is inexact, the result has the same primary value asreal2; and ifreal2has no primary value, the result has no primary value. The result has the same robustness asreal2. If possible, the result uses the same internal representation asreal2. Ifreal2is exact, the primary value of the result is as close toreal2as the implementation can reasonably arrange; overflow and underflow are handled as described in …. The lower bound of the result is no greater than the lower bound ofreal1, the primary value ofreal2, and the primary value of the result. The upper bound of the result is no less than the upper bound ofreal3, the primary value ofreal2, and the primary value of the result.
— Applicative: real->exact (real->exact real)
Applicative
real->exactbehaves just asget-real-exact-primary.If
realis inexact, applicativereal->inexactreturnsreal. Ifrealis exact, applicativereal->inexactreturns an inexact realx0such thatrealwould be a permissible result of passingx0toreal->exact. If the implementation does not support any suchx0, an error may be signaled. Otherwise,x0is robust, and its bounds are only required to contain its primary value andreal.
— Applicative: get-string-arithmetic (get-strict-arithmetic?)
These applicatives are the binder and accessor of the
strict-arithmetickeyed dynamic variable. When this keyed variable is true, various survivable but dubious arithmetic events signal an error – notably, operation results with no primary value, and over- and underflows.
— Applicative: / (/ number . numbers)
numbersshould be a nonempty list of numbers.Applicative
/returnsnumberdivided by the product ofnumbers. If the product ofnumbersis zero, an error is signaled. Ifnumberis infinite and the product ofnumbersis infinite, an error is signaled.
— Applicative: denominator (denominator rational)
These applicatives return the numerator and denominator of
rational, in least terms (i.e., chosen for the least positive denominator). Note that ifrationalis inexact, and either of its bounds is not its primary value, the denominator has upper bound positive infinity, and the numerator must have at least one infinite bound (two infinite bounds if the bounds of rational allow values of both signs).
— Applicative: ceiling (ceiling real)
— Applicative: truncate (truncate real)
— Applicative: round (round real)
Applicative
floorreturns the largest integer not greater thanreal.Applicative
ceilingreturns the smallest integer not less thanreal.Applicative
truncatereturns the integer closest torealwhose absolute value is not greater than that ofreal.Applicative
roundreturns the closest integer toreal, rounding to even whenrealis halfway between two integers.
— Applicative: simplest-rational (simplest-rational real1 real2)
A rational number
r1is simpler than another rationalr2ifr1 = p1 / q1andr2 = p2 / q2, both in lowest terms, and|p1| <= |p2|and|q1| <= |q2|. Thus3/5is simpler than4/7. Not all rationals are comparable in this ordering, as for example2/7and3/5. However, any interval (that contains rational numbers) contains a rational number that is simpler than every other rational number in that interval. Note that0 = 0/1is simpler than any other rational (so that one never has to choose betweenp/qand−p/q).For applicative
simplest-rational, letx0be the simplest rational mathematically not less than the primary value ofreal1and not greater than the primary value ofreal2. If no suchx0exists (because the primary value ofreal1is greater, or because the primary values of the arguments are equal and irrational), or if either argument does not have a primary value, an error is signaled.For applicative
rationalize, letx0be the simplest rational mathematical number within the interval bounded by the primary value ofreal1plus and minus the primary value ofreal2. If no suchx0exists (because the primary value ofreal1is irrational and the primary valuereal2is zero), or if either argument does not have a primary value, an error is signaled.If
real1andreal2are exact, the applicative (whichever it is) returns exactx0. If one or both ofreal1andreal2are inexact, the applicative returns an inexact rational approximatingx0(as byreal->inexact. Note that an inexact result returned is not necessarily bounded by the primary values of the arguments; but the result is an approximation ofx0, which is so bounded, and the bounds of the result includex0.
— Applicative: sqrt (sqrt number)
If
numberis negative, the result is undefined.Applicative
sqrtreturns the positive square root of number. The result may be inexact even ifnumberis exact and the square root is rational.
— Applicative: expt (expt number1 number2)
Applicative
exptreturnsnumber1to the power ofnumber2. Ifnumber1is zero, then the result is 1 ifnumber2is zero and 0 otherwise.
— Applicative: log (log number)
— Applicative: sin (sin number)
— Applicative: cos (cos number)
— Applicative: tan (tan number)
— Applicative: asin (asin number)
— Applicative: acos (acos number)
— Applicative: atan (atan number1 [number2])
These applicatives compute the usual transcendental functions.
logcomputes the natural logarithm (not the base-10 logarithm). The two argument version ofatancomputes(angle (make-recutangular number1 number2))even thou klisp doesn’t support complex numbers.All results may be inexact even if
numberis exact and the result of the transcendental function is rational. TODO add intervals returned for multidefined functions (inverses and log)
— Applicative: string->number (string->number string [radix])
radixshould be an exact integer, either 2, 8, 10, or 16.stringshould be a string describing a number in the specified radix, but may contain a radix prefix to override it. The defaultradix, if not supplied, is 10.Applicative
string->numberreturns the best approximation of the number represented bystring. Ifstringis not a valid representation of a number in the givenradixan error is signaled.Examples:
(string->number "100") ⇒ 100 (string->number "100" 16) ⇒ 256 (string->number "#o100" 2) ⇒ 64 (string->number "1.0") ⇒ 1.0SOURCE NOTE: this is taken from r7rs.
— Applicative: number->string (number->string number [radix])
radixshould be an exact integer, either 2, 8, 10, or 16. The defaultradix, if not supplied, is 10.Applicative
number->stringreturns a string representingnumberin the givenradix. No radix prefix is present in the returned string. If an inexact number is passed together with a radix other from 10, an error is signaled.The returned string is such that
(string->number (number->string number radix) radix) == numberExamples:
(number->string 100) ⇒ "100" (number->string 256 16) ⇒ "100" (number->string 1.0) ⇒ "1.0"SOURCE NOTE: this is taken from r7rs.