c++ - Error while comparing a int and a double -


i'm trying make simple function count number of digits. have written function work i'm stuck on 1 :

// number want count digits after decimal long double d=33.56; // going modify d wan't keep original value long double dtemp=d; // v store part after decimal , e part before long double v, e;  v=modfl(dtemp,&e);  // j number of digit after decimal int j=0; while(dtemp!=e) {     j++;     dtemp*=10;     v=modfl(dtemp,&e);     std::cout<<"d = "<<dtemp<<" e = "<<e<<" v = "<<v<<std::endl; } std::cout<<"j = "<<j<<std::endl; 

the output :

d = 335.6 e = 335 v = 0.6 d = 3356 e = 3356 v = 2.27374e-13 d = 33560 e = 33560 v = 2.27374e-12 d = 335600 e = 335600 v = 2.27374e-11 d = 3.356e+06 e = 3.356e+06 v = 2.27374e-10 d = 3.356e+07 e = 3.356e+07 v = 2.27374e-09 d = 3.356e+08 e = 3.356e+08 v = 2.27301e-08 d = 3.356e+09 e = 3.356e+09 v = 2.27243e-07 d = 3.356e+10 e = 3.356e+10 v = 2.27243e-06 d = 3.356e+11 e = 3.356e+11 v = 2.27094e-05 d = 3.356e+12 e = 3.356e+12 v = 0.000226974 d = 3.356e+13 e = 3.356e+13 v = 0.00226974 d = 3.356e+14 e = 3.356e+14 v = 0.0227051 d = 3.356e+15 e = 3.356e+15 v = 0.227051 d = 3.356e+16 e = 3.356e+16 v = 0.269531 d = 3.356e+17 e = 3.356e+17 v = 0.6875 d = 3.356e+18 e = 3.356e+18 v = 0 j = 17 

but, if @ line 2 of output, have :

d = 3356 e = 3356 v = 2.27374e-1

so, dtemp equal e , while loop still continues.

what tried :

  • i though due d not being able store number big needed. why used long double

    • i tried different types of variables end same result.

it not practice check equation/not equation of float/double/long double numbers directly. suggest use this

not equal:

while (abs(dtemp - e) > 1e-12) { ... } 

equal:

while (abs(dtemp - e) < 1e-12) { ... } 

the "tiny" number depends on type of not-integer number (floating point or fixed precision real). m-gregoire's comment using std::numeric_limits<double>::epsilon() may sound good, 1 may face problem again. 33.56 cannot calculated finite sum of positive and/or negative powers of 2, cannot stored! there itsy-bitsy differences. internally stored this: 33.56000000000000227373675443232059478759766 (i printed in , in similar result). may set "tiny" difference value "proper" value, high enough disable internal float format problem.

you may use approach. std::ostringstream used manipulator setprecision setting precision "proper" number convert number string , count digits in string. not simple.

i checked value '33.56' in well. see example code:

#include <iostream> #include <iomanip>  int main() {   float df = 33.56;   double dd = 33.56;   long double dld = 33.56;   std::cout << std::setprecision(50) << df << std::endl;   std::cout << std::setprecision(50) << dd << std::endl;   std::cout << std::setprecision(50) << dld << std::endl; } 

the output is:

33.560001373291015625 33.56000000000000227373675443232059478759766 33.56000000000000227373675443232059478759766 

so in case of float 1e-5 used gap value, double 1e-14 seems right value in case.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -