ToC DocOverview CGDoc RelNotes Index PermutedIndex
Allegro CL ANSI Common Lisp 12 Numbers 12.2 Dictionary of Numbers

 12.2.19 floor, ffloor, ceiling, fceiling, truncate, ftruncate, round, fround Function

Syntax:
 floor number &optional divisor quotient, remainder ffloor number &optional divisor quotient, remainder ceiling number &optional divisor quotient, remainder fceiling number &optional divisor quotient, remainder truncate number &optional divisor quotient, remainder ftruncate number &optional divisor quotient, remainder round number &optional divisor quotient, remainder fround number &optional divisor quotient, remainder

Arguments and Values:
number - a real.

divisor - a non-zero real. The default is the integer 1.

quotient - for floor, ceiling, truncate, and round: an integer; for ffloor, fceiling, ftruncate, and fround: a float.

remainder - a real.

Description:
These functions divide number by divisor, returning a quotient and remainder, such that

quotient·divisor+remainder=number

The quotient always represents a mathematical integer. When more than one mathematical integer might be possible (i.e., when the remainder is not zero), the kind of rounding or truncation depends on the operator:

• floor, ffloor

floor and ffloor produce a quotient that has been truncated toward negative infinity; that is, the quotient represents the largest mathematical integer that is not larger than the mathematical quotient.

• ceiling, fceiling

ceiling and fceiling produce a quotient that has been truncated toward positive infinity; that is, the quotient represents the smallest mathematical integer that is not smaller than the mathematical result.

• truncate, ftruncate

truncate and ftruncate produce a quotient that has been truncated towards zero; that is, the quotient represents the mathematical integer of the same sign as the mathematical quotient, and that has the greatest integral magnitude not greater than that of the mathematical quotient.

• round, fround

round and fround produce a quotient that has been rounded to the nearest mathematical integer; if the mathematical quotient is exactly halfway between two integers, (that is, it has the form integer+1/2), then the quotient has been rounded to the even (divisible by two) integer.

All of these functions perform type conversion operations on numbers.

The remainder is an integer if both x and y are integers, is a rational if both x and y are rationals, and is a float if either x or y is a float.

ffloor, fceiling, ftruncate, and fround handle arguments of different types in the following way: If number is a float, and divisor is not a float of longer format, then the first result is a float of the same type as number. Otherwise, the first result is of the type determined by contagion rules; see Section 12.1.1.2 Contagion in Numeric Operations.

Examples:
``` (floor 3/2) 1, 1/2
(ceiling 3 2) 2, -1
(ffloor 3 2) 1.0, 1
(ffloor -4.7) -5.0, 0.3
(ffloor 3.5d0) 3.0d0, 0.5d0
(fceiling 3/2) 2.0, -1/2
(truncate 1) 1, 0
(truncate .5) 0, 0.5
(round .5) 0, 0.5
(ftruncate -7 2) -3.0, -1
(fround -7 2) -4.0, 1
(dolist (n '(2.6 2.5 2.4 0.7 0.3 -0.3 -0.7 -2.4 -2.5 -2.6))
(format t "~&~4,1@F ~2,' D ~2,' D ~2,' D ~2,' D"
n (floor n) (ceiling n) (truncate n) (round n))) +2.6  2  3  2  3 +2.5  2  3  2  2 +2.4  2  3  2  2 +0.7  0  1  0  1 +0.3  0  1  0  0 -0.3 -1  0  0  0 -0.7 -1  0  0 -1 -2.4 -3 -2 -2 -2 -2.5 -3 -2 -2 -2 -2.6 -3 -2 -2 -3 NIL
```

Notes:
When only number is given, the two results are exact; the mathematical sum of the two results is always equal to the mathematical value of number.

(function number divisor) and (function (/ number divisor)) (where function is any of one of floor, ceiling, ffloor, fceiling, truncate, round, ftruncate, and fround) return the same first value, but they return different remainders as the second value. For example:

``` (floor 5 2) 2, 1
(floor (/ 5 2)) 2, 1/2
```

If an effect is desired that is similar to round, but that always rounds up or down (rather than toward the nearest even integer) if the mathematical quotient is exactly halfway between two integers, the programmer should consider a construction such as (floor (+ x 1/2)) or (ceiling (- x 1/2)).

Allegro CL Implementation Details:
None. © Franz Inc. 1998-2019 - File last updated 02-07-2019
{"ft_lang_label":"__label__en","ft_lang_prob":0.78835225,"math_prob":0.98673743,"size":4181,"snap":"2021-04-2021-17","text_gpt3_token_len":1221,"char_repetition_ratio":0.18338521,"word_repetition_ratio":0.083687946,"special_character_ratio":0.2917962,"punctuation_ratio":0.17081448,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9944241,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-21T15:43:02Z\",\"WARC-Record-ID\":\"<urn:uuid:1e128281-b931-47e6-b1aa-2ff0848fd133>\",\"Content-Length\":\"14860\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:896c7edd-15cf-4139-8949-780825e09d98>\",\"WARC-Concurrent-To\":\"<urn:uuid:7707434f-d8f7-4f55-a15c-13dd13c0d694>\",\"WARC-IP-Address\":\"192.235.120.131\",\"WARC-Target-URI\":\"https://franz.com/support/documentation/current/ansicl/dictentr/floorffl.htm\",\"WARC-Payload-Digest\":\"sha1:7D6B3GJJVH5REDUDESYBYHJBCEFY4YQP\",\"WARC-Block-Digest\":\"sha1:LN2CZRPT4IKWYDRYOOY4YSTL3JNQFQQU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703524858.74_warc_CC-MAIN-20210121132407-20210121162407-00408.warc.gz\"}"}