Thread: Stupid error with long and double types

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    9

    Stupid error with long and double types

    Hello everybody.
    I work in c++ for a long time, but this never happen to me before.
    I have a long variable, let's say
    long x;
    then i try to assign it with
    x=36*1000 (this is only an example. in fact, i use other variables)

    Guess what? after this line, x=-29536 !!! Why???

    Same if i use double type.

    I really don't understand.
    Last edited by qmadd; 08-22-2006 at 06:17 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Wow. You really will have to show some real code with that one. It's just... not possible. Unless your compiler is under some Fool's Day spell.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You're likely assigning a value greater than a signed long can hold and the value is wrapping around to the negatives, again. However, if that's the case I'm sure the value you assigned wasn't 36*1000 which isn't even close to large enough. I honestly don't understand why people think writing random crap and asking why their code doesn't work is a good idea.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    9
    Hello
    C'mon, i am not that stupid and i know what i am doing. The closest to the truth is the answer with the Fool's day spell.

    Trust me, i know what i am doing.
    Further details.
    i wanted to calculate the number of seconds from a given hour of the day. Crap, simple no?
    So...

    double number_seconds;
    number_seconds=t.ti_hour*3600;
    [......]

    Actualy, i was trying to calculate the number of seconds from a given number of years, but the code above is the part that gives errors.
    So first i run it and i see negative value for number_seconds. I thought it's too much for double type. Then i start debugging. When i get to the lines above, t.ti_hour is 14 in my case, and it shows in the debugger a negative value for number_seconds.
    Here is the interesting part: i create new program:

    void main()
    {
    double x;
    x=14*3600;
    printf("%.0f",x);
    }
    guess what? Negative value again.
    I tried
    x=360000;
    and it worked.
    Then again
    x=10*3600;
    and again negative value.
    Believe me or not, that was exactly what happened.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You gotta learn to use code tags man. Anyway, what is your compiler and OS?

    ...and while your at it, what does
    Code:
    printf("%d",14*3600);
    yield for you? Also, what about if you cast your result as a double before assigning it to a double?
    Last edited by SlyMaelstrom; 08-22-2006 at 07:05 AM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    9
    I am sorry for the tags.

    Code:
    printf("%d",14*3600);
    will this ever work? it will give negative value.

    The small program works now, but the big one keeps saying the same crap i have never seen.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ...and how did you make the small program work? Also, unless your system uses 16 bit signed integers, then that printf statement should be positive. As for using longs, there really is no reason a signed long should give a negative value for that as far as I can see.
    Sent from my iPadŽ

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Trust me, i know what i am doing.
    Saying this and then showing us void main doesn't inspire confidence in your statement.

    >will this ever work?
    Probably not, unless you start giving us the information we ask for. What compiler and version of that compler are you using? What OS are you using? Can you deign to give us a complete program that exhibits the problem rather than snippets?
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    9
    I just restarted the compiler.
    yeah, i know this sounds incredibble, but it's the truth.
    As for the big program, i have casted the result as a double, along with other variables, and it works too. Thanks
    But i still don't understand. What is the difference between

    Code:
    x=t.ti+hour * 3600
    (gives a negative value) and
    Code:
    x = (double) t.ti_hour *  3600
    (this one works).

    Thanks again for the help.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    9
    The compiler is Borland 3.1 and the OS is Windows Xp.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Perharps use correct code:
    Code:
    #include <iostream.h>
    
    int main(){
        double x;
        x=14*3600;
        printf("%.0f",x);
        cin.get();
    }
    Iostream.h or stdio.h are necessary for the program to recognize the printf function
    DO NOT USE VOID MAIN.
    This program exits before you can see anything, that's why I added cin.get().

    If you submit some code, make sure you don't make these MISTAKES again.
    Btw, this program returned 50400.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by qmadd
    But i still don't understand. What is the difference between

    Code:
    x=t.ti+hour * 3600
    (gives a negative value) and
    Code:
    x = (double) t.ti_hour *  3600
    (this one works).
    The difference depends on what your compiler and OS is, as we've asked you for. I may have given you the trick for solving it, but we're no closer to knowing why it really happened. I'd suspect your compiler doesn't like casting returns from operators as it would cast just a regular integer. Maybe your compiler's multiplication operator doesn't even return an integer when multiplying integers, but something that isn't properly assigned to a double. Who knows... you can give up here and hope you never run into it again or you can figure out what went wrong. We need info from you, first.
    Sent from my iPadŽ

  13. #13
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    DO NOT USE VOID MAIN WTF IS WRONG WITH YOU HAVENT YOU HEARD THIS BEFORE???
    Well, maybe nothing is wrong with him, he just maybe didn't knew, you shouldn't push him around like this... But anyways, someone in the past gave me a link to some webpage containing informations about these kind of C++ "rules", though I can't find it. I think it used to be posted somewhere here... It could be usefull to qmadd, too...

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by maxorator
    Iostream.h or stdio.h are necessary for the program to recognize the printf function
    On the topic of correct code, I didn't know the standard says iostream has to include stdio.h or define the printf() function, so I'm not sure your code, right there, is even as portable as the original void main code. I could be wrong, though.
    The compiler is Borland 3.1 and the OS is Windows Xp.
    I dispise Borland. They play by their own rules. Try a different compiler, perhaps Dev-C++ or Code::Blocks.
    Sent from my iPadŽ

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >yeah, i know this sounds incredibble, but it's the truth.
    It doesn't sound incredible at all, and we believe you.

    >What is the difference between
    Borland 3.1 is a 16-bit compiler. The very fact that it works on Windows XP astounds me, but I can easily see all kinds of undefined behavior from your overflowing arithmetic. Casting the expression to double forces the range to increase drastically, and suddenly you're no longer overflowing your data types. Simple. Well, sort of.

    >The compiler is Borland 3.1 and the OS is Windows Xp.
    You really should update your compiler. The one you're using is ancient.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  2. converting double to long int
    By gunshipolitico in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2005, 10:26 AM
  3. Replies: 15
    Last Post: 04-16-2003, 08:06 PM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 12:26 PM
  5. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 12:11 PM