当前位置: C语言 -- 标准库 -- <math.h> -- quantized128

quantized128函数


概要:
#include <math.h>
#ifdef __STDC_IEC_60559_DFP__
    _Decimal128 quantized128(_Decimal128 x, _Decimal128 y);
#endif 

描述:

该函数在可能的情况下计算出具有参数x的值、参数y的量子指数的值。

如果量子指数增加,结果应正确舍入;如果结果与参数x的值不同,则会引发FE_INEXACT浮点异常。

如果量子指数减小,并且结果的有效数字位数超过了对应类型所能表示的范围,结果为非数值,引发FE_INVALID浮点异常,并发生域错误。

如果其中一个或者两个操作数都是非数值,结果是非数值;否则如果只有一个操作数是无穷大,结果是非数值,引发FE_INVALID浮点异常,并发生域错误。

如果两个操作数都是无穷大,结果为DEC_INIFINITY,符号与参数x相同,并转换为函数的返回类型。

该函数不会引发FE_OVERFLOW浮点异常和FE_UNDERFLOW浮点异常。


参数:
_Decimal128 x

参数为_Decimal128类型的浮点数。

_Decimal128 y

参数为_Decimal128类型的浮点数。


返回值:

函数返回值的数值与参数x相同(除非发生舍入。),量子指数与参数y相同。


范例:
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 
/*函数quantized128范例*/

#include <math.h>
#include <stdio.h>

int main(void)
{
    #if __STDC_VERSION_MATH_H__ >= 202311L && defined(__STDC_IEC_60559_DFP__)
        _Decimal128 x = 1.0dl;
        _Decimal128 y = 1.23dl;
        _Decimal128 z = 1.00dl;
        
        _Decimal128 quant = quantized128(x, y);
        
        if(quant == z)
        {
            puts("The two values are equal.");
        }
        else
        {
            puts("The two values are different.");
        }
        
        if(samequantumd128(quant, z))
        {
            puts("The two quantum exponents are the same.");
        }
        else
        {
            puts("The two quantum exponents are different.");
        }
    #else
        puts("The implementation does not support quantized128 function.");
    #endif

    return 0;
}

可能输出:

The two values are equal.

The two quantum exponents are the same.

(注:编译时实现还未支持quantized128函数。)


相关内容:
quantized32 _Decimal32类型的返回值具有参数x的值、参数y的量子指数的函数。
quantized64 _Decimal64类型的返回值具有参数x的值、参数y的量子指数的函数。


主要参考资料:

1、ISO/IEC 9899:2024