Is this a Rounding Error or ?

Is this a Rounding Error or ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 1st */
double Eff = 1.7;
cout <<"Eff= "<< Eff << endl;

int Eff_i = Eff * 1000.0;
cout <<"Eff_i= "<< Eff_i << endl;

double Eff_d = (double)Eff_i / 10.0;
cout <<"Eff_d= "<< Eff_d << endl << endl;

/* 2nd */
double Eff_ = 1.7;
cout <<"Eff_= "<< Eff_ << endl;

int Eff_i_ = Eff_ * 1000000.0;
cout <<"Eff_i_= "<< Eff_i_ << endl;

double Eff_d_ = (double)Eff_i_ / 10.0;
cout <<"Eff_d_= "<< Eff_d_ << endl;


Console output

Eff= 1.7
Eff_i= 1699
Eff_d= 169.9

Eff_= 1.7
Eff_i_= 1699999
Eff_d_= 170000

Last edited on
I tried your code with MinGW and VS compilers and I got 170 and 170000. What compiler are you using? and what system? did you try casting with static_cast?
Code::Blocks 10.05
MinGW compiler
XP sp3

and yes i did try static_cast<double>(Eff_i), with same results

Now i tryed with Release settings and it works fine.
I think its IDE giving me the problems :)
Il post there to see what i get
Last edited on
That's really weird!!!

What's the result of this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 1st */
double Eff = 1.7;
cout <<"Eff= "<< Eff << endl;

int Eff_i = Eff * static_cast<int>(1000.0);
cout <<"Eff_i= "<< Eff_i << endl;

double Eff_d = static_cast<double>(Eff_i) / 10.0;
cout <<"Eff_d= "<< Eff_d << endl << endl;

/* 2nd */
double Eff_ = 1.7;
cout <<"Eff_= "<< Eff_ << endl;

int Eff_i_ = Eff_ * 1000000.0;
cout <<"Eff_i_= "<< Eff_i_ << endl;

double Eff_d_ = (double)Eff_i_ / 10.0;
cout <<"Eff_d_= "<< Eff_d_ << endl;
It's not really "error", just "rounding".

Specifically, your compiler appears to be a rounding the results of the multiplying 1.6999999999999999555910790149937383830547332763671875 by 1000.0 and 1.6999999999999999555910790149937383830547332763671875 by 1000000.0 down/to zero instead of the more common rounding to the nearest valid double.

test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cfenv>
#include <iomanip>
#pragma STDC_FENV_ACCESS ON
using namespace std;
int main()
{
    std::cout << "Default: \n";
    double Eff = 1.7;
    cout <<"Eff= "<< Eff << endl;

    int Eff_i = Eff * 1000.0;
    cout <<"Eff_i= "<< Eff_i << endl;

    double Eff_d = (double)Eff_i / 10.0;
    cout <<"Eff_d= "<< Eff_d << endl << endl;

    std::cout << "Down: \n";

    fesetround(FE_TOWARDZERO);
    double Eff_ = 1.7;
    cout <<"Eff= "<< Eff_ << endl;

    int Eff_i_ = Eff_ * 1000.0;
    cout <<"Eff_i= "<< Eff_i_ << endl;

    double Eff_d_ = (double)Eff_i_ / 10.0;
    cout <<"Eff_d= "<< Eff_d_ << endl << endl;
}
Default: 
Eff= 1.7
Eff_i= 1700
Eff_d= 170

Down: 
Eff= 1.7
Eff_i= 1699
Eff_d= 169.9


As for the second output being 170000, try std::setprecision(100) and you'll see that Eff_d_ is actually 169999.89999999999417923390865325927734375. That's just the default output format.
Last edited on
@TheDestroyer
Same results.

@Cubbi
Well isnt rounding happen when the number is to big to fit recever format ?
and
1.6999999999999999555910790149937383830547332763671875 how did u get it?
Eff is 1.7, 1.7 * 1000.0 should come 1700


btw im kinda new in programing. maeby i changed some settings that i shouldnt. :)


but were it did gues that i need rounding.(that troubles me)
1.7 * 1000.0 = 1700.0 and not 1699.....
Last edited on
Eff is not 1.7 because 1.7 is not a valid value for a double.

Rounding happens, yes, and the value that's actually stored is 7656119366529843 x 2-52 or 1.6999999999999999555910790149937383830547332763671875. The default precision of std::cout is 6, so it is presented as "1.7" for output only.
Last edited on
i didnt know that -Tankyou-@Cubbi

im still looking on Code::Blocks forum is there is another post related to this one,
and if i dont find one il post one.
Because this happends only on Debug and not on Relese.

kinda mind bugling that it stores something else, instead of what i expected to be there. :|


im gona use it like this atm as it doese the job.
later il find out a nicer way
1
2
3
4
5
6
/* 1st */
double Eff = 1.764646;
cout <<"Eff= "<< Eff << endl;

double Eff_d = ( (int)(Eff * 10.0) / 10.0 );
cout <<"Eff_d= "<< Eff_d << endl << endl;


Eff= 1.764646
Eff_d= 1.7



Ty again.
Last edited on
Topic archived. No new replies allowed.