Skip to content

Commit

Permalink
Make lapy and nrm2 consistent
Browse files Browse the repository at this point in the history
Avoid the introduction of NaN by divisons inf / inf
  • Loading branch information
angsch committed Aug 8, 2021
1 parent 3b26987 commit 906adae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
8 changes: 6 additions & 2 deletions SRC/dlapy2.f
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ DOUBLE PRECISION FUNCTION DLAPY2( X, Y )
PARAMETER ( ONE = 1.0D0 )
* ..
* .. Local Scalars ..
DOUBLE PRECISION W, XABS, YABS, Z
DOUBLE PRECISION W, XABS, YABS, Z, HUGEVAL
LOGICAL X_IS_NAN, Y_IS_NAN
* ..
* .. External Functions ..
LOGICAL DISNAN
EXTERNAL DISNAN
* ..
* .. External Subroutines ..
DOUBLE PRECISION DLAMCH
* ..
* .. Intrinsic Functions ..
INTRINSIC ABS, MAX, MIN, SQRT
* ..
Expand All @@ -94,13 +97,14 @@ DOUBLE PRECISION FUNCTION DLAPY2( X, Y )
Y_IS_NAN = DISNAN( Y )
IF ( X_IS_NAN ) DLAPY2 = X
IF ( Y_IS_NAN ) DLAPY2 = Y
HUGEVAL = DLAMCH( 'Overflow' )
*
IF ( .NOT.( X_IS_NAN.OR.Y_IS_NAN ) ) THEN
XABS = ABS( X )
YABS = ABS( Y )
W = MAX( XABS, YABS )
Z = MIN( XABS, YABS )
IF( Z.EQ.ZERO ) THEN
IF( Z.EQ.ZERO .OR. W.GT.HUGEVAL ) THEN
DLAPY2 = W
ELSE
DLAPY2 = W*SQRT( ONE+( Z / W )**2 )
Expand Down
8 changes: 6 additions & 2 deletions SRC/dlapy3.f
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ DOUBLE PRECISION FUNCTION DLAPY3( X, Y, Z )
PARAMETER ( ZERO = 0.0D0 )
* ..
* .. Local Scalars ..
DOUBLE PRECISION W, XABS, YABS, ZABS
DOUBLE PRECISION W, XABS, YABS, ZABS, HUGEVAL
* ..
* .. External Subroutines ..
DOUBLE PRECISION DLAMCH
* ..
* .. Intrinsic Functions ..
INTRINSIC ABS, MAX, SQRT
* ..
* .. Executable Statements ..
*
HUGEVAL = DLAMCH( 'Overflow' )
XABS = ABS( X )
YABS = ABS( Y )
ZABS = ABS( Z )
W = MAX( XABS, YABS, ZABS )
IF( W.EQ.ZERO ) THEN
IF( W.EQ.ZERO .OR. W.GT.HUGEVAL ) THEN
* W can be zero for max(0,nan,0)
* adding all three entries together will make sure
* NaN will not disappear.
Expand Down
11 changes: 6 additions & 5 deletions SRC/slapy2.f
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,33 @@ REAL FUNCTION SLAPY2( X, Y )
PARAMETER ( ONE = 1.0E0 )
* ..
* .. Local Scalars ..
REAL W, XABS, YABS, Z
REAL W, XABS, YABS, Z, HUGEVAL
LOGICAL X_IS_NAN, Y_IS_NAN
* ..
* .. External Functions ..
LOGICAL SISNAN
EXTERNAL SISNAN
* ..
* .. External Subroutines ..
REAL SLAMCH
* ..
* .. Intrinsic Functions ..
INTRINSIC ABS, MAX, MIN, SQRT
* ..
* .. Executable Statements ..
*
* ..
* .. Executable Statements ..
*
X_IS_NAN = SISNAN( X )
Y_IS_NAN = SISNAN( Y )
IF ( X_IS_NAN ) SLAPY2 = X
IF ( Y_IS_NAN ) SLAPY2 = Y
HUGEVAL = SLAMCH( 'Overflow' )
*
IF ( .NOT.( X_IS_NAN.OR.Y_IS_NAN ) ) THEN
XABS = ABS( X )
YABS = ABS( Y )
W = MAX( XABS, YABS )
Z = MIN( XABS, YABS )
IF( Z.EQ.ZERO ) THEN
IF( Z.EQ.ZERO .OR. W.GT.HUGEVAL ) THEN
SLAPY2 = W
ELSE
SLAPY2 = W*SQRT( ONE+( Z / W )**2 )
Expand Down
8 changes: 6 additions & 2 deletions SRC/slapy3.f
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ REAL FUNCTION SLAPY3( X, Y, Z )
PARAMETER ( ZERO = 0.0E0 )
* ..
* .. Local Scalars ..
REAL W, XABS, YABS, ZABS
REAL W, XABS, YABS, ZABS, HUGEVAL
* ..
* .. External Subroutines ..
REAL SLAMCH
* ..
* .. Intrinsic Functions ..
INTRINSIC ABS, MAX, SQRT
* ..
* .. Executable Statements ..
*
HUGEVAL = SLAMCH( 'Overflow' )
XABS = ABS( X )
YABS = ABS( Y )
ZABS = ABS( Z )
W = MAX( XABS, YABS, ZABS )
IF( W.EQ.ZERO ) THEN
IF( W.EQ.ZERO .OR. W.GT.HUGEVAL ) THEN
* W can be zero for max(0,nan,0)
* adding all three entries together will make sure
* NaN will not disappear.
Expand Down

0 comments on commit 906adae

Please sign in to comment.