Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c++ - 用c ++计算加倍的inf或零(c++ calculation of long doubles giving inf or zero)

so I created a program that takes in two long double numbers and some a particular form of calculation on them.

(因此,我创建了一个程序,该程序使用两个长双精度数并对其进行某种特殊形式的计算。)

but the issue is, the output for me is inf or in some compilers, 0.. So the error only occurs when I enter exponential values like say I enter 1.11224e+2121 1.11224e+2121 then I get inf or 0 but if I get something like 2.4 5.9 I get proper value.

(但问题是,对我来说输出是inf或在某些编译器中为0。因此,仅当我输入指数值(例如说我输入1.11224e + 2121 1.11224e + 2121)时才会出现错误,然后得到inf或0,但是如果我得到像2.4 5.9这样的值,我得到了适当的价值。)

How do I fix this?

(我该如何解决?)

here is my code

(这是我的代码)

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

int main()
{
    long double a,b,add=0,mean=0;
    cin>>a>>b;
    vector<long double> vector;
    vector.push_back(a);
    vector.push_back(b);
    mean=(vector[0]+vector[1])/2;
    for (int k = 0; k < 2; k++)
    {
        add= add+ pow((vector[k] - mean), 2);
    }
    cout<<add/2;

    return 0;
}
  ask by Jim Kelly translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...