Skip to content

Commit

Permalink
various: add recoverable overflow and underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
yitzchak committed Aug 20, 2024
1 parent 62dc944 commit 78217dc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
41 changes: 33 additions & 8 deletions code/condition/utility.lisp
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
(in-package #:quaviver.condition)

(defun floating-point-overflow (operation &rest operands)
(error 'cl:floating-point-overflow
:operation operation
:operands operands))
(defun floating-point-underflow (float-type sign operation &rest operands)
(restart-case
(error 'cl:floating-point-underflow
:operation operation
:operands operands)
(recover ()
:report (lambda (stream)
(format stream "Recover using ~:[negative~;positive~] zero as the value."
(plusp sign)))
(quaviver:triple-float nil float-type 2 0 0 sign))))

(defun floating-point-underflow (operation &rest operands)
(error 'cl:floating-point-underflow
:operation operation
:operands operands))
(defun floating-point-overflow (float-type sign operation &rest operands)
(restart-case
(error 'cl:floating-point-overflow
:operation operation
:operands operands)
(recover ()
:report (lambda (stream)
(format stream
#+clisp "Recover using the most ~:[negative~;positive~] floating point as the value."
#-clisp "Recover using ~:[negative~;positive~] infinity as the value."
(plusp sign)))
#+clisp (if (minusp sign)
(ecase float-type
(short-float most-negative-short-float)
(single-float most-negative-single-float)
(double-float most-negative-double-float)
(long-float most-negative-long-float))
(ecase float-type
(short-float most-positive-short-float)
(single-float most-positive-single-float)
(double-float most-positive-double-float)
(long-float most-positive-long-float)))
#-clisp (quaviver:triple-float nil float-type 2 0 :infinity sign))))
8 changes: 4 additions & 4 deletions code/jaffer/implementation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
(cond ((> q ,(+ max-exponent
(quaviver.math:ceiling-log-expt 2 10 1)))
(quaviver.condition:floating-point-overflow
'quaviver:triple-float
,client ',float-type 10
',float-type ,sign-var
'quaviver:triple-float ,client ',float-type 10
,significand ,exponent ,sign))
((< q ,(- min-exponent
(quaviver.math:ceiling-log-expt 2 10 1)))
(quaviver.condition:floating-point-underflow
'quaviver:triple-float
,client ',float-type 10
',float-type ,sign-var
'quaviver:triple-float ,client ',float-type 10
,significand ,exponent ,sign))
((minusp ,exponent)
(let* ((scale (expt 5 (- ,exponent)))
Expand Down
16 changes: 10 additions & 6 deletions code/liebler/implementation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
,(quaviver:primitive-triple-float-form float-type significand-var exponent-var sign-var))
((< ,exponent-var ,(- (expt 2 21)))
(quaviver.condition:floating-point-underflow
'triple-float
',float-type ,sign-var
'quaviver:triple-float ,client ',float-type 10
,significand-var ,exponent-var ,sign-var))
((> ,exponent-var ,(1- (expt 2 21)))
(quaviver.condition:floating-point-overflow
'triple-float
',float-type ,sign-var
'quaviver:triple-float ,client ',float-type 10
,significand-var ,exponent-var ,sign-var))
(t
(let* ((shift (- ,significand-size (integer-length ,significand-var)))
Expand All @@ -39,13 +41,15 @@
(cond ((> k ,(+ max-exponent
(quaviver.math:ceiling-log-expt 2 10 1)))
(quaviver.condition:floating-point-overflow
'quaviver:triple-float
,client ',float-type 10 ,significand-var ,exponent-var ,sign-var))
',float-type ,sign-var
'quaviver:triple-float ,client ',float-type 10
,significand-var ,exponent-var ,sign-var))
((< k ,(- min-exponent
(quaviver.math:ceiling-log-expt 2 10 1)))
(quaviver.condition:floating-point-underflow
'quaviver:triple-float
,client ',float-type 10 ,significand-var ,exponent-var ,sign-var))
',float-type ,sign-var
'quaviver:triple-float ,client ',float-type 10
,significand-var ,exponent-var ,sign-var))
(t
(setf ,significand-var (quaviver.math:round-to-odd
,arithmetic-size
Expand Down
3 changes: 2 additions & 1 deletion code/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@
#:invalid-character-error
#:invalid-leading-zeros-error
#:invalid-property-error
#:missing-digits-error))
#:missing-digits-error
#:recover))
12 changes: 8 additions & 4 deletions code/primitive-triple-float-form.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@
((zerop ,significand-var))
((< ,exponent-var ,(- (expt 2 21)))
(quaviver.condition:floating-point-underflow
'triple-float
',float-type ,sign-var
'triple-float nil ',float-type 2
,significand-var ,exponent-var ,sign-var))
((> ,exponent-var ,(1- (expt 2 21)))
(quaviver.condition:floating-point-overflow
'triple-float
',float-type ,sign-var
'triple-float nil ',float-type 2
,significand-var ,exponent-var ,sign-var))
(t
(let ((shift (- ,significand-size
Expand All @@ -65,11 +67,13 @@
(decf ,exponent-var shift))
(cond ((< ,exponent-var ,min-exponent)
(quaviver.condition:floating-point-underflow
'triple-float
',float-type ,sign-var
'triple-float nil ',float-type 2
,significand-var ,exponent-var ,sign-var))
((> ,exponent-var ,max-exponent)
(quaviver.condition:floating-point-overflow
'triple-float
',float-type ,sign-var
'triple-float nil ',float-type 2
,significand-var ,exponent-var ,sign-var))
(t
(incf ,exponent-var ,exponent-bias)
Expand Down

0 comments on commit 78217dc

Please sign in to comment.