Thread: Getting a floating point exception

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Getting a floating point exception

    I'm getting a strange floating point exception. I have:
    unsigned long int a;
    unsigned long int b;

    a is assigned a value of 1356 at some point, and b is assigned a value of 1355 at another point.

    Computing a - b gives me a floating point exception. Casting to int doesn't help, and I'm not really sure WHY this is happening.

    Any ideas? Tried searching, but most info I could find is pretty specific.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    a-b where a and b are unsigned long int should NEVER give a floating point error. Are you sure you are looking at the right piece of code?

    And the values you indicate aren't even viable to cause an integer under/overflow.

    --
    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
    Mar 2008
    Posts
    5
    Yes, I'm sure. a - b was a loop condition, but I pulled this out before the loop and using my usual debugging method, printed both the values of a and b right before the subtraction. I printed something else after the subtraction, but all I get for output is what I have before the subtraction, and then a floating point exception.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What processor is this on? [Not that it REALLY makes any sense].

    What compiler are you using?

    --
    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.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Not sure about processor, and I'm remotely connecting to the machine. Using gcc.

    Looks like that actually wasn't it, but this is still not making any sense.

    I print the values of a and b just fine, but if I try to printf again IMMEDIATELY after this, even if it's just "hi", this is not included in the output. Now I'm completely lost...

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    So are we without any code.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    make sure that you either do
    Code:
    fprintf (stderr, "Hi\n");
    rather than just printf. Printf goes to stdout by default, which is buffered, so if you don't print a newline (or use stderr), the printf might well be reached, but you don't see the output! I've had this a couple of times. Forcing the output to flush will tell you for sure which part of code you're getting the floating point exception in.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Oh I can provide the code, it's just that it's rather large and I don't think anyone would want to bother looking at it. Here it is though: http://pastebin.com/d5baf9655

    The error seems to be happening in the select_wsclock() function somewhere.

    Thanks, I will try using fprintf.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Flushing helped a lot. I found the error - I was trying to cram 2 lines into one where I shouldn't have. Thanks Pete.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by SnertyStan View Post
    Flushing helped a lot. I found the error - I was trying to cram 2 lines into one where I shouldn't have. Thanks Pete.
    You ought to feel dirty and low when you type a printf() line that doesn't end in "\n"

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    You ought to feel dirty and low when you type a printf() line that doesn't end in "\n"
    But not ALL printf implementations flush on every newline either.

    --
    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.

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    But not ALL printf implementations flush on every newline either.
    So for total peace of mind you ought to write:
    Code:
    fprintf (stderr, "This is output\n"); fflush (stderr)
    Or is that OTT?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    So for total peace of mind you ought to write:
    Code:
    fprintf (stderr, "This is output\n"); fflush (stderr)
    Or is that OTT?

    QuantumPete
    I'm pretty sure that stderr is supposed to be completely unbuffered [unless you specifically change it with it using setvbuf() or whatever it's called]. stdout, on the other hand is not specified as to it's behaviour. It is OFTEN line buffered, but it can be "fully" buffered [whatever the name of that variant is called].

    --
    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.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    But not ALL printf implementations flush on every newline either.
    The standard is no guarantee of course, but it does state that when stdout is attached to a terminal device, it is line-buffered, meaning "\n" will cause a flush.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM