Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Math function not working in Fortran

zengsong
Beginner
1,942 Views

Hi,
I just run into this strange problem in my code. The cosine function continuously returns 1, while sine always give 0 for whatever inputs. I check that it happens after a certain number of iterations. Does anyone else see this case before? I put a checking line in different places in the code, and it starts to return warning message after a certain number of iterations:

if(sin(real(Pi/4.0d0))==0) write(1,*),'warning'

I worry whether other math functions, such as log and exp are also affected. My code was run in other cases before without encountering such strange problems. I assure all arrays are not overflowed and no errors was return during the test. Please give me some advice. All your help will be highly appreciated.

FYI: I run the code on Linux with the Intel compiler. I check that the some sin/cos functions used in this code give right values in a separated code.

Robert

0 Kudos
6 Replies
jimdempseyatthecove
Honored Contributor III
1,942 Views

Robert,

What is the value of Pi at the time of the error?

Jim Dempsey
0 Kudos
zengsong
Beginner
1,942 Views

Robert,

What is the value of Pi at the time of the error?

Jim Dempsey

Real number Pi is just 3.1415... defined at the beginning of the code. Pi is not changed in the program. The same checking sentence will not return an error at the beginning of the code, but it turns wrong in the iteration part after a while. Actually I check any arbitrary input for sine will return 0 in this case.

Robert

0 Kudos
Steven_L_Intel1
Employee
1,942 Views
Rather than explain what you think is the problem, please show a small but complete program that demonstrates the problem. My experience is that most of the time, when we are shown a single statement and a theory as to what is going wrong, it is actually something else based on code not shown.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,942 Views

Robert,

Literal interpretation of your response is "I did not check the value of Pi" and by inference, you did not specifically check thevalueof Piat the statement where, and more specifically when, use of Pi/4.0D as argument to standard intrinsic function produced invalid results.

Either input value of Pi is wrong (your program stomped on it) or the code/data within the intrinsic function is wrong (it used to be right, did your program stomp on it?), or the code/data near the offending statement was altered. Note this may also include the modification of theliteral value "4.0D".

By testing the value of Pi at the point of error, you would confirm (or not) if the value of Pi was altered. Testing the same 4.0D will be harder to do.

You have identified a location within your program produces bad results.

Examine all the possible causes. Do not assume you know what cannot be a possible cause.

When your test discovers the error, if necessary step back to scope of statement that made the call, then using the debugger, reset the Next Statement to the statement that produced the error (your debugger should have this capability). Examine Pi, verify it is indeed 3.1415.... If it is, look deeper, open a Dissassembly window. Step through assembly statements up to the sin (or call to sin function). What got calculated? did the divide 4.0D work as expected? What gets passed into the sin?

If nothing shows up, you can then step over the sin function and check the result again.

Now, if this time you get the right result then the case may be that your program JUMPED or RETURNED to the assembly code at or preceeding the sin/call to sin function, but at a position after where Pi/4.0D is properly calculated leaving trash (?0.0) as the calling argument. How you got here will be harder to determine. Hopefully examining the stack might lead to a discovery (include potential values representing last call that was poped from stack).


Jim Dempsey


0 Kudos
roddur
Beginner
1,942 Views
Robert,
Plz note that, if value of pi is not changing, you can define as a parameter, and the full line will give same result in each iteration, irrespective of the activity of what rest of the loop is doing.

And to assure yourself, this will always give the right ans!
[cpp]	implicit none
	real(4),parameter::pi=3.14159
	
	write(*,*) pi
	if(sin(real(Pi/4.0d0))==0) write(1,*),'warning' 
	write(*,*) sin(real(Pi/4.0d0))
	

	end
[/cpp]
These math functions are such basic functions that we cant dream of any such bug as you mentioned in here. As Steve suggested, plz reproduce your problem in a smaller code and put it here, so it will be easy to check whats going wrong.
0 Kudos
zengsong
Beginner
1,942 Views
Quoting - roddur
Robert,
Plz note that, if value of pi is not changing, you can define as a parameter, and the full line will give same result in each iteration, irrespective of the activity of what rest of the loop is doing.

And to assure yourself, this will always give the right ans!
[cpp]	implicit none
real(4),parameter::pi=3.14159

write(*,*) pi
if(sin(real(Pi/4.0d0))==0) write(1,*),'warning'
write(*,*) sin(real(Pi/4.0d0))


end
[/cpp]
These math functions are such basic functions that we cant dream of any such bug as you mentioned in here. As Steve suggested, plz reproduce your problem in a smaller code and put it here, so it will be easy to check whats going wrong.

This is where the code is not working. I use sin(Pi/4.0d0) just to confirm this. I find Geky(iii) returns 0 all the time because of sin function problem. Note Rphi (real*4) is a random number here. This part used to give correct answers. I am checking other possible causes now for this issue.

call random_number(Rtheta)
call random_number(Rphi)
Gekx(iii)=sqrt(1-((2.0d0*Rtheta-1)**2))*cos(2.0d0*pi*Rphi)
Geky(iii)=sqrt(1-((2.0d0*Rtheta-1)**2))*sin(2.0d0*pi*Rphi)

0 Kudos
Reply