Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Math evaluation puzzler

TommyCee
Beginner
1,366 Views

In the following snippet, Term1 is part of a larger equation:

real(4) :: Term1, T, Twb, Tref
T = Twb
Tref = 273.15

Term1 = ((2501.6 - 2.3263*(Twb-Tref)) / 2501.6 + 1.8577*(T-Tref) - 4.184*(Twb-Tref))

 

With the temps equal to each other, this term should go to 1.  In fact, by inspecting the form of the equation, it's obvious that it should go to 1.

However, in IVF - when I pause in debug - Term1 is evaluated as -63.62723

Here's the puzzler part:

When I sweep out the numerator w/ my cursor, it's evaluated as 2436.999

When I sweep out the denominator w/ my cursor, it's evaluated as 2436.999

Thus, Term1 should be evaluated very close to 1.0

But it's not!

What the deuce?


 

0 Kudos
1 Solution
DavidWhite
Valued Contributor II
1,366 Views

in other words, your equation for Term1 should be

Term1 = (2501.6 - 2.3263*(Twb-Tref)) / (2501.6 + 1.8577*(T-Tref) - 4.184*(Twb-Tref))
 
You had an extra left parenthesis at the beginning of the equation, which should have been at the beginning of the denominator.

View solution in original post

0 Kudos
5 Replies
mecej4
Honored Contributor III
1,366 Views

Parentheses in the eye of the beholder... Let us rewrite your expression using different pairs of parentheses, braces and brackets.

[{2501.6 - 2.3263*(Twb-Tref)} / 2501.6 + 1.8577*(T-Tref) - 4.184*(Twb-Tref)]

We can remove the [..] since they are the first and last characters, and are therefore superfluous. Thus, what you actually have is X1 + X2, where

X1 = {2501.6 - 2.3263*(Twb-Tref)} / 2501.6

and

X2 = 1.8577*(T-Tref) - 4.184*(Twb-Tref)

Even when T = Twb, X1 need not equal 1 - X2. In fact, if T > Tref, and Twb = T, X1 is less than 1 and X2 is negative, so the sum of X1 and X2 is less than 1 by a larger amount.

The denominator has only the number 2501.6 and nothing else. Everything else is "upstairs". To see that, rewrite the whole expression in built-up display form rather than single-line. When you select just "2501.6 + 1.8577" with your mouse, I doubt that the debugger's expression evaluator knows that the first number is in the denominator and the second in the numerator!

0 Kudos
DavidWhite
Valued Contributor II
1,367 Views

in other words, your equation for Term1 should be

Term1 = (2501.6 - 2.3263*(Twb-Tref)) / (2501.6 + 1.8577*(T-Tref) - 4.184*(Twb-Tref))
 
You had an extra left parenthesis at the beginning of the equation, which should have been at the beginning of the denominator.
0 Kudos
TommyCee
Beginner
1,366 Views

The outer parentheses were retained when I lifted it from the larger equation.  Let's remove them, as you suggest:

Term1 = 2501.6 - 2.3263*(Twb-Tref) / 2501.6 + 1.8577*(T-Tref) - 4.184*(Twb-Tref)

Now it got even crazier.  The left-hand side (via debug pause) is 2436.999

The right-hand side is 2436.999

Term1 is evaluates as:  2436.973

This just doesn't make sense.

0 Kudos
mecej4
Honored Contributor III
1,366 Views

Now you have mangled the expression further. There were two right parentheses before '/' in #1 and #3, and I had ')}' in #2. By removing one of the parentheses, you changed the expression. Elementary algebra books give fail-safe recipes for processing expressions with nested parentheses and brackets (e.g., G. Chrystal, http://djm.cc/library/Algebra_Elementary_Text-Book_Part_I_Chrystal_edited.pdf ).

David gave you the correct expression. Please try it.

0 Kudos
TommyCee
Beginner
1,366 Views

This was subtle.  I have it now.  Thank you.

0 Kudos
Reply