xthat has a value depends on the variables
b. The most obvious choice is to write an
if-elsestatement to determine what
if(a > b) then
x = a
x = b
But this seems a bit cumbersome, particularly when you look at a more compressed form using the ternary operator ? as in C (among other languages):
x = a > b ? a : b;
I used to be jealous of C because they had the inline-if and I believed that Fortran did not. Fortunately for us, Fortran added this inline-if into the 1995 standard. Unfortunately, they chose the most random of intrinsic function names I have ever heard of:
merge. It is used quite like the above:
x = merge(a,b,a>b)
variable = merge(value if true, value if false, condition)
The following is a snippet from a simulation I wrote that involved a Monte Carlo sampling of positions in cylindrical coordinates. R,P,Z are the rho-phi-zed coordinate-arrays.
R(i) = sqrt(rTemp)*rScale
P(i) = 2.*pi*pTemp
Z(i) = merge(zTemp*zScale,-zTemp*zScale, cTemp > 0.5)
A couple notes on the above:
(1) Fortran uses 1-index, rather than C's 0-index--which leads me to the aside: quickly count to 10. Did you start at 0? Fortran can actually start at 0, if you want. You would have to define the array to start there by using
(2) Fortran uses curved braces for the array index, whereas C uses the square braces for the same thing.
(3) The variable
piis not an intrinsic (why not!?) and needs to be declared beforehand; I find the easiest option is using
pi=acos(-1.0), though others may suggest
pi=4*atan(1.0)(which is fine, but it seems to me that one operation is better/faster than two).
(4) The intrinsic subroutine
random_number(variable)was used because it is superior to the intrinsic function
rand(). The former has a period of 2123 (~1037) while the latter has a period of (I think) 232 (~109).
Comments always welcome.