Thread: Problems with double division when using gcc's -ffast-math option

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    101

    Problems with double division when using gcc's -ffast-math option

    I am getting incorrect results with division when using -ffast-math. Here is what I have to work with:

    Code:
    int64_t duration;
    int64_t time_units;
    double frame_rate;
    int64_t frame_count;
    
    //get frame count
    frame_count = ((double)duration/(double)time_units) * (double)frame_rate;
    This works great as long as -ffast-math isn't enabled.

    Is there another way I can calculate frame_count that uses only integers? Perhaps multiplying everything by 1000 (to increase precision) and then do the calculations using integers?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    -ffast-math enables this...

    -freciprocal-math
    Allow the reciprocal of a value to be used instead of dividing by the value if this enables optimizations. For example x / y can be replaced with x * (1/y) which is useful if (1/y) is subject to common subexpression elimination. Note that this loses precision and increases the number of flops operating on the value.
    If the answers are wrong because of the loss in precision, you need to just leave it off.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    101
    Quote Originally Posted by whiteflags View Post
    If the answers are wrong because of the loss in precision, you need to just leave it off.
    Thanks. I will leave it off. I just tried compiling without it, and it didn't make much of a difference in performance. It did when I had complex loops, but I replaced them with other code a while back so it looks like I don't need it. Good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double division error (I get -0.0000)
    By dorakura in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 07:33 PM
  2. Math Division Problem
    By cookie in forum C Programming
    Replies: 7
    Last Post: 06-14-2007, 10:34 AM
  3. Math problems, I think...
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2007, 06:29 AM
  4. Casting unsigned long division to a double?
    By smoothdogg00 in forum C Programming
    Replies: 5
    Last Post: 12-22-2006, 09:22 AM
  5. help my math (int/double)
    By major_small in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2003, 02:36 PM