The type of function I have in mind is something like:
q = safe_div(a,b,altv)
where q is set at a/b in the normal case, but if a/b overflows, then the
returned value is altv. Or something of that sort.
Or perhaps, someone can suggest a better way of handling situations when a
division (or other operation) may produce overflow (or other error), without
requiring the intervention of the user.
Thank you,
Julio
-- Chuck Crawford, Toronto
For some compilers, it may be faster to let the overflow occur
and capture it and return "Signed Infinity" , for example, as the result.
Indeed, the (vendor dependent) program, slightly modified
from the on-line documentation:
use dflib
Double Precision A,B,C
Integer(2) Status
A=1.D200
B=1.D-200
C=A/B
C DVF dependent check for overflow during division
CALL GETSTATUSFPQQ(Status)
IF (IAND(Status, FPSW$OVERFLOW ) .NE. 0) THEN
WRITE (*,*) 'Overflow occurred.'
END IF
WRITE(*,*) 'C=',C
STOP
END
when compiled with DVF (6.0b3) and run yields the output lines:
Overflow occurred.
C= Infinity
Salford FTN95 allows one to capture this overflow and
via a vendor dependent routine, FUNCTION TRAP_EXCEPTION@,
write a routine to handle it as needed.
Similarly Lahey FTN95 offers vendor dependent routines,
Intrup and Invalop.
On Sun, 11 Jul 1999 22:07:20 -0400, <kat...@ibm.net> wrote:
-|Please, could anyone give me a routine or function to divide two real
-|(double, if possible) numbers safely? That is, one that performs the
-|division only if it does not produce overflow.
-|
-|The type of function I have in mind is something like:
-|
-|q = safe_div(a,b,altv)
-|
-|where q is set at a/b in the normal case, but if a/b overflows, then the
-|returned value is altv. Or something of that sort.
-|
-|Or perhaps, someone can suggest a better way of handling situations when a
-|division (or other operation) may produce overflow (or other error), without
-|requiring the intervention of the user.
-|
-|Thank you,
-|
-|Julio
-|
Herman D. (Skip) Knoble, Research Associate
Mailto:h...@psu.edu
Web: http://www.personal.psu.edu/hdk
Center for Academic Computing
Penn State University
214C Computer Building
University Park, PA 16802-2101
Phone:+1 814 865-0818 Fax:+1 814 863-7049
if(exponent(a) - exponent (b) >= maxexponent(a) .OR. b==0)Then
q=altv
else
q=a/b
endif
The = in the >= is to take into account the case when the fractional part of
a is 1.111... and that of b is 1.
It works very well.
Thank you again,
Julio.