c++ - Error in displaying a double value in a PlainText widget -
[update]
this code:
double tmp=0; double a=4.87149e+07; double b=10; double c=5.29e-06; ... double sum=0; ofstream m2; m2.open("c:/capture/m2.doc"); (i=0;i<916;i++) { (int j=0;j<916;j++) { tmp=a*b*c; sum= sum+tmp; m2 << sum << "\n"; } }
'm having above:
when print sum, gives me nan result.
when omitted c sum formula, gives me non nan result. thus, believe compiler pretending it's +oo/-oo multiplication (a big, , c small), not case!
i,m dealing important data that.
i want print result @ end in textedit:
plaintextedit->setplaintext(qstring::number(sum));
as reach half of count loop (458), , j (458), values of sum become equal -1.#ind
how handle that?
running following:
#include <iostream> #include <fstream> #include <qdebug> int main(int argc, char *argv[]) { double tmp=0; double a=4.87149e+07; double b=10; double c=5.29e-06; double sum=0; std::ofstream m2; m2.open("m2.doc"); (int i=0;i<916;i++) { (int j=0;j<916;j++) { tmp=a*b*c; sum= sum+tmp; m2 << sum << "\n"; } qdebug() << sum; } }
yields: (lines 1-10 of m2.doc)
2577.02 5154.04 7731.05 10308.1 12885.1 15462.1 18039.1 20616.1 23193.2 25770.2
... (lines 209760-209770) 209764 = 458 , j = 458
5.40555e+008 5.40558e+008 5.4056e+008 5.40563e+008 5.40566e+008 5.40568e+008 5.40571e+008 5.40573e+008 5.40576e+008 5.40579e+008 5.40581e+008
... (lines 839047 839056 aka end)
2.16224e+009 2.16224e+009 2.16224e+009 2.16225e+009 2.16225e+009 2.16225e+009 2.16225e+009 2.16226e+009 2.16226e+009 2.16226e+009
i opened file in notepad++ because ms word struggles such long files. tmp
optimized compiler act constant: 2577.02
, or if use printf can see additional precision pretty easily: 2577.018210
. qdebug()
line yields similar answers terminal.
double
can keep track of huge range of values:
#include <limits> //... printf("%g\n", std::numeric_limits<double>::min()); printf("%g\n", std::numeric_limits<double>::max());
prints out
2.22507e-308 1.79769e+308
with setup.
i hope helps.
Comments
Post a Comment