Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Comunicados
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Math evaluation puzzler

TommyCee
Principiante
1.696 Visualizações

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 Solução
DavidWhite
Contribuidor valorado II
1.696 Visualizações

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.

Ver solução na publicação original

5 Respostas
mecej4
Colaborador honorário III
1.696 Visualizações

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!

DavidWhite
Contribuidor valorado II
1.697 Visualizações

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.
TommyCee
Principiante
1.696 Visualizações

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.

mecej4
Colaborador honorário III
1.696 Visualizações

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.

TommyCee
Principiante
1.696 Visualizações

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

Responder