|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
jetcheve
Joined: 23 Aug 2006 Posts: 23
|
Posted: Wed Sep 06, 2006 9:47 am Post subject: Math Errors? |
|
|
I have a piece of software originally written as a standalone program that I have turned into a dll using FTN95. It now recieves variables from a namelist file created by visual basic, and runs some computations. The standalone program is fine, it's been working for years. The variables pass in correctly, I've tested that. However, when the computations start, I'm getting ridiculous math errors in equations that I never changed, ones that I literally copied and pasted, save for some syntax changes such as line continuations. Take this block of code for example:
Code: | Do 90, INC = 1, INC1
RAD = DEG / 57.296
MAINVALS(INDEX,1) = DEG
PINVALS(INDEX,1) = DEG
XHVALS(INDEX,1) = DEG
SHOEVALS(INDEX,1) = DEG
SN = R * SIN(RAD)
CO = SQRT((L ** 2 - (R * SIN(RAD)) ** 2))
Call SUB530(ANG, SN, CO)
FEE = ANG
FT1 = NRL1(INC) * SIN(RAD) * (1 + COS(RAD) / ((L / R) ** 2 - SIN(RAD) ** 2) ** .5)
FT2 = NRL2(INC) * SIN(RAD) * (1 + COS(RAD) / ((L / R) ** 2 - SIN(RAD) ** 2) ** .5)
FR1 = NRL1(INC) * (COS(RAD) - (SIN(RAD)) ** 2 / ((L / R) ** 2 - SIN(RAD) ** 2) ** .5) - 0.0000284 * R * N ** 2 * WRT
FR2 = NRL2(INC) * (COS(RAD) - (SIN(RAD)) ** 2 / ((L / R) ** 2 - SIN(RAD) ** 2) ** .5) - 0.0000284 * R * N ** 2 * WRT
FRC1 = NRL1(INC) * (COS(RAD) - SIN(RAD) ** 2 / ((L / R) ** 2 - SIN(RAD) ** 2) ** .5) - 0.0000284 * R * N * N * (WRT - CP)
FRC2 = NRL2(INC) * (COS(RAD) - SIN(RAD) ** 2 / ((L / R) ** 2 - SIN(RAD) ** 2) ** .5) - 0.0000284 * R * N * N * (WRT - CP)
RA = (90.0 - FEE - DEG) / 57.296
FC1Y = -FRC1 * COS(RA) + FT1 * SIN(RA)
FC1X = -FRC1 * SIN(RA) - FT1 * COS(RA)
FC1 = SQRT((FC1X ** 2 + FC1Y ** 2))
FC2Y = -FRC2 * COS(RA) + FT2 * SIN(RA)
FC2X = -FRC2 * SIN(RA) - FT2 * COS(RA)
FC2 = SQRT((FC2X ** 2 + FC2Y ** 2))
SN = FC1Y
CO = FC1X |
Starting with RAD = 0 or DEG = 90, obviously sin and cosine should be 0 and 1 respectively. Thus, as you'd expect, FT1 and FT2 are also 0. Now comes the first error... When FR1 is calculated using NRL1(1) = 15467 and rad still 0, you should get -15470.3 according to my hand calculations, maybe I made a mistake maybe I didn't, there are still others, but either way, the computer returns -18404.2. However, the most interesting one I have found is at the point where it calculate FC1Y or FC1X etc... Upon performing this calculation the first time, you have RA = 90 - FEE - DEG ( take my word that FEE and DEG are both, and correctly so, 90) and thus equal to 0. thus RA is PI/2 and cos is 0 and sin is 1. Therefore, since FT1 is 0 from earlier, and cos is 0, FC1Y should be -FRC1*0 + 0 * 1 which is 0. But to my surprise, I've discovered that 0+0 is now .107879. Now, if this were a smaller error, I could attribute it to rounding error, truncation, etc... But it's a fairly significant difference, and it's by far not the only occurance of it. What |
|
Back to top |
|
|
jetcheve
Joined: 23 Aug 2006 Posts: 23
|
Posted: Wed Sep 06, 2006 10:00 am Post subject: Math Errors? |
|
|
do you think is going on? |
|
Back to top |
|
|
brucebowler Guest
|
Posted: Wed Sep 06, 2006 2:14 pm Post subject: Math Errors? |
|
|
A couple questions...
What do the declarations of everything look like? Do you have an IMPLICIT NONE in there to make sure you've type'd all variables?
Using either the debugger or "diagnostic writes", where does the calculation start differing from what you calculate by hand?
Why do you use **0.5 rather than sqrt()? They may not do *exactly* the same thing (Paul can probably answer that better than I can)
|
|
Back to top |
|
|
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Thu Sep 07, 2006 1:24 am Post subject: Math Errors? |
|
|
This may be trivial, but like Bruce's comment on **0.5 I always thought that it was better programming practice to also avoid using **2 as well _________________ John Horspool
Roshaz Software Ltd.
Gloucestershire |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7927 Location: Salford, UK
|
Posted: Thu Sep 07, 2006 2:38 am Post subject: Math Errors? |
|
|
John
Have you tested the code using /CHECKMATE?
Is it possible that line continuations have gone wrong?
For example, with fixed format Fortran it is possible to have a valid equation but part of it is actually commented out.
If you can post a very short working example of the problem then fault should be easy to find. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|