Thread: casting double to singed int

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    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));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. subfunctions returning different types together
    By muzihc in forum C Programming
    Replies: 38
    Last Post: 10-08-2008, 01:19 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  5. Replies: 2
    Last Post: 03-24-2006, 08:36 PM

Tags for this Thread