Next: , Previous: , Up: baselib math ops   [Index]


4.6.4.6 Part separation

Procedure: numerator q
Procedure: denominator q

These procedures return the numerator or denominator of their argument; the result is computed as if the argument was represented as a fraction in lowest terms. The denominator is always positive. The denominator of 0 is defined to be 1.

(numerator   (/ 6 4))           ⇒ 3
(denominator (/ 6 4))           ⇒ 2
(denominator (inexact (/ 6 4))) ⇒ 2.0
Procedure: floor x
Procedure: ceiling x
Procedure: truncate x
Procedure: round x

These procedures return inexact integer objects for inexact arguments that are not infinities or NaNs, and exact integer objects for exact rational arguments.

floor

Returns the largest integer object not larger than x.

ceiling

Returns the smallest integer object not smaller than x.

truncate

Returns the integer object closest to x whose absolute value is not larger than the absolute value of x.

round

Returns the closest integer object to x, rounding to even when x represents a number halfway between two integers (this conforms to IEEE 754 round to nearest mode).

If the argument to one of these procedures is inexact, then the result is also inexact. If an exact value is needed, the result should be passed to the exact procedure.

Although infinities and NaNs are not integer objects, these procedures return an infinity when given an infinity as an argument, and a NaN when given a NaN.

(floor -4.3)            ⇒ -5.0
(ceiling -4.3)          ⇒ -4.0
(truncate -4.3)         ⇒ -4.0
(round -4.3)            ⇒ -4.0

(floor 3.5)             ⇒ 3.0
(ceiling 3.5)           ⇒ 4.0
(truncate 3.5)          ⇒ 3.0
(round 3.5)             ⇒ 4.0

(round 7/2)             ⇒ 4
(round 7)               ⇒ 7

(floor +inf.0)          ⇒ +inf.0
(ceiling -inf.0)        ⇒ -inf.0
(round +nan.0)          ⇒ +nan.0
Procedure: rationalize x1 x2

The rationalize procedure returns a number object representing the simplest rational number differing from x1 by no more than x2.

A rational number r_1 is simpler than another rational number r_2 if r_1 = p_1/q_1 and r_2 = p_2/q_2 (in lowest terms) and |p_1| <= |p_2| and |q_1| <= |q_2|. Thus 3/5 is simpler than 4/7.

Although not all rationals are comparable in this ordering (consider 2/7 and 3/5) any interval contains a rational number that is simpler than every other rational number in that interval (the simpler 2/5 lies between 2/7 and 3/5).

Note that 0 = 0/1 is the simplest rational of all.

(rationalize (exact .3) 1/10)   ⇒ 1/3
(rationalize .3 1/10)           ⇒ #i1/3 ; approximately

(rationalize +inf.0 3)          ⇒ +inf.0
(rationalize +inf.0 +inf.0)     ⇒ +nan.0
(rationalize 3 +inf.0)          ⇒ 0.0

The first two examples hold only in implementations whose inexact real number objects have sufficient precision.


Next: , Previous: , Up: baselib math ops   [Index]