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

a question on the atan2 function in fortran

jsenent
Beginner
1,785 Views
Hi,

I have been playing around with the atan2 function in the Visual and Intel compilers and I run into this problem:

write(*,*) cos(atan2(1d0,0d0))

gives: 6.123031769111886E-017 when it should be 0d0

but
write(*,*) cosd(atan2d(1d0,0d0))

gives: 0d0

For some reason I always thought that atan2 was more accurate than atan2d but in any case I think that there is an issue with the implementation of the atan2 function.

I have tried the same in C with the math.h library and I get the correct answer, however in some implementations of Matlab I have seen the same problem.

Although you might think that 1d-17 is small, I'm multiplying it by huge numbers so I running into problems.

Is there any setting in the compiler that can solve this problem?

Thanks!!!

Juan
0 Kudos
1 Reply
TimP
Honored Contributor III
1,785 Views
If you got your "correct" answer with a C compiler, you most likely were using a compilation mode which invoked the built-in x87 functions with extra precision. CVF included a system call which would set x87 precision mode to 64 bits, in case that is what you want. I haven't tried it with ifort, to check whether it is still implemented, should you desire to forgo SSE2 optimizations. Microsoft has specified the default 53-bit precision mode for years.
Otherwise, you would have to give more specifics. C math.h does not specify a library; it only supplies the definitions, equivalent to those built in to Fortran.
An algorithm which intentionally magnifies uncertainties of the order of EPSILON(x) has to be questionable, at least without a lot of justification.
0 Kudos
Reply