(j3.2006) is MOD (INT_MIN, -1) legal?

Malcolm Cohen malcolm
Mon Oct 27 22:35:31 EDT 2008

Michael Ingrassia wrote:
>> MOD(A,P) is interpreted as A - INT ( A / P) * P
> That's not what I read in 13.7.80.  There's no explicit or implicit
> claim that the MOD intrinsic is "interpreted".  Rather, it has a value
> which is mathematically given as A-INT (A/P) * P.
That is exactly right. We had a similar discussion with ANINT, and the 
upshot was that we clarified the words in the standard to avoid giving 
the impression that it was just a name for a different expression.

Jim Xia wrote:
> OK, let me try to rephrase the question: in MOD(A,P) the standard 
> says: "Result Value. The value of the result is A?INT (A/P) * P."
> The question is: does this mean that any restrictions apply to 
> expression "A?INT (A/P) * P" also apply to intrinsic MOD()?
No; it does not *require* that the processor uses that formula, or any 
other, in the evaluation of MOD(x,y).

Never mind -HUGE(0)-1, MOD(1e20,1e10) is not *required* to overflow the 
INT intrinsic instead of producing the right answer.

Back to the original question:
> Say you have a minimum integer value, INT_MIN (e.g. -HUGE(1) - 1), 
> then is it a legal Fortran code to compute MOD(INT_MIN, -1)?
Depends on what you mean by "legal". It conforms to the standard on a 
processor that can calculate this, but on a processor that cannot 
calculate this it does not conform (as per Bill).

Aside: It certainly does not conform on a one's complement machine or a 
sign-magnitude machine, and might not conform if some kind of "bignums" 
package is being used.

Why do we care? Is this more than how many bits can dance on the head of 
an electron? We could certainly lengthen the description of MOD so that 
it does not give the impression of being a statement function, but that 
is going to have zero effect in the real world, where if the hardware 
supports it it is going to "work" and where the hardware does not 
support it it will not work.

.........................Malcolm Cohen.

More information about the J3 mailing list