Thread: Rounding errors

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    48

    Rounding errors

    I'm doing a conversion from Fortran 90 to C++ and I have run into a small (or large, I'm not sure) problem. In the Fortran there are some calculations and they use the REAL data type, which is equivalent to double. My problem is that the Fortran seems to do these calculations a bit more precisely because I lose some data to rounding errors (or so it seems when I output the values after the calculations) in the C++ version of the code. Im wondering if there is a way to force the C code to keep all the data to the 10th decimal point or if its just the output that is messing it up. The reason I don't think it is the output statements messing up is because at the end of a few functions I have a variable that is about .7 off from the Fortran equivalent. Any help would be appreciated, and I would post some code, but it would be a lot to go through, so I'm not sure what code would be helpful.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you still get those round off errors if you use long double instead of double?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It could just be an issue with how you are outputting the values. The output of floating point values defaults to 6 decimal places unless you specify otherwise by using either the setprecision stream manipulator or the stream's own precision function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    48
    But the thing is that the outputted values don't go all the way out to six decimal points. I haven't tried long doubles, but REAL in Fortran is only 32 bit so that shouldn't matter.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The order of computations in your algorithm will also affect your results (I'm sure that's mentioned somewhere in Salem's link, but anyway...), so if you switched things around and have a mathematically equivalent algorithm, it may produce different results.

    And, make sure that it is printing correctly.
    Code:
    std::cout.precision(10);
    ... As hk_mp5kpdw suggests.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    48
    It may be the order the calculations are done, but most of them are pretty straight forward and linear (line 3 depends on lines 1 and 2 executing and line 2 depends on line 1). And like I said, the last value for the calculation ends up being about .7 off of what the Fortran says it should be, so I don't think its an output format error.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I doubt were going to get much further unless you post some code. Post the lines of code responsible for the calculation, include the initial values of your variables, include the line of code responsible for any output, and finally include what the fortran output is.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    48
    A lot of these problems occur in arrays
    for example:
    Code:
    y_min_mu_pi_D[j] += y_min_mu_pi[i];
    the values in y_min_mu_pi_D end up being slightly off from the fortran values. And there are about 200 of them, so I don't really think you want me to post all of the values

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ends up being about .7 off
    I doubt if .7 off could be attributed to floating point representation.

    Also if, as you state, a Fortran REAL is really 32 bits, then this would be equivalent to a float, not a double. So you might try using a float.

    >(line 3 depends on lines 1 and 2 executing and line 2 depends on line 1).
    It doesn't sound like a lot of code to post. But we would need the Fortran also.

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    48
    Well the three lines was just an example, there is a lot of code and its spread out all over...
    I was told never to use floats by another computer science major, he said they're not standardized like doubles are..
    And the reason I say the .7 off comes from some rounding error is because all of the errors build up, the calculation for the last variable relies on a bunch of variables before it.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I was told never to use floats by another computer science major
    But you stated a Fortran REAL is 32 bits.

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    48
    Ok, I see your point. I'll try that and see how it goes. Thanks

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I believe in Fortran you can do something like:
    Code:
    REAL*8
    if you want an 8 byte real. This would be the equivalent of a double, so you might check the code.

    Assuming you can change the Fortran, one idea might be to add some print statements within the calculation loops for both C++ and Fortran, to see where the difference is occurring. You could print to a file to make it easy to compare.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Another thought: Do you have any real data to compare these values to? That is, can you see if the FORTRAN code appears to be closer, farther, or about the same distance away from reality?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  3. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM