Node: Component Extractions on Numbers, Next: , Previous: Arithmetic Operations, Up: Numbers



Component Extractions on Numbers

numerator x Function

denominator x Function

Given x, an Lpp Rational number or int, the numerator function returns the numerator of x and denominator returns the denominator. Each function returns an Lpp Integer. If an integer n is given as the argument then it is treated as if it was n/1 so that numerator returns n and denominator returns 1. For example

     let ratio = divide(23, -198);
     numerator(ratio) => -23
     denominator(ratio) => 198
     numerator(numerator(ratio)) => -23
     denominator(numerator(ratio)) => 1
     

numeratorOf x Function

denominatorOf x Function

Since Lpp maintains Ratio objects as pairs of Lpp Integer objects it allows the access of the actual numerator and denominator objects. The functions numeratorOf and denominatorOf are exactly the same as numerator and denominator respectively except for the fact that they do not make a copy but instead return the actual Integer object. This is not necessary in Lisp but in C++ this allows operations on the parts of a Ratio without having to worry about deallocating the copies. This is a useful efficiency; however the programmer has the responsibility to not inadvertently change parts of a Ratio object which could break the rational canonicalization rule and even break the code. For example

     let ratio = readFromString("23/198");
     times(numerator(ratio), denominator(ratio));      // Oops, memory leak
     times(numeratorOf(ratio), denominatorOf(ratio));  // No leak
     dec(numerator(ratio));          // OK, doesn't change ratio
     dec(numeratorOf(ratio));        // Oops, does change ratio ...
                                     // and breaks canonicalization rule,
                                     // should now be 1/9 instead of 22/198
     

truncate x Function

truncate x y Function

truncateCons x Function

truncateCons x y Function

The truncate function with a single argument x converts it to an integer by truncating it toward zero. So if an integer n is given as the argument then it just returns a copy of n. If given a ratio as the argument then it is truncated as described here. If given two arguments x and y then truncate acts as if it is given a single ratio argument of x/y. The truncateCons function returns a cons whose car is what truncate would have returned and whose cdr is the remainder. The arguments to truncate and truncateCons can be int values or Lpp Rational numbers. Here are some examples

     truncate(L(5)) => 5
     truncate(divide(L(13), L(4))) => 3
     truncate(readFromString("13/4")) => 3
     truncate(13, L(4)) => 3
     truncateCons(readFromString("13/4")) => (3 . 1)
     let a = readFromString("5/3"); let b = readFromString("-2/3");
     truncate(a, b) => -2
     truncateCons(a, b) => (-2 . 1/3)
     

floor x Function

floor x y Function

floorCons x Function

floorCons x y Function

The floor family of functions above are the same as the truncate family above except that floor truncates toward negative infinity. For example

     floor(L(5)) => 5
     floor(divide(L(13), L(4))) => 3
     floor(readFromString("13/4")) => 3
     floor(13, L(4)) => 3
     floor(-13, 4) => -4
     floorCons(readFromString("13/4")) => (3 . 1)
     floorCons(readFromString("-13/4")) => (-4 . 3)
     let a = readFromString("5/3"); let b = readFromString("-2/3");
     floor(a, b) => -3
     floorCons(a, b) => (-3 . -1/3)
     

rem x y Function

mod x y Function

The rem function returns the same result that truncateCons of the same two arguments returns in its cdr. The mod function returns the same result that floorCons of the same two arguments returns in its cdr. For example comparing with the above examples of truncateCons and floorCons

     rem(13, 4) => 1
     mod(13, 4) => 1
     mod(-13, 4) => 3