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

the meaning of max(some_value,0._r8)*j/0

changks888
Beginner
2,528 Views
Hi all, I confused by the following statement:

max(some_value,0._r8)*j/0

What is the result and its meaning?

thx,
Michael
0 Kudos
1 Solution
Steven_L_Intel1
Employee
2,528 Views

Well, the result of this would probably be a NaN (Not a Number), since dividing by zero is undefined. the 0._r8 means a real value of 0 with the real kind defined by the parameter constant r8, whatever that happens to be. The result of max(some_value,0._r8) is either 0 or some_value, whichever is larger. That is then multiplied by variable j and divided by zero (or multiplied by j/0 - either meaning is acceptable though both are undefined.)

View solution in original post

0 Kudos
6 Replies
Steven_L_Intel1
Employee
2,529 Views

Well, the result of this would probably be a NaN (Not a Number), since dividing by zero is undefined. the 0._r8 means a real value of 0 with the real kind defined by the parameter constant r8, whatever that happens to be. The result of max(some_value,0._r8) is either 0 or some_value, whichever is larger. That is then multiplied by variable j and divided by zero (or multiplied by j/0 - either meaning is acceptable though both are undefined.)
0 Kudos
TimP
Honored Contributor III
2,528 Views
meaningless, without context. I'd even suspect a typo. Some codes have had O and 0 mixups for decades, tending to disprove any claim to QA checking.
0 Kudos
changks888
Beginner
2,528 Views

Well, the result of this would probably be a NaN (Not a Number), since dividing by zero is undefined. the 0._r8 means a real value of 0 with the real kind defined by the parameter constant r8, whatever that happens to be. The result of max(some_value,0._r8) is either 0 or some_value, whichever is larger. That is then multiplied by variable j and divided by zero (or multiplied by j/0 - either meaning is acceptable though both are undefined.)
Hi Steve,
I'm dealing with some climatic modeling. I have a controller to change the calculation. Let's say the controller is defined as "psn" and "psn" can be 1 or 0 depends on the input file.a, b, c and d will be calculated from the other equations.

--- example 1 ---
ans_1 = max(a-b,0._r8)*c/(a+2.*b)*psn + c*(1.-c3psn)

if psn = 1, then
ans_1 =max(a-b,0._r8)*c/(a+2.*b)

if psn = 0, then
ans_1 = max(a-b,0._r8)*c/(a+2.*b)*psn + c
--- end of example 1 ---

--- example 2 ---
ans_2 = a * ((b -c)/(b+d))*psn + a*(1.-psn)

if psn = 1, then
ans_2 = a * ((b -c)/(b+d))

if psn = 0, then
ans_2 = a * ((b -c)/(b+d))*psn + a
--- end of example 2 ---


If that is the case, how can I make it works? Do you have good advice? These code was wrote under Compaq Fortran Compiler. I'm not sure I have to rewrite all of formulas... (that will be very painful!!)

Thank you Steve.








0 Kudos
Steven_L_Intel1
Employee
2,528 Views

If it compiled with Compaq Visual Fortran, it should compile with Intel Visual Fortran. I don't know your application or its algorithms, so I can't tell you what to do. I will comment that expressions such as

max(a-b,0._r8)*c/(a+2.*b)*psn + c

could certainly be a problem if psn was zero and the compiler chose to interpret this as:

max(a-b,0._r8)*(c/((a+2.*b)*psn)) + c

Perhaps what is meant instead is:

max(a-b,0._r8)*c/(a+2.*b)*(psn + c)

which would do the add before the multiply. Again, I don't know your application so can't promise that this is the correct interpretation. In a Fortran expression, multiplication and division have "higher precedence" than addition and subtraction and, in the absence of parentheses to direct otherwise, are done first. The compiler can choose to do the multiply and divide in either order.

First, understand what the basis of the expression is and then you can determine how to correctly write it in Fortran.
0 Kudos
Hirchert__Kurt_W
New Contributor II
2,528 Views

Hi Steve,
I'm dealing with some climatic modeling. I have a controller to change the calculation. Let's say the controller is defined as "psn" and "psn" can be 1 or 0 depends on the input file.a, b, c and d will be calculated from the other equations.

--- example 1 ---
ans_1 = max(a-b,0._r8)*c/(a+2.*b)*psn + c*(1.-c3psn)

if psn = 1, then
ans_1 =max(a-b,0._r8)*c/(a+2.*b)

if psn = 0, then
ans_1 = max(a-b,0._r8)*c/(a+2.*b)*psn + c
--- end of example 1 ---


You appear to be incorrectly parsing the expression.

ans_1 = max(a-b,0._r8)*c/(a+2.*b)*psn + c*(1.-psn)

is equivalent to

ans_1 = (max(a-b,0._r8)*c/(a+2.*b))*psn + c*(1.-psn)

not

ans_1 = max(a-b,0._r8)*c/((a+2.*b)*psn) + c*(1.-psn)

so when psn = 0, ans_1 = c.

-Kurt


0 Kudos
JVanB
Valued Contributor II
2,528 Views
DIM(a,b)

0 Kudos
Reply