简体   繁体   English

是否有任何指数函数在math.h标头中返回IEEE_FLOAT64?

[英]Is there any exponential function which returns IEEE_FLOAT64 in math.h header?

I want to use exponential function which returns IEEE_FLOAT64 value 我想使用返回IEEE_FLOAT64值的指数函数

Currently I am using expf function, but still I am getting lots of warnings. 目前,我正在使用expf函数,但仍然收到很多警告。

value = IEEEPosOne - (IEEE_FLOAT64)expf(value1);

From man 3 exp : man 3 exp

NAME
       exp, expf, expl - base-e exponential function

SYNOPSIS
       #include <math.h>

       double exp(double x);
       float expf(float x);
       long double expl(long double x);

       Link with -lm.

So just use exp() . 因此,只需使用exp()

//c++
#include <cmath>
double x = 7.0;//float64
auto y = std::exp(x);//exp(float64);

C++ standard provides appropriate overloads. C ++标准提供了适当的重载。 No need to reflect operand type in function name. 无需在函数名称中反映操作数类型。

This does answer does not apply to C only for C++ 此答案并不仅适用于C ++的C语言
For C see @iBug's answer. 对于C,请参见@iBug的答案。

The C++ standard does not require an implementation to use the IEEE standard. C ++标准不需要使用IEEE标准的实现。 Though this is usually the easiest floating point implementation to use as the chips are relatively standard in modern machines. 尽管这通常是最容易使用的浮点实现,因为芯片在现代机器中是相对标准的。

The standard provides a way to check using std::numeric_limits . 该标准提供了一种使用std::numeric_limits进行检查的方法。

So if it is a requirement then you should validate. 因此,如果有要求,则应进行验证。

#include <limits>
#include <iostream>

int main()
{
    static_assert(sizeof(double) == 64);
    static_assert(std::numeric_limits<double>::is_iec559());

    // If the above compiles your double is IEEE 64 bit value.
    // Or IEEE_754 compliant https://en.wikipedia.org/wiki/IEEE_754_revision
}

Now that you have established you are using IEEE values you can look at the cmath header to see that the functions there all take and return a double value. 现在,你已经建立您使用IEEE值,你可以看看CMATH头看到的功能也都采用并返回一个double值。

Note: You should note that Windows machines (usually) use an 80 bit floating point register (not 64). 注意:您应注意,Windows计算机通常使用80位浮点寄存器(而非64)。 So things can get super whaky if you need strict compliance. 因此,如果您需要严格遵守规定,事情可能会变得非常混乱。

Note: Do NOT use : 注意: 请勿使用

  • expf() for float, expf()用于浮点,
  • expl() for long double expl()长双

These are for C library users where the language does not do the correct type checking. 这些是针对C语言库用户的,该语言不进行正确的类型检查。 In C++ the language uses overloading to use the correct version of the function. 在C ++中,该语言使用重载来使用函数的正确版本。 If you look at the standard for exp : 如果您查看exp的标准:

Defined in header <cmath> 定义于头文件<cmath>

* float       exp( float arg );
* double      exp( double arg );
* long double exp( long double arg );
* double      exp( Integral arg );

The above are all in the standard namespace. 以上都是标准名称空间中的内容。

std::cout << std::exp(100000.1) << "\n";

Notice that exp() can take any floating point type float , double or long double and generate the appropriate result type. 请注意, exp()可以采用浮点类型floatdoublelong double并生成适当的结果类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM