% Function FLOOR, FFLOOR, CEILING, FCEILING, TRUNCATE, FTRUNCATE, ROUND, FROUND
Function FLOOR, FFLOOR, CEILING, FCEILING,
TRUNCATE, FTRUNCATE, ROUND, FROUND
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
number - 実数
divisor - ゼロではない実数。デフォルトは整数の1。
quotient -
floor, ceiling, truncate, roundは整数。
ffloor, fceiling, ftruncate, froundは浮動小数。
remainder - 実数
これらの関数はnumberをdivisorによって割り算し、 quotientとremainderを次の式になるよう返却します。
quotient*divisor+remainder=number
quotientは常に数学的な整数としてあらわされます。
数学的な整数の1より大きい数になる可能性があるとき
(例えばremainderがゼロではないとき)、
丸めと切り捨ては次のような処理ごとに依存します。
これら全ての関数は、numberに型の変換処理が行われます。
xとyが両方とも整数のときremainderは整数であり、 xとyが両方とも有理数のときremainderは有理数であり、 xとyのどちらかが浮動小数のときは浮動小数です。
ffloor, fceiling, ftruncate, froundは、
次に示す方法で引数間で違っている型を扱います。
もしnumberが浮動小数でdivisorが浮動小数の大きな形式ではないとき、
最初の結果はnumberと同じ型の浮動小数です。
それ以外のときは最初の結果は伝染の規則にしたがって型が決定されます。
12.1.1.2. 数値演算の伝染をご確認ください。
(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
なし。
なし。
なし。
なし。
numberだけが与えられたとき、2つの結果は正確に決まっており、 2つの結果の数学的な合計がnumberの数学的な値と常に等しいようになります。
(function number divisor)と(function (/ number divisor))は
(ただしfunctionはfloor, ceiling, truncate, round,
ffloor, fceiling, ftruncate, froundのどれか)、
最初の値は同じですが、
しかし2番目の値であるあまりは違っています。
例えば、
(floor 5 2) => 2, 1
(floor (/ 5 2)) => 2, 1/2
もしroundと似たようなものの、
しかし常に切り上げか切り捨て(偶数の整数に近い方向に行うのではなく)
のような効果を行いたいときは、
数学的な商が正確に2つの整数の半分であるとき、
プログラマーは(floor (+ x 1/2))か(ceiling (- x 1/2))のような
実行を考える必要があります。