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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NaN problem

Status
Not open for further replies.

beism

Technical User
Joined
Nov 16, 2002
Messages
1
Location
GB
Hi all,

I have a nasty problem with floating point arithmetic.
Let E be a double variable which is assigned a value using an expression like:
E=A+B*((i<j) ? f(C1)/(1-f(C2)) : 0);
where A,B,C1,C2 are double variables, f is a function returning a double and i,j are integers.

The problem I have is that the value assigned to E is 'nan' but when I use
printf(&quot;E=%f\n&quot;,A+B*f(C1)/(1-f(C2)));
the actual value of E (which is in the range [-5,5]) appears correctly! It seems that there is a problem during the assignment.

Any help will be welcome,

Mike
 
Instead of using the triadic operator, try an if statement.

if (i < j)
E = A + B * f(C1)/(1-f(C2));
else
E = A;

if this still causes problems, work out the divisor first. Some compilers are just plain weird. QuickC used to give these problems all the time.
 
I guess U have to use
E=A+B*((i<j) ? (f(C1)/(1-f(C2))) : 0);
instead of
E=A+B*((i<j) ? f(C1)/(1-f(C2)) : 0);

please see that if works....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top