![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 41
| casting double to singed int 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! |
| patiobarbecue is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| You probably need to post more of the code, like for example how you are printing it. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Jun 2005
Posts: 1,343
| 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. |
| grumpy is offline | |
| | #4 |
| Registered User Join Date: Dec 2008
Posts: 41
| 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));
}
|
| patiobarbecue is offline | |
![]() |
| Tags |
| casting, numerical stability |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NEED HELP READING FILE and PRINTING | geoffr0 | C Programming | 4 | 04-16-2009 05:26 PM |
| subfunctions returning different types together | muzihc | C Programming | 38 | 10-08-2008 01:19 PM |
| Code review | Elysia | C++ Programming | 71 | 05-13-2008 09:42 PM |
| Conversion From C++ To C | dicon | C++ Programming | 2 | 06-10-2007 02:54 PM |
| Incorporating en-passant and Castling into a chess program | mac025 | C Programming | 2 | 03-24-2006 08:36 PM |