Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unexpected floating point error 1

Status
Not open for further replies.

aioie2

Technical User
Oct 31, 2010
2
GB
Hello all,
I am a beginner programmer so please bear with me if I appear to be missing something obvious. I am using Fortran 95 and compiling with Nag 5.2.

I am attempting to evaluate the value of a polynomial at different values of x. The polynomial contains 'j' terms.

I have created 2 allocatable arrays, the first containing values of x and the second containing the exponents of a polynomial of x. i.e. [x1 x2 x3 ... xn] and [exponent1, exponent2, exponent3 ... exponentj]

The program then evaluates

x1^exponent1 + x1^exponent2 ... x1^exponentj
x2^exponent1 + x2^exponent2 ... x2^exponentj
.
.
.
xn^exponent1 + xn^exponent2 ... xn^exponentj

and puts the results in n number of arrays of size j

My problem is that because both of the arrays are double precision, if one of the x values is 0 then I get a floating point error. How do I overcome this? I cannot know in advance what the user input x values are, and x=0 will very probably be an input.

Thanks in advance. Apologies if my question strikes you as extremely basic. (I am an engineer not a programmer).
 
You speak about polynomial. But with a polynomial function, all exponents are integer values and one avoids the explicit use of the exponent 0 in assuming that x^0==1 for any x. With an integer exponent y, is is possible to compute x^y with x < 0.

If your exponents are real values, then you have to take care about x^y with x <= 0. This is not a FORTRAN problem by a mathematical one, x^y being equivalent to exp(y*log(x)), which has no solution for x <= 0.

So before computing x^y, you need to test x and there is no other solution.
 
FJacq, thank you so much for the help. I think I understand what you mean and I will begin implementing what you have said now.

Zero exponents are in fact possible inputs here because the terms in the equation will later be multiplied by an array of coefficients. (In other words there may be a constant in the equation).

for example 4x^3+3x^2+2x+0.5

I will check the exponent array for any zero values and replace the corresponding x terms in the array of [x^exponents] with a non-zero.

I think this should rectify the situation. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top