Converting double to integer

This code:
1
2
3
4
5
6
cout.setf(ios_base::fixed, ios_base::floatfield);
double a=5.67;
double b = a*10000;
int c = a*10000;
int d = (double)(b);
printf("a: %lf b: %lf c: %d d: %d", a, b, c, d);

Is giving me this output
 
a: 5.670000 b: 56700.000000 c: 56699 d: 56700


Why is c 56699 and not 56700?

Thanks
Not to mention that floating point arithmetic sucks.
A quick google returned this result:
http://www.atalasoft.com/cs/blogs/stevehawley/archive/2008/01/25/never-forget-floating-point-sucks.aspx
Which basically says the same thing. Floating point arithmetic is imprecise and should be avoided (IMO). In fact, Linus Torvalds has virtually banned it from Linux kernel code.
The obligatory link on the subject of floating point arithmetic is:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

While it is true that Linux does not use floating point arithmetic in the kernel, I think
the larger reason for this is to avoid the need to save/restore state of the FPU
every time a context switch occurs.

maybe it should be:
int c = ( (int)a ) * 10000;
That would be 50000 =P
@Disch
oh sorry,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    cout.setf(ios_base::fixed, ios_base::floatfield);
    double a=5.67;
    double b = a*10000;
    int c = (int)(a*10000);
    int d = (double)(b);
    c = (int) b;
    printf("a: %lf b: %lf c: %d d: %d", a, b, c, d);
    return 0;
}
a: 5.670000 b: 56700.000000 c: 56700 d: 56700

Topic archived. No new replies allowed.