Raymond Toy pushed to branch issue-275b-signal-float-underflow at cmucl / cmucl

Commits:

3 changed files:

Changes:

  • src/code/reader.lisp
    ... ... @@ -1855,12 +1855,12 @@ the end of the stream."
    1855 1855
                                    vm:single-float-bias))))
    
    1856 1856
                   ((double-float long-float
    
    1857 1857
                                  #+double-double kernel:double-double-float)
    
    1858
    +               ;; Double-float exponent range is -1074 to -1023
    
    1858 1859
                    (values (* 2 (- vm:double-float-normal-exponent-min
    
    1859 1860
                                    vm:double-float-bias
    
    1860 1861
                                    vm:double-float-digits))
    
    1861 1862
                            (* 2 (- vm:double-float-normal-exponent-max
    
    1862 1863
                                    vm:double-float-bias)))))
    
    1863
    -          ;; Double-float exponent range is -1074 to -1023
    
    1864 1864
               (unless (< log2-low log2-num log2-high)
    
    1865 1865
                 ;; The number is definitely too large or too small to fit.
    
    1866 1866
                 ;; Signal an error.
    
    ... ... @@ -1879,18 +1879,19 @@ the end of the stream."
    1879 1879
               (error _"Underflow"))
    
    1880 1880
             result)
    
    1881 1881
         (floating-point-underflow ()
    
    1882
    -      ;; Resignal the underflow, but allow the user to continue with
    
    1882
    +      ;; Resignal a reader error, but allow the user to continue with
    
    1883 1883
           ;; 0.
    
    1884 1884
           (let ((zero (coerce 0 float-format)))
    
    1885 1885
             (restart-case
    
    1886
    -            (error 'floating-point-underflow)
    
    1886
    +            (%reader-error stream _"Floating point underflow when reading ~S"
    
    1887
    +                           (read-buffer-to-string))
    
    1887 1888
               (continue ()
    
    1888 1889
                 :report (lambda (stream)
    
    1889 1890
                           (format stream "Return ~A" zero))
    
    1890 1891
                 zero))))
    
    1891 1892
         (error ()
    
    1892
    -      (%reader-error stream _"Number not representable as a ~S: ~S"
    
    1893
    -		           float-format (read-buffer-to-string)))))
    
    1893
    +	   (%reader-error stream _"Number not representable as a ~S: ~S"
    
    1894
    +			  float-format (read-buffer-to-string)))))
    
    1894 1895
     
    
    1895 1896
     
    
    1896 1897
     (defun make-ratio (stream)
    

  • src/i18n/locale/cmucl.pot
    ... ... @@ -8731,6 +8731,14 @@ msgstr ""
    8731 8731
     msgid "Number not representable as a ~S: ~S"
    
    8732 8732
     msgstr ""
    
    8733 8733
     
    
    8734
    +#: src/code/reader.lisp
    
    8735
    +msgid "Underflow"
    
    8736
    +msgstr ""
    
    8737
    +
    
    8738
    +#: src/code/reader.lisp
    
    8739
    +msgid "Floating point underflow when reading ~S"
    
    8740
    +msgstr ""
    
    8741
    +
    
    8734 8742
     #: src/code/reader.lisp
    
    8735 8743
     msgid "Invalid ratio: ~S/~S"
    
    8736 8744
     msgstr ""
    

  • tests/float.lisp
    ... ... @@ -217,12 +217,31 @@
    217 217
         (:tag :issues)
    
    218 218
       (lisp::with-float-traps-enabled (:underflow)
    
    219 219
         ;; A denormal
    
    220
    -    (assert-error 'floating-point-underflow
    
    220
    +    (assert-error 'reader-error
    
    221 221
                       (read-from-string "1e-40"))
    
    222
    -    (assert-error 'floating-point-underflow
    
    222
    +    (assert-error 'reader-error
    
    223 223
                       (read-from-string (format nil "~A" least-positive-single-float)))
    
    224 224
         ;; The same for double-floats
    
    225
    -    (assert-error 'floating-point-underflow
    
    225
    +    (assert-error 'reader-error
    
    226 226
                       (read-from-string "1d-308"))
    
    227
    -    (assert-error 'floating-point-underflow
    
    227
    +    (assert-error 'reader-error
    
    228 228
                       (read-from-string (format nil "~A" least-positive-double-float)))))
    
    229
    +
    
    230
    +(define-test reader.float-underflow
    
    231
    +    (:tag :issues)
    
    232
    +  (lisp::with-float-traps-enabled (:underflow)
    
    233
    +    ;; The expected string comes from make-float-aux.
    
    234
    +    (let ((expected "Floating point underflow when reading ~S"))
    
    235
    +      (flet ((test-reader-underflow (string)
    
    236
    +               ;; Test that the we got a reader-error when a number
    
    237
    +               ;; would underflow and that the message says we got an
    
    238
    +               ;; underflow.
    
    239
    +               (let ((condition (nth-value 1 (ignore-errors (read-from-string string)))))
    
    240
    +                 (assert-equal 'reader-error (type-of condition))
    
    241
    +                 (assert-equal expected (lisp::reader-error-format-control condition)))))
    
    242
    +        ;; Underflow single-floats
    
    243
    +        (test-reader-underflow "1e-40")
    
    244
    +        (test-reader-underflow (format nil "~A" least-positive-single-float))
    
    245
    +        ;; Underflow double-floats
    
    246
    +        (test-reader-underflow "1d-308")
    
    247
    +        (test-reader-underflow (format nil "~A" least-positive-double-float))))))