首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::ldexp

Defined in header <cmath>

?

?

float ldexp( float x, int exp );

(1)

?

double ldexp( double x, int exp );

(2)

?

long double ldexp( long double x, int exp );

(3)

?

double ldexp( Integral x, int exp );

(4)

(since C++11)

1-3%29乘以浮点值。x按数字计算2升到exp权力。

4%29一组过载或接受任意参数的函数模板积分型等效于%282%29%28的参数转换为double29%。

参数

x

-

floating point value

exp

-

integer value

返回值

如果没有错误发生,x乘以2乘以exp%28x×2 exp

%29被返回。

如果溢出导致范围错误,±HUGE_VAL,,,±HUGE_VALF,或±HUGE_VALL会被归还。

如果由于下流发生范围错误,则返回舍入%29后的正确结果%28。

错误处理

错误按数学[医]错误处理...

如果实现支持ieee浮点算法%28IEC 60559%29,

  • 除非发生距离错误,FE_INEXACT永远不会提高%28,结果是精确的%29。
  • 除非发生距离错误,当前舍入模式被忽略
  • 如果x是±0,它被返回,未经修改
  • 如果x是±∞,它被返回,未经修改
  • 如果exp是0,那么x被返回,未修改
  • 如果x是南,南回来了

注记

关于二进制系统%28FLT_RADIX2%29,std::ldexp等于std::scalbn...

功能std::ldexp%28“负荷指数”%29及其对偶,std::frexp,可用于操纵浮点数的表示,而不需要直接的位操作。

在许多实现中,std::ldexp比乘法或除法的效率要低,使用算术运算符的乘幂为2。

二次

代码语言:javascript
复制
#include <iostream>
#include <cmath>
#include <cerrno>
#include <cstring>
#include <cfenv>
 
#pragma STDC FENV_ACCESS ON
int main()
{
    std::cout << "ldexp(7, -4) = " << std::ldexp(7, -4) << '\n'
              << "ldexp(1, -1074) = " << std::ldexp(1, -1074)
              << " (minimum positive subnormal double)\n"
              << "ldexp(nextafter(1,0), 1024) = "
              << std::ldexp(std::nextafter(1,0), 1024)
              << " (largest finite double)\n";
    // special values
    std::cout << "ldexp(-0, 10) = " << std::ldexp(-0.0, 10) << '\n'
              << "ldexp(-Inf, -1) = " << std::ldexp(-INFINITY, -1) << '\n';
    // error handling
    errno=0; std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "ldexp(1, 1024) = " << std::ldexp(1, 1024) << '\n';
    if(errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if(std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

二次

产出:

二次

代码语言:javascript
复制
ldexp(7, -4) = 0.4375
ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal double)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)
ldexp(-0, 10) = -0
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

二次

另见

frexp

decomposes a number into significand and a power of 2 (function)

scalbnscalbln (C++11)(C++11)

multiplies a number by FLT_RADIX raised to a power (function)

c ldexp文档

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com