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

This is a discussion on Problems with double division when using gcc's -ffast-math option within the C Programming forums, part of the General Programming Boards category; I am getting incorrect results with division when using -ffast-math. Here is what I have to work with: Code: int64_t ...

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

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    -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
    97
    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, 11:34 AM
  3. Math problems, I think...
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2007, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21