値の操作と強制変換

Curl® 言語には、値の操作および強制変換を実行する以下のプリミティブがあります。
このセクションのほとんどのプロシージャは、2 番目の値をオプションとして返します。このようなプロシージャの作成および呼び出し方法の詳細は、「複数の値を返すプロシージャ」と「複数の戻り値があるプロシージャ」を参照してください。

floor

プリミティブ:floor
要約:floor (切り捨て) 関数
構文:{floor dividend[, divisor]}
引数:dividend - floor を取得する数値式。数値または数量を指定できます。
divisor - dividend を除算するためのオプションの値。数値または数量を指定できます。引数の既定値は 1 です。
戻り値:2 つの値: quotientremainder。最初の戻り値 quotientdivisorで割った dividend の数学的 floor (divisor で割った後の、dividend 以下の最大整数) です。2 番目の戻り値 remainder は数学的剰余で、これは元の値から指数 (quotient) と除数 (divisor) の積を引いた値を意味します。
説明:
2 つの数値の数学的 floor と剰余。最も簡単な形式では、この演算子は単に値の数学的 floor を取得します (負の無限大の方向に切り捨てます)。
quotient のデータ型は dividend/divisor の型と同じになり、remainder のデータ型は dividend rem divisor と同じになります。つまり、たとえば両方の引数が整数型であれば戻り値も整数型になるということを意味します。それ以外の場合、quotient には端数部分がありませんが戻り値は 1 つ以上の浮動小数点型になります。
数量の場合、dividenddivisor の単位が合意できるものである必要があり、この条件を満たす明示的な 2 番目の引数を指定しなければなりません。最初の戻り値 quotient は単位なしで、2 番目の戻り値 divisordividend と同じ単位になります。
例:

例: floor
|| Use the first return value:
The floor of 3.6 is {floor 3.6}.
{br}The floor of -3.6 is {floor -3.6}.
|| Use both return values
{value
    || Compute the number of weeks and days in a 365-day year
    let (weeks:int, days:int) = {floor 365, 7}
    {text {br}In a non-leap year there are {value weeks} weeks,
        with {value days} day(s) left over.}
}
注:{floor x, y}{floor x/y} の場合、quotient が同じでも remainderが異なる場合があります。
参照:API リファレンスの floor

ceiling

プリミティブ:ceiling
要約:ceiling (切り上げ) 関数
構文:{ceiling dividend[, divisor]}
引数:dividend - ceiling を取得する数値式。数値または数量を指定できます。
divisor - dividend を除算するためのオプションの値。数値または数量を指定できます。引数の既定値は 1 です。
戻り値:2 つの値: quotientremainder。最初の戻り値 quotientdivisor で割った dividend の数学的 ceiling (divisor で割った後の、dividend 以上の最少整数) です。2 番目の戻り値、remainder は数学的剰余で、元の値から指数 (quotient) と除数 (divisor) の積を引いた値を意味します。
説明:
2 つの数の指数の数学的 ceiling と剰余を返します。最も簡単な形式では、この演算子は単に値の数学的 ceiling を取得します (正の無限大の方向に切り上げます)。
quotient のデータ型は dividend/divisor の型と同じになり、remainder のデータ型は dividend rem divisor と同じになります。つまり、たとえば両方の引数が整数型であれば戻り値も整数型になるということを意味します。それ以外の場合、quotient には端数部分がありませんが戻り値は 1 つ以上の浮動小数点型になります。
数量の場合、dividenddivisor の単位が合意できるものである必要があり、この条件を満たす明示的な 2 番目の引数を指定しなければなりません。最初の戻り値 quotient は単位なしで、2 番目の戻り値 divisordividend と同じ単位になります。
例:

例: ceiling
|| Use the first return value
{br}The ceiling of 4.2 is {ceiling 4.2}.
{br}The ceiling of -4.2 is {ceiling -4.2}.
{br}The ceiling of -4.2 on division by 2 is {ceiling -4.2, 2}.

|| Use both return values to calculate the number of boxes
|| of 12 widgets to order if we need 100 widgets, and find
|| how many extras will be left over:
{value
    let (boxes:int, error:double) = {ceiling 100, 12}
    {text To obtain 100 widgets, we must order {value boxes} boxes.
        Our total will be off by {value error} (excess) widgets.}
},
                                                                                                                                                                                                                                        
注:{ceiling x, y}{ceiling x/y} の場合、quotient が同じでも remainderが異なる場合があります。
dividenddivisor は符号なしオペランドをサポートしません。
参照:API リファレンス ceiling

round

プリミティブ:round
要約:round (丸め) 関数
構文:{round dividend[, divisor]}
引数:dividend - round (丸め値) を取得する数値式。数値または数量を指定できます。
divisor - dividend を除算するためのオプションの値。数値または数量を指定できます。引数の既定値は 1 です。
戻り値:2 つの値: quotientremainder。最初の戻り値 quotientdivisor で割った dividend の丸め値です。2 番目の戻り値 remainder は数学的剰余で、元の値から指数 (quotient) と除数 (divisor) の積を引いた値を意味します。
説明:
2 つの数の指数の整数値と対応する剰余を返します。 最も簡単な形では 1 つの値を丸めるだけです。 quotient が 2 つの整数値から同距離の値になる場合、偶数値が優先されます。
quotient のデータ型は dividend/divisor の型と同じになり、remainder のデータ型は dividend rem divisor と同じになります。つまり、たとえば両方の引数が整数型であれば戻り値も整数型になるということを意味します。それ以外の場合、quotient には端数部分がありませんが戻り値は 1 つ以上の浮動小数点型になります。
数量の場合、dividenddivisor の単位が合意できるものである必要があり、この条件を満たす明示的な 2 番目の引数を指定しなければなりません。最初の戻り値 quotient は単位なしで、2 番目の戻り値 divisordividend と同じ単位になります。
例:

例: round
|| Use the first return value
The rounded value of 3.5 is {round 3.5}.
{br}The rounded value of 4.5 is {round 4.5}.
{br}The rounded value of -4.5 is {round -4.5}.
{br}The rounded value of -4.51 is {round -4.51}.

|| Use both return values to round the number 103.675
|| to the nearest .01, and finding the error:
{value
    let (approx:double, error:double) = {round 103.675, .01}
    {text The rounded approximation is {value approx/100}, with an
        error of {value error}.}
}
注:{round x, y}{round x/y} の場合、quotient が同じでも remainderが異なる場合があります。
dividenddivisor は符号なしオペランドをサポートしません。
参照:API リファレンスの round

truncate

プリミティブ:truncate
要約:truncate (切り捨て) 関数
構文:{truncate dividend[, divisor]}
引数:dividend - truncate (切り捨て値) を取得する数値式。数値または数量を指定できます。
divisor - dividend を除算するためのオプションの値。数値または数量を指定できます。引数の既定値は 1 です。
戻り値:2 つの値: quotientremainder。最初の戻り値 quotient は、divisor で割った dividend の切り捨て値 (divisor で割った dividend と同じ符号を持つ値で、divisor で割った後の、dividend の絶対値以下の最大整数である絶対値) です。2 番目の戻り値 remainder は数学的剰余で、元の値から指数 (quotient) と除数 (divisor) の積を引いた値を意味します。
説明:
2 つの数の商を整数値に切り捨てた値と、対応する剰余を返します。最も簡単な形式では、演算子は 0 に近い整数値に値を切り捨てます。
quotient のデータ型は dividend/divisor の型と同じになり、remainder のデータ型は dividend rem divisor と同じになります。つまり、たとえば両方の引数が整数型であれば戻り値も整数型になるということを意味します。それ以外の場合、quotient には端数部分がありませんが戻り値は 1 つ以上の浮動小数点型になります。
数量の場合、dividenddivisor の単位が合意できるものである必要があり、この条件を満たす明示的な 2 番目の引数を指定しなければなりません。最初の戻り値 quotient は単位なしで、2 番目の戻り値 divisordividend と同じ単位になります。
例:

例: truncate
The truncated value of 3.9 is {truncate 3.9}.
{br}The truncated value of -3.9 is {truncate -3.9}.

|| Find the number of decimal digits needed to represent a positive
|| integer:
{define-proc {num-digits n}:int
    {return {truncate {log10 n}} + 1}
}
You need {num-digits 999} digits to represent the number 999,
{br} {num-digits 1000} digits to represent the number 1000,
{br} and {num-digits 1001} digits to represent the number 1001.
注:{truncate x, y}{truncate x/y} の場合、quotient が同じでも remainderが異なる場合があります。
参照:API リファレンスの truncate

nextafter

プリミティブ:nextafter
要約:次に隣接する浮動小数点値
構文:{nextafter dividend, direction}
引数:dividend - 数値式。引数には、隣接する次の数値を得るための数値を指定します。
direction - 数値式。戻り値は direction の方向に隣接する数値になります。
戻り値:direction に近い側の dividend に隣接する浮動小数点値。dividenddirection が全く等しい場合、dividend が返されます。
説明:両方の引数が単精度の浮動小数点型であれば結果もそれと同じになり、その他の場合は倍精度になります。引数の 1 つが数量型の場合、両方の引数は同じ単位で表現できなければなりません。返される値も同じ単位で表現可能になります。
例:

例: nextafter
The next float before {format "%.8f", 5.0f} is
{format "%.8f", {nextafter 5.0f, 4.9f}}.
{br} The next float after {format "%.8f", 5.0f} is
{format "%.8f", {nextafter 5.0f, 5.1f}}.
{br} The next double before {format "%.20f", 5.0} is
{format "%.20f", {nextafter 5.0, 4.0}}.
{br} The next double after {format "%.20f", 5.0} is
{format "%.20f", {nextafter 5.0, 6.0}}.
参照:API リファレンスの nextafter