Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
长双溢出,但值小于最大可表示值 我试图用C++来计算一个系列。 该系列为: (给那些想知道的人)_C++_Infinity_Long Double - Fatal编程技术网

长双溢出,但值小于最大可表示值 我试图用C++来计算一个系列。 该系列为: (给那些想知道的人)

长双溢出,但值小于最大可表示值 我试图用C++来计算一个系列。 该系列为: (给那些想知道的人),c++,infinity,long-double,C++,Infinity,Long Double,我的代码如下: #include <iostream> #include <fstream> #include <cmath> // exp #include <iomanip> //setprecision, setw #include <limits> //numeric_limits (http://en.cppreference.com/w/cpp/types/numeric_limits) long double Smi

我的代码如下:

#include <iostream>
#include <fstream>
#include <cmath> // exp 
#include <iomanip> //setprecision, setw 
#include <limits> //numeric_limits (http://en.cppreference.com/w/cpp/types/numeric_limits)

long double SminOneCenter(long double gamma)
{
    using std::endl; using std::cout;
    long double result=0.0l;
    for (long double k = 1; k < 1000 ; k++)
    {   
            if(isinf(pow(1.0l+pow(gamma,k),6.0l/4.0l)))
            {   
                    cout << "infinity for reached for gamma equals:   " << gamma <<  "value of k:  " << k ; 
                    cout << "maximum allowed:   " <<  std::numeric_limits<long double>::max()<< endl;
                    break;
            }   

                    // CAS PAIR: -1^n = 1
                    if ((int)k%2 == 0)
                    {   
                            result += pow(4.0l*pow(gamma,k),3.0l/4.0l) /(pow(1+pow(gamma,k)),6.0l/4.0l);
                    }   
                    // CAS IMPAIR:-1^n = -1
                    else if ((int)k%2!=0)
                    {   
                            result -= pow(4.0l*pow(gamma,k),3.0l/4.0l) /(pow(1+pow(gamma,k)),6.0l/4.0l);

                            //if (!isinf(pow(k,2.0l)*zeta/2.0l))
                    }   
                    //              cout << result << endl;
    }    


    return 1.0l + 2.0l*result;
}
#包括
#包括
#包括//exp
#包括//setprecision,setw
#包括//数值限制(http://en.cppreference.com/w/cpp/types/numeric_limits)
长双中心(长双伽马)
{
使用std::endl;使用std::cout;
长双结果=0.0l;
用于(长双k=1;k<1000;k++)
{   
如果(isinf(功率(1.0升+功率(伽马,k),6.0升/4.0升)))
{   

cout如果要提供
长双精度
参数,则需要使用
powl
而不是
pow

当前,您在
pow
调用中遇到了
numeric\u limits::max()

作为替代,考虑使用<代码> STD::POW 具有适当的重载。


Reference

如果要提供
长双参数,则需要使用
powl
而不是
pow

当前,您在
pow
调用中遇到了
numeric\u limits::max()

作为替代,考虑使用<代码> STD::POW 具有适当的重载。


参考资料

首先:您知道计算机上的浮点运算会导致复合舍入错误(参见示例)第二,您是否尝试过在调试器中逐行逐行地检查代码?是的,我知道浮点运算不安全,但这不是问题所在,请参见下文。首先:您确实知道计算机上的浮点运算会导致复杂的舍入错误?(参见示例)第二,你有没有试过在调试器中逐行逐行地检查你的代码?是的,我知道浮点数学不能避免错误,但这不是问题所在,见下文。pow不是应该被重载很长时间吗?你的链接是针对
。OP使用的
是重载的:@NathanOliver但是有什么魔力呢在不使用名称空间std;
?(除非OP的代码中省略了这一点)的情况下,
pow
被称为不合格这里有些不太合理的地方…好吧,似乎是这样。调用std::pow和std::isinf不会触发if,除非长双精度溢出。非常感谢您的帮助作为最后一点,始终在系列中首先添加较小的浮点项。长双精度不应该重载pow吗?您的链接是f或者
。OP使用的
是重载的:@NathanOliver但是有什么魔法可以让
pow
在不使用命名空间std的情况下被称为unqualified;
?(除非OP的代码中省略了这一点,也就是说)这里有些不太合理的地方…好吧,似乎是这样。调用std::pow和std::isinf不会触发if,除非长双精度溢出。非常感谢您的帮助。作为最后一点,始终在系列中首先添加较小的浮点项。