extern float quantizef (float x, float y); extern double quantize (double x, double y); extern long double quantizel (long double x, long double y); extern long_long_double quantizell (long_long_double x, long_long_double y); extern __float80 quantizew (__float80 x, __float80 y); extern __float128 quantizeq (__float128 x, __float128 y); extern decimal_float quantizedf (decimal_float x, decimal_float y); extern decimal_double quantized (decimal_double x, decimal_double y); extern decimal_long_double quantizedl (decimal_long_double x, decimal_long_double y); extern decimal_long_long_double quantizedll (decimal_long_long_double x, decimal_long_long_double y);
NB: Functions with prototypes containing underscores in type names may be available only with certain extended compilers.
This function is particularly useful for doing fixed-point arithmetic with decimal floating-point arithmetic, since it provides a way to ensure a common scale.
Another important application of this function is conversion to integers: quantized(x, 1DD) returns the whole number nearest to x, in floating-point format, assuming the default rounding mode.
Since the purpose of the second argument is just to communicate a power of ten, its particular coefficient digits are not significant. For decimal use, trailing zero digits are commonly used, so that 1.00 means quantize to two decimal digits. For the binary companions, the normalized floating-point format requires that the argument be a fractional value chosen somewhat away from an exact power of ten to avoid off-by-one errors in the inexact determination of the power of ten.
If both arguments are Infinity, return the first value. If either argument is a NaN, return that NaN. If one argument is Infinity, return a quiet NaN. If the operation would require more digits than are available, return a quiet NaN.
decimal quantized(+1.DD, +1.00DD) -> +1.00DD quantized(+100.DD, +1.00DD) -> +100.00DD quantized(+0.125DD, +1.00DD) -> +0.12DD quantized(+0.135DD, +1.00DD) -> +0.14DD quantized(+0.145DD, +1.00DD) -> +0.14DD quantized(+NaN(0x1234), +1.00DD) -> +NaN(0x1234) quantized(+100.DD, +NaN(0x1234)) -> +NaN(0x1234) quantized(+NaN(0x1234), +NaN(0x5678)) -> +NaN(0x1234) quantized(+Infinity, -Infinity) -> +Infinity quantized(-Infinity, +Infinity) -> -Infinity quantized(+100.00DD, -Infinity) -> +NaN quantized(-Infinity, +100.00DD) -> +NaN quantizedf(+1234567DD, +1.DD) -> +1234567 quantizedf(+1234567DD, +1.0DD) -> +NaN
binary quantize(+1, +0.02) -> +1. quantize(+100, +0.02) -> +100. quantizef(+0.125, +0.02) -> +0.12 quantizef(+0.135, +0.02) -> +0.14000000000000001 quantizef(+0.145, +0.02) -> +0.14000000000000001 quantize(NaN(0x1234), +0.02) -> NaN(0x1234) quantize(+100, NaN(0x1234)) -> NaN(0x1234) quantize(NaN(0x1234), NaN(0x5678)) -> NaN(0x1234) quantize(+Infinity, -Infinity) -> +Infinity quantize(-Infinity, +Infinity) -> -Infinity quantize(+100, -Infinity) -> NaN quantize(-Infinity, +100) -> NaN quantize(+1234567890123456., +1.) -> +1234567890123456. quantize(+1234567890123456., +0.02) -> NaN
The parenthesized hexadecimal values following some of the NaNs are payloads stored in the significand bits to identify which NaN is returned.
The third through fifth examples show that quantization can cause rounding, here with the default round-to-nearest-even rule.