Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28445 Discussions

Strange bug with different method of adding

Wee_Beng_T_
Beginner
336 Views

Hi,

I have a CFD code whereby in a subroutine, I have to calculate the angle theta_x_rad. I calculate it using:

theta_x_rad = - 0.5d0*omega_r_max*(tmp_time2 - (0.5d0*dt_r/Pi)*sin(2.d0*Pi*(tmp_time2 + 0.5d0*dt_r)/dt_r)) + k_r1 (= 1.57, given by print)
    
theta_x_rad = - theta_x_rad + Pi

I got the correct ans, which is ~1.569. Now if I do it in this way:

theta_x_rad = 0.5d0*omega_r_max*(tmp_time2 - (0.5d0*dt_r/Pi)*sin(2.d0*Pi*(tmp_time2 + 0.5d0*dt_r)/dt_r)) + k_r1 + Pi (no minus sign in front)

I got 4.71. It seems that the 1st term above is calculated as 1.57 instead of -1.57

I tried different intel versions, debug, opt etc, but the ans are the same.

Did I miss out on something?

Thanks

 

 

 

 

 

 

0 Kudos
6 Replies
mecej4
Honored Contributor III
336 Views

Check the sign of the term k_r1 in the line on which you commented "no minus sign".

0 Kudos
Wee_Beng_T_
Beginner
336 Views

Hi mecej4,

It's a predefined variable and it's ~ 1.57. I checked too.

 

0 Kudos
mecej4
Honored Contributor III
336 Views

Here is the clarification of my comment, which was about the expression and not the declaration or definition of k_r1.

You wrote:

     theta_x_rad = 0.5d0*omega_r_max*(tmp_time2 - (0.5d0*dt_r/Pi)*sin(2.d0*Pi*(tmp_time2 + 0.5d0*dt_r)/dt_r)) + k_r1 + Pi (no minus sign in front)

The + sign attached to k_r1 is not consistent with what you get from combining the two preceding assignment statements in your original post. Algebra rules say that you should have written - k_r1 instead.

I suspect that the leading term is negligibly small. If so, the three statements in questions could be written as follows:

    theta_x_rad =  + k_r1 

    theta_x_rad = - theta_x_rad + Pi

    theta_x_rad = + k_r1 + Pi  <<<=====  Error: the first '+' should be '-'

If k_r1 is close to π/ 2 (≈ 1.57), the first two statements taken together yield the result π/2, whereas the third, because of the erroneous sign, yields 3 π / 2 (≈ 4.71). 

In other words, the culprit is an error in algebraic simplification and not anything involving programming, Fortran, or compiler options.

0 Kudos
Wee_Beng_T_
Beginner
336 Views

Hmm, still don't really understand. I'll check with some other values and get back to you.

Thanks
 

0 Kudos
mecej4
Honored Contributor III
336 Views

Well, let's try something different. The two sequential assignments

   x = -a + b

   x = -x + c

cause x to have the final value a - b + c

Your third assignment (the alternative), on the other hand, causes x to have the value a + b + c. The second term in this "result" has an incorrect sign.

That is the crux of the matter, and you really do not need to try different values, etc.; simply recollect and apply the rules of algebra.

0 Kudos
Wee_Beng_T_
Beginner
336 Views

Ops ... I saw it!

It should be:

 

x = a - b + c, but I gave it x = a + b +c !

It's so embarrassing. I just can't see it intially... Thanks again!

 

 

0 Kudos
Reply