# Thread: casting double to singed int

1. ## casting double to singed int

dear there,
with offset of type signed int, and both curprice and prevprice of type double. I compute the offset as this:
offset = (signed int) ((curprice - prevprice) * 10000);
the result is not consistent.
curprice offset
1.239 0
1.2391 0 <=====
1.2389 -2
1.2389 0
1.239 1

I also tried to cast it this way:
offset = (signed int)(curprice*10000) - (signed int)(prevprice * 10000);
and the result is not consistent neither.
1.2393 0
1.2393 0
1.2392 -1
1.2391 -1
1.2391 0
1.2391 0
1.2391 0
1.2389 -3 <=====

the curprice and prevprice are read from text file, which always has 4 digits after decimal point: 1.xxxx.
can anybody show me how to cast it? Thanks!

2. You probably need to post more of the code, like for example how you are printing it.

--
Mats

3. Your basic problem is one of expectations: you're expecting a floating point variable to hold your input values exactly, and round as you would if working with decimal arithmetic.

Floating point variables cannot represent any of the powers of 0.1 (0.01, 0.001), or quite a few multiples of those values, exactly. Adding values and rounding (as in your first example) or multiplying by a power of ten and then rounding (as in your second example) both tend to magnify the effects of rounding error.

There is no universal technique to work around this phenomenon: it is a fundamental property of floating point representations. Depending on your goal, you either need to accept the behaviour or constrain your situation (eg only work within a fixed range of values with a particular number of significant figures). In practice, it is usually only important when outputting the values (eg writing to a file), so most techniques amount to formatting output appropriately.

There are some techniques (eg store your values using a pair of integers, and represent as a fraction) - and keep track of the remainder when doing an integer division so you can manage your output. This doesn't work with irrational numbers (which can't be represented as ratio of two integers) though.

4. ## code attached

Code:
```#include <iostream>
#define P(x) cout << "# " << #x  << " : " << x << endl;
using namespace std;
int main(){
double p1 = 1.2389;
double p2 = 1.2391;
double p3 = 1.2390;
P(p1);P(p2);P(p3);
cout << "should be -2 and 1" << endl;
//casting method 1
P( (signed int) (p1*10000) - (signed int) (p2*10000));
P( (signed int) (p2*10000)- (signed int) (p3*10000));
//casting method 2
P((signed int) ((p1 - p2) * 10000));
P((signed int) ((p2 - p3) * 10000));
}```