- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Robert,
What is the value of Pi at the time of the error?
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 noneThese 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.
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]
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)

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page