unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#11887: string->number edge cases
@ 2012-07-09 12:29 Ian Price
  2013-03-05 14:49 ` Andy Wingo
  0 siblings, 1 reply; 8+ messages in thread
From: Ian Price @ 2012-07-09 12:29 UTC (permalink / raw)
  To: 11887; +Cc: Peter.Bex

[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]


Hi guilers,

I've mentioned these to Mark Weaver on IRC off-hand before, but I'm
posting this bug report to "formalise" it, and make sure we don't
forget.

Peter Bex, as one of the maintainers of the chicken "numbers" egg,
created a thorough test suite for string->number a while ago[0], and it
points out a number of possible and actual errors in the handling of
string->number.

I've attached a copy with the modifications for guile already applied,
and a copy of the full results, but the summary is as follows.

If the number contains a division by zero, we get a numerical overflow
error.

scheme@(guile−user)> (string->number "3/0")
ERROR: In procedure string−>number:
ERROR: Throw to key `numerical−overflow' with args `("make−ratio" "Numerical overflow" #f #f)'.

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile−user) [1]> ,q

This contradicts the r6rs specification[1], which mandates a return
value of #f. IMO, this is the correct behaviour, since otherwise, we
have created a special case in the API for one type of invalid number.

(These tests cause the program to stop, and so are commented out in the
attached version.)

The rest of the errors are less serious, and show that we treat nans and
infinity someone inconsistently in guile. Specifically, we are strict on
case, and lenient on the numerical prefix.

0. http://bugs.call-cc.org/browser/release/4/numbers/trunk/tests/string-conversion.scm
1. http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_idx_584

Peter,

Thanks for these

-- 
Ian Price

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"

[-- Attachment #2: Conversion tests courtesy of Peter Bex --]
[-- Type: text/plain, Size: 15897 bytes --]

;;;
;;; Numerical syntax "torture test"
;;;
;;; This tries to test a lot of edge cases in Scheme's numerical syntax.
;;;
;;; Output is written so that if you run it through "grep ERROR" it will
;;; output nothing (and exit status will be nonzero) if there are no errors.
;;; If you run it through "tail -n 1" you will just get the total error summary.
;;;
;;; This code assumes that string->number accepts numbers with embedded radix
;;; specifiers (R5RS mentions that it's allowed to return #f in those cases).
;;; It also doesn't try to support Schemes which support *only* integers or
;;; *only* flonums (which is also allowed by R5RS).
;;;

;;;
;; The prelude below is messy but required to get everything working
;; with some of the major Schemes.
;;
;; Also note that to test with Gambit, it appears you first need to type in
;; (load "~~/lib/syntax-case") and then load this file, or use gsi's -:s switch
;;;

;(use extras numbers)            ; Chicken w/ numbers
;(use-syntax (ice-9 syncase)) ; Guile

;; Set this to #f if the Scheme has no compnums at all, 'inexact if it only
;; supports inexact compnums or 'exact if it supports exact compnums.
;; (Gauche, Guile, SCM: inexact, Chicken w/o numbers: #f)
(define compnum-type 'inexact)

;; Set this to #f if the Scheme has no fractional number support,
;; 'exact if it supports rational numbers and 'inexact if it converts fractions
;; to floating-point, inexact numbers
(define fraction-type 'exact)

;; Fix these if your Scheme doesn't allow division by zero
;; For Chicken without the numbers egg, use fp/ instead of /
(define the-nan (/ 0.0 0.0))
(define pos-inf (/ 1.0 0.0))
(define neg-inf (/ -1.0 0.0))

; Scheme48, Racket, Gambit, Chicken w/o numbers, SCM
;(define (nan? x) (and (number? x) (not (= x x))))

(define total-errors 0)

(define (check-string-against-values! str . possible-values)
  (let ((res (string->number str)))
    (let lp ((values possible-values))
      (if (null? values)
          (begin (display       "PARSE ERROR         ")
                 (write (cons str possible-values))
                 (display " => ") (write res) (newline)
                 (set! total-errors (+ total-errors 1)))
          (let ((value (car values)))
            (if (not (or (and (not (string? value)) (equal? res value))
                         (and res (nan? res) (or (and value (nan? value))))))
                (lp (cdr values))
                (let ((re-str (and res (number->string res))))
                  (let lp2 ((values possible-values))
                    (if (null? values)
                        (begin (display "SERIALIZATION ERROR ")
                               (write (cons str possible-values))
                               (display " => ") (write re-str) (newline)
                               (set! total-errors (+ total-errors 1)))
                        (let ((value (car values)))
                          (if (not (or (and res (string=? re-str str))
                                       (and (not res) (not value))
                                       (and res (string? value) (string=? re-str value))))
                              (lp2 (cdr values))
                              (begin (display "OK                  ")
                                     (write (cons str possible-values))
                                     (newline)))))))))))))

;; Here comes a horrible nasty hack.  It seems to work though ;)
(define-syntax test-numbers
  (syntax-rules (compnums fractions)
    ((_ (compnums (types e1 ...) ...) rest ...)
     (begin
       (case compnum-type (types (test-numbers e1 ... "no-totals")) ...)
       (test-numbers rest ...)))
    ((_ (fractions (types e1 ...) ...) rest ...)
     (begin
       (case fraction-type (types (test-numbers e1 ... "no-totals")) ...)
       (test-numbers rest ...)))
    ((_ (str value ...) rest ...)
     (begin
       (check-string-against-values! str value ...)
       (test-numbers rest ...)))
    ((_ "no-totals") #f)
    ((_ x rest ...)
     (begin (newline) (display "-> ") (display x) (newline)
            (display "-----------------------------------------------------")
            (newline)
            (test-numbers rest ...)))
    ((_)
     (if (= 0 total-errors)
         (begin (newline)
                (display "-----> Everything OK, no errors!")
                (newline))
         (begin (newline)
                (display "-----> TOTAL ERRORS: ")
                (display total-errors)
                (newline))))))

(test-numbers
 "Simple integers"
 ("1" 1)
 ("+1" 1 "1")
 ("-1" (- 1))
 ("#i1" 1.0 "1.0" "1.")
 ("#I1" 1.0 "1.0" "1.")
 ("#i-1" (- 1.0) "-1.0" "-1.")
 ("-#i1" #f)
 ("+-1" #f)
 ("" #f)
 ("-" #f)
 ("+" #f)
 ("+-" #f)

 "Basic decimal notation"
 ("1.0" (exact->inexact 1) "1.")
 ("1." 1.0 "1.0" "1.")
 ("1.#" 1.0 1.5 "1.0" "1." "1.5")
 (".1" 0.1 "0.1" "100.0e-3")
 ("-.1" (- 0.1) "-0.1" "-100.0e-3")
 ;; Some Schemes don't allow negative zero. This is okay with the standard
 ("-.0" -0.0 "-0." "-0.0" "0.0" "0." ".0")
 ("-0." -0.0 "-.0" "-0.0" "0.0" "0." ".0")
 ("." #f)
 (".1." #f)
 ("..1" #f)
 ("1.." #f)
 ("#i1.0" 1.0 "1.0" "1.")
 ("#e1.0" 1 "1")
 ("#e-.0" 0 "0")
 ("#e-0." 0 "0")
 ("-#e.0" #f)

 "Decimal notation with padding"
 ("1#" 10.0 15.0 "10.0" "15.0" "10." "15.")
 ("#e1#" 10 15 "10" "15")
 ("#E1#" 10 15 "10" "15")
 ("#1" #f)
 ("#" #f)
 ("1#2" #f)
 ("1.#2" #f)
 (".#" #f)
 ("#.#" #f)
 ("#.1" #f)
 ("1#.2" #f)
 ("1#." 10.0 15.0 "10.0" "15.0" "10." "15.")

 "Decimal notation with suffix"
 ("1e2" 100.0 "100.0" "100.")
 ("1E2" 100.0 "100.0" "100.")
 ("1s2" 100.0 "100.0" "100.")
 ("1S2" 100.0 "100.0" "100.")
 ("1f2" 100.0 "100.0" "100.")
 ("1F2" 100.0 "100.0" "100.")
 ("1d2" 100.0 "100.0" "100.")
 ("1D2" 100.0 "100.0" "100.")
 ("1l2" 100.0 "100.0" "100.")
 ("1L2" 100.0 "100.0" "100.")
 ("1e2e3" #f)
 ("1e2s3" #f)
 ("1e2.0" #f)

 "Decimal notation with suffix and padding"
 ("1#e2" 1000.0 1500.0 "1000.0" "1500.0" "1000." "1500." "1.0e3" "15.0e2")
 ("1e2#" #f)

 "NaN, Inf"
 ("+nan.0" the-nan "+NaN.0")
 ("+NAN.0" the-nan "+nan.0" "+NaN.0")
 ("+nan.1" #f)
 ("+nan.01" #f)
 ("+inf.0" pos-inf "+Inf.0")
 ("+InF.0" pos-inf "+inf.0" "+Inf.0")
 ("-inf.0" neg-inf "-Inf.0")
 ("-iNF.0" neg-inf "-inf.0" "-Inf.0")
 ("+inf.01" #f)
 ("+inf.1" #f)
 ("-inf.01" #f)
 ("-inf.1" #f)
 ("+inf.0/1" #f)
 ("1/+inf.0" #f)
 ("+nan" #f)
 ("+inf" #f)
 ("-inf" #f)
 ("nan.0" #f)
 ("inf.0" #f)
 ;; Thanks to John Cowan for these
 ("#e+nan.0" #f)
 ("#e+inf.0" #f)
 ("#e-inf.0" #f)
 ("#i+nan.0" the-nan "+nan.0" "+NaN.0")
 ("#i+inf.0" pos-inf "+inf.0" "+Inf.0")
 ("#i-inf.0" neg-inf "-inf.0" "-Inf.0")

 "Fractions"
 (fractions
  ((exact)
   ("1/2" (/ 1 2))
   ("#e1/2" (/ 1 2) "1/2")
   ("10/2" 5 "5")
   ("-1/2" (- (/ 1 2)))
   ;("10/0" #f)
   ("0/10" 0 "0")
   ("#e0/10" 0 "0")
   ("#e1#/2" 5 (/ 15 2) "5" "15/2")
   ("#e1/2#" (/ 1 20) "1/20")
   ("#i3/2" (/ 3.0 2.0) "1.5"))
  ((inexact)
   ("1/2" (/ 1 2) "0.5" ".5" "500.0e-3")
   ("0/10" 0.0 "0.0")
   ("10/2" 5.0 "5.0" "5.")
   ;; Unsure what "#e1/2" is supposed to do in Scheme w/o exact fractions
   ("#i10/2" 5.0 "5.0" "5.")
   ("-1/2" (- (/ 1 2)) "-0.5" "-.5" "-500.0e-3")))
 (fractions
  ((inexact exact)
   ;("#i1/0" pos-inf "+inf.0" "+Inf.0")
   ;("#i-1/0" neg-inf "-inf.0" "-Inf.0")
   ;("#i0/0" the-nan "+nan.0" "+NaN.0")
   ;; This _could_ be valid in some Schemes (but isn't as pretty)
   ;("#i1/0" #f)
   ;("#i-1/0" #f)
   ;("#i0/0" #f)
   
   ("1/-2" #f)
   ("1.0/2" #f)
   ("1/2.0" #f)
   ("1/2e2" #f)
   ("1/2e2" #f)
   ("1#/2" 5.0 7.5 "5.0" "5." "7.5")
   ("1/2#" 0.05 "0.05" ".05" "50.0e-3")
   ("1#/#" #f)
   ("1/" #f)
   ("1/+" #f)
   ("+/1" #f)
   ("/1" #f)
   ("/" #f)))
 
 "Basic complex numbers (rectangular notation)"
 (compnums
  ((exact)
   ("1+2i" (make-rectangular 1 2))
   ("1+2I" (make-rectangular 1 2) "1+2i")
   ("1-2i" (make-rectangular 1 -2))
   ("-1+2i" (make-rectangular -1 2))
   ("-1-2i" (make-rectangular -1 -2))
   ("+i" (make-rectangular 0 1) "+1i" "0+i" "0+1i")
   ("0+i" (make-rectangular 0 1) "+i" "+1i" "0+1i")
   ("0+1i" (make-rectangular 0 1) "+i" "+1i" "0+i")
   ("-i" (make-rectangular 0 -1) "-1i" "0-i" "0-1i")
   ("0-i" (make-rectangular 0 -1) "-i" "-1i" "0-1i")
   ("0-1i" (make-rectangular 0 -1) "-i" "-1i" "0-i")
   ("+2i" (make-rectangular 0 2) "0+2i")
   ("-2i" (make-rectangular 0 -2) "-2i" "0-2i"))
  ((inexact)
   ("1+2i" (make-rectangular 1 2) "1.0+2.0i" "1.+2.i")
   ("1+2I" (make-rectangular 1 2) "1.0+2.0i" "1.+2.i")
   ("1-2i" (make-rectangular 1 -2) "1.0-2.0i" "1.-2.i")
   ("-1+2i" (make-rectangular -1 2) "-1.0+2.0i" "-1.+2.i")
   ("-1-2i" (make-rectangular -1 -2) "-1.0-2.0i" "-1.-2.i")
   ("+i" (make-rectangular 0 1) "+1.i" "+1.0i" "0.+1.i" "0.0+1.0i")
   ("0+i" (make-rectangular 0 1) "0+1i" "+1.i" "+1.0i" "0.+1.i" "0.0+1.0i")
   ("0+1i" (make-rectangular 0 1) "+1.i" "+1.0i" "0.+1.i" "0.0+1.0i")
   ("-i" (make-rectangular 0 -1) "-1.i" "-1.0i" "0.-1.i" "0.0-1.0i")
   ("0-i" (make-rectangular 0 -1) "-1.i" "-1.0i" "0.-1.i" "0.0-1.0i")
   ("0-1i" (make-rectangular 0 -1) "-1.i" "-1.0i" "0.-1.i" "0.0-1.0i")
   ("+2i" (make-rectangular 0 2) "+2.0i" "+2.i" "0.+2.i" "0.0+2.0i")
   ("-2i" (make-rectangular 0 -2) "-2.0i" "-2.i" "0.-2.i" "0.0-2.0i")))
 (compnums
  ((exact inexact)
   ("1#+1#i" (make-rectangular 10.0 10.0) (make-rectangular 15.0 15.0)
    "10.0+10.0i" "10.+10.i" "15.0+15.0i" "15.+15.i")))
 ("2i" #f)
 ("+-i" #f)
 ("i" #f)
 ("1+2i1" #f)
 ("1+2" #f)
 ("1#+#i" #f)

 (compnums
  ((exact inexact)
   "Decimal-notation complex numbers (rectangular notation)"
   ("1.0+2i" (make-rectangular 1.0 2) "1.0+2.0i" "1.0+2i" "1.+2i" "1.+2.i")
   ("1+2.0i" (make-rectangular 1 2.0) "1.0+2.0i" "1+2.0i" "1.+2.i" "1+2.i")
   ("1#.+1#.i" (make-rectangular 10.0 10.0) (make-rectangular 15.0 15.0)
    "10.0+10.0i" "10.+10.i" "15.0+15.0i" "15.+15.i")
   ("1e2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
   ("1s2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
   ("1.0+1e2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
   ("1.0+1s2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
   ("1#e2+1.0i" (make-rectangular 1000.0 1.0) (make-rectangular 1500.0 1.0)
    "1000.0+1.0i" "1000.+1.i" "1500.0+1.0i" "1500.+1.i" "1.0e3+1.0i" "15.0e2+1.0i")
   ("1.0+1#e2i" (make-rectangular 1.0 1000.0) (make-rectangular 1.0 1500.0)
    "1.0+1000.0i" "1.+1000.i" "1.0+1500.0i" "1.+1500.i" "1.0+1.0e3i" "1.0+15.0e2i")
   (".i" #f)
   ("+.i" #f)
   (".+i" #f)))

 (compnums
  ((exact)
   "Fractional complex numbers (rectangular notation)"
   ("1/2+3/4i" (make-rectangular (/ 1 2) (/ 3 4))))
  ((inexact)
   "Fractional complex numbers (rectangular notation)"
   ("1/2+3/4i" (make-rectangular (/ 1 2) (/ 3 4)) "0.5+0.75i" ".5+.75i" "500.0e-3+750.0e-3i")))

 (compnums
  ((exact inexact)
   "Mixed fractional/decimal notation complex numbers (rectangular notation)"
   ("1#/2+3/4i" (make-rectangular 5.0 (/ 3 4)) (make-rectangular 7.5 (/ 3 4))
    "5.0+0.75i" "5.+.75i" "7.5+0.75i" "5.0+3/4i" "5.+3/4i" "7.5+3/4i" "5.0+750.0e-3i")
   ("0.5+3/4i" (make-rectangular 0.5 (/ 3 4))
    "0.5+0.75i" ".5+.75i" "0.5+3/4i" ".5+3/4i" "500.0e-3+750.0e-3i")
   ("1.5+1#/4i" (make-rectangular 1.5 2.5) (make-rectangular 1.5 3.75)
    "1.5+2.5i" "1.5+3.75i")
   ("0.5+1/#i" #f)
   ("0.5+1/1#2i" #f)
   ("1/#+0.5i" #f)
   ("1/1#2+0.5i" #f)

   "Mixed notation with infinity (might fail on mixed exactness compnums)"
   ;; This is a nasty one. Switch to inexact *after* reading the first number.
   ;; Note that it's perfectly acceptable for a scheme with *mixed* exactness
   ;; in complex values to return #f here.  TODO: How to parameterize this, we
   ;; *really* want to test that single-exactness compnums systems accept this.
   ;("1/0+1.2i" (make-rectangular pos-inf 1.2) "+inf.0+1.2i" "+Inf.0+1.2i")
   ;; Less nasty, most get this right.  Same caveat as above re: mixed exactness
   ;("1.2+1/0i" (make-rectangular 1.2 pos-inf) "1.2+inf.0i"
   ;"1.2+Inf.0")
   ))

 (compnums
  ((exact inexact)
   "Complex NaN, Inf (rectangular notation)"
   ("+nan.0+nan.0i" (make-rectangular the-nan the-nan) "+NaN.0+NaN.0i")
   ("+inf.0+inf.0i" (make-rectangular pos-inf pos-inf) "+Inf.0+Inf.0i")
   ("-inf.0+inf.0i" (make-rectangular neg-inf pos-inf) "-Inf.0+Inf.0i")
   ("-inf.0-inf.0i" (make-rectangular neg-inf neg-inf) "-Inf.0-Inf.0i")
   ("+inf.0-inf.0i" (make-rectangular pos-inf neg-inf) "+Inf.0-Inf.0i")
 
   "Complex numbers (polar notation)"
   ;; TODO: Add some here. The problem is the representation
   ;;       is hard to nail down when echoed back as rectangular
   ;;       since they're floating point with many digits, and depend
   ;;       on the precision of PI used internally.
   ("1@2i" #f)
   ("0.5@1/#" #f)
   ("0.5@1/1#2" #f)
   ("1/#@0.5" #f)
   ("1/1#2@0.5" #f)
   ("1@" #f)
   ("1#@#" #f)
   ("1/@" #f)
   ("@/1" #f)
   ("@1" #f)
   ("1@+" #f)
   ("+@1" #f)
   ("@" #f)))

 "Base prefixes"
 ("#x11" 17 "17")
 ("#X11" 17 "17")
 ("#d11" 11 "11")
 ("#D11" 11 "11")
 ("#o11" 9 "9")
 ("#O11" 9 "9")
 ("#b11" 3 "3")
 ("#B11" 3 "3")
 ("#da1" #f)
 ("#o8" #f)
 ("#b2" #f)
 ("#o7" 7 "7")
 ("#xa" 10 "10")
 ("#xA" 10 "10")
 ("#xf" 15 "15")
 ("#xg" #f)
 ("#x-10" -16 "-16")
 ("#d-10" -10 "-10")
 ("#o-10" -8 "-8")
 ("#b-10" -2 "-2")
 ("-#x10" #f)
 ("-#d10" #f)
 ("-#o10" #f)
 ("-#b10" #f)
 ("#x-" #f)
 ("#x" #f)
 ("#d" #f)
 ("#d-" #f)
 ("#d+" #f)
 ("#o" #f)
 ("#o-" #f)
 ("#b" #f)
 ("#b-" #f)
 ("#e" #f)
 ("#e-" #f)
 ("#i" #f)
 ("#i-" #f)

 "Combination of prefixes"
 ("#x#x11" #f)
 ("#x#b11" #f)
 ("#b#o11" #f)
 ("#e#x10" 16 "16")
 ("#i#x10" 16.0 "16.0" "16.")
 ("#e#e10" #f)
 ("#e#e#x10" #f)
 ("#E#e#X10" #f)
 ("#i#e#x10" #f)
 ("#e#x#e10" #f)
 ("#x#x#e10" #f)
 ("#x#e#x10" #f)

 "Base prefixes with padding"
 ("#x1#0" #f)
 ("#d1#0" #f)
 ("#o1#0" #f)
 ("#b1#0" #f)
 ("#x1#" 16.0 24.0 "16.0" "24.0" "16." "24.")
 ("#d1#" 10.0 15.0 "10.0" "15.0" "10." "15.")
 ("#o1#" 8.0 12.0 "8.0" "12.0" "8." "12.")
 ("#b1#" 2.0 3.0 "2.0" "3.0" "2." "3.")

 "(Attempted) decimal notation with base prefixes"
 ("#x1.0" #f)
 ("#d1.0" 1.0 "1.0" "1.")
 ("#o1.0" #f)
 ("#b1.0" #f)
 ("#x1.#" #f)
 ("#d1.#" 1.0 1.5 "1.0" "1.5" "1.")
 ("#o1.#" #f)
 ("#b1.#" #f)
 ("#x1." #f)
 ("#d1." 1.0 "1.0" "1.")
 ("#o1." #f)
 ("#b1." #f)
 ("#x.1" #f)
 ("#d.1" 0.1 "0.1" ".1" "100.0e-3")
 ("#o.1" #f)
 ("#b.1" #f)
 ("#x1e2" 482 "482")
 ("#d1e2" 100.0 "100.0" "100.")
 ("#o1e2" #f)
 ("#b1e2" #f)

 "Fractions with prefixes"
 (fractions
  ((inexact)
   ("#x10/2" 8.0 "8.0" "8.")
   ("#x11/2" 8.5 "8.5")
   ("#d11/2" 5.5 "5.5")
   ("#o11/2" 4.5 "4.5")
   ("#b11/10" 1.5 "1.5"))
  ((exact)
   ("#x10/2" 8 "8")
   ("#x11/2" (/ 17 2) "17/2")
   ("#d11/2" (/ 11 2) "11/2")
   ("#o11/2" (/ 9 2) "9/2")
   ("#b11/10" (/ 3 2) "3/2")))
 (fractions
  ((inexact exact)
   ("#b11/2" #f)
   ("#x10/#o10" #f)
   ("10/#o10" #f)
   ("#x1#/2" 8.0 12.0 "8.0" "8." "12.0" "12.")
   ("#d1#/2" 5.0 7.5 "5.0" "5." "7.5")
   ("#o1#/2" 4.0 6.0 "4.0" "4." "6.0" "6.")
   ("#b1#/2" #f)
   ("#b1#/10" 1.0 1.5 "1.0" "1." "1.5")))

 (compnums
  ((exact inexact)
   "Complex numbers with prefixes"
   ("#x1#+1#i" (make-rectangular 16.0 16.0) (make-rectangular 24.0 24.0)
    "16.0+16.0i" "16.+16.i" "24.0+24.0i" "24.+24.i")
   ("#x1.0+1.0i" #f)
   ("#d1.0+1.0i" (make-rectangular 1.0 1.0) "1.0+1.0i" "1.+1.i")
   ("#o1.0+1.0i" #f)
   ("#b1.0+1.0i" #f)
   ("#x10+#o10i" #f)
   ("10+#o10i" #f)
   ("#x1@#x1" #f)
   ("1@#x1" #f)))
 (compnums
  ((exact)
   ("#x10+11i" (make-rectangular 16 17) "16+17i")
   ("#d10+11i" (make-rectangular 10 11) "10+11i")
   ("#o10+11i" (make-rectangular 8 9) "8+9i")
   ("#b10+11i" (make-rectangular 2 3) "2+3i")
   ("#e1.0+1.0i" (make-rectangular 1 1) "1+1i" "1+i")
   ("#i1.0+1.0i" (make-rectangular 1.0 1.0) "1.0+1.0i" "1.+1.i"))
  ((inexact)
   ("#x10+11i" (make-rectangular 16 17) "16.0+17.0i" "16.+17.i")
   ("#d10+11i" (make-rectangular 10 11) "10.0+11.0i" "10.+11.i")
   ("#o10+11i" (make-rectangular 8 9) "8.0+9.0i" "8.+9.i")
   ("#b10+11i" (make-rectangular 2 3) "2.0+3.0i" "2.+3.i")))

 )

[-- Attachment #3: results --]
[-- Type: text/plain, Size: 14158 bytes --]

;;; note: source file /home/ian/Downloads/string-conversion.scm
;;;       newer than compiled /home/ian/.cache/guile/ccache/2.0-LE-4-2.0/home/ian/Downloads/string-conversion.scm.go
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /home/ian/Downloads/string-conversion.scm
;;; compiled /home/ian/.cache/guile/ccache/2.0-LE-4-2.0/home/ian/Downloads/string-conversion.scm.go

-> Simple integers
-----------------------------------------------------
OK                  ("1" 1)
OK                  ("+1" 1 "1")
OK                  ("-1" -1)
OK                  ("#i1" 1.0 "1.0" "1.")
OK                  ("#I1" 1.0 "1.0" "1.")
OK                  ("#i-1" -1.0 "-1.0" "-1.")
OK                  ("-#i1" #f)
OK                  ("+-1" #f)
OK                  ("" #f)
OK                  ("-" #f)
OK                  ("+" #f)
OK                  ("+-" #f)

-> Basic decimal notation
-----------------------------------------------------
OK                  ("1.0" 1.0 "1.")
OK                  ("1." 1.0 "1.0" "1.")
OK                  ("1.#" 1.0 1.5 "1.0" "1." "1.5")
OK                  (".1" 0.1 "0.1" "100.0e-3")
OK                  ("-.1" -0.1 "-0.1" "-100.0e-3")
OK                  ("-.0" -0.0 "-0." "-0.0" "0.0" "0." ".0")
OK                  ("-0." -0.0 "-.0" "-0.0" "0.0" "0." ".0")
OK                  ("." #f)
OK                  (".1." #f)
OK                  ("..1" #f)
OK                  ("1.." #f)
OK                  ("#i1.0" 1.0 "1.0" "1.")
OK                  ("#e1.0" 1 "1")
OK                  ("#e-.0" 0 "0")
OK                  ("#e-0." 0 "0")
OK                  ("-#e.0" #f)

-> Decimal notation with padding
-----------------------------------------------------
OK                  ("1#" 10.0 15.0 "10.0" "15.0" "10." "15.")
OK                  ("#e1#" 10 15 "10" "15")
OK                  ("#E1#" 10 15 "10" "15")
OK                  ("#1" #f)
OK                  ("#" #f)
OK                  ("1#2" #f)
OK                  ("1.#2" #f)
OK                  (".#" #f)
OK                  ("#.#" #f)
OK                  ("#.1" #f)
OK                  ("1#.2" #f)
OK                  ("1#." 10.0 15.0 "10.0" "15.0" "10." "15.")

-> Decimal notation with suffix
-----------------------------------------------------
OK                  ("1e2" 100.0 "100.0" "100.")
OK                  ("1E2" 100.0 "100.0" "100.")
OK                  ("1s2" 100.0 "100.0" "100.")
OK                  ("1S2" 100.0 "100.0" "100.")
OK                  ("1f2" 100.0 "100.0" "100.")
OK                  ("1F2" 100.0 "100.0" "100.")
OK                  ("1d2" 100.0 "100.0" "100.")
OK                  ("1D2" 100.0 "100.0" "100.")
OK                  ("1l2" 100.0 "100.0" "100.")
OK                  ("1L2" 100.0 "100.0" "100.")
OK                  ("1e2e3" #f)
OK                  ("1e2s3" #f)
OK                  ("1e2.0" #f)

-> Decimal notation with suffix and padding
-----------------------------------------------------
OK                  ("1#e2" 1000.0 1500.0 "1000.0" "1500.0" "1000." "1500." "1.0e3" "15.0e2")
OK                  ("1e2#" #f)

-> NaN, Inf
-----------------------------------------------------
OK                  ("+nan.0" +nan.0 "+NaN.0")
PARSE ERROR         ("+NAN.0" +nan.0 "+nan.0" "+NaN.0") => #f
PARSE ERROR         ("+nan.1" #f) => +nan.0
PARSE ERROR         ("+nan.01" #f) => +nan.0
OK                  ("+inf.0" +inf.0 "+Inf.0")
PARSE ERROR         ("+InF.0" +inf.0 "+inf.0" "+Inf.0") => #f
OK                  ("-inf.0" -inf.0 "-Inf.0")
PARSE ERROR         ("-iNF.0" -inf.0 "-inf.0" "-Inf.0") => #f
OK                  ("+inf.01" #f)
OK                  ("+inf.1" #f)
OK                  ("-inf.01" #f)
OK                  ("-inf.1" #f)
OK                  ("+inf.0/1" #f)
OK                  ("1/+inf.0" #f)
OK                  ("+nan" #f)
OK                  ("+inf" #f)
OK                  ("-inf" #f)
PARSE ERROR         ("nan.0" #f) => +nan.0
PARSE ERROR         ("inf.0" #f) => +inf.0
PARSE ERROR         ("#e+nan.0" #f) => +nan.0
PARSE ERROR         ("#e+inf.0" #f) => +inf.0
PARSE ERROR         ("#e-inf.0" #f) => -inf.0
OK                  ("#i+nan.0" +nan.0 "+nan.0" "+NaN.0")
OK                  ("#i+inf.0" +inf.0 "+inf.0" "+Inf.0")
OK                  ("#i-inf.0" -inf.0 "-inf.0" "-Inf.0")

-> Fractions
-----------------------------------------------------
OK                  ("1/2" 1/2)
OK                  ("#e1/2" 1/2 "1/2")
OK                  ("10/2" 5 "5")
OK                  ("-1/2" -1/2)
OK                  ("0/10" 0 "0")
OK                  ("#e0/10" 0 "0")
OK                  ("#e1#/2" 5 15/2 "5" "15/2")
OK                  ("#e1/2#" 1/20 "1/20")
OK                  ("#i3/2" 1.5 "1.5")
OK                  ("1/-2" #f)
OK                  ("1.0/2" #f)
OK                  ("1/2.0" #f)
OK                  ("1/2e2" #f)
OK                  ("1/2e2" #f)
OK                  ("1#/2" 5.0 7.5 "5.0" "5." "7.5")
OK                  ("1/2#" 0.05 "0.05" ".05" "50.0e-3")
OK                  ("1#/#" #f)
OK                  ("1/" #f)
OK                  ("1/+" #f)
OK                  ("+/1" #f)
OK                  ("/1" #f)
OK                  ("/" #f)

-> Basic complex numbers (rectangular notation)
-----------------------------------------------------
OK                  ("1+2i" 1.0+2.0i "1.0+2.0i" "1.+2.i")
OK                  ("1+2I" 1.0+2.0i "1.0+2.0i" "1.+2.i")
OK                  ("1-2i" 1.0-2.0i "1.0-2.0i" "1.-2.i")
OK                  ("-1+2i" -1.0+2.0i "-1.0+2.0i" "-1.+2.i")
OK                  ("-1-2i" -1.0-2.0i "-1.0-2.0i" "-1.-2.i")
OK                  ("+i" 0.0+1.0i "+1.i" "+1.0i" "0.+1.i" "0.0+1.0i")
OK                  ("0+i" 0.0+1.0i "0+1i" "+1.i" "+1.0i" "0.+1.i" "0.0+1.0i")
OK                  ("0+1i" 0.0+1.0i "+1.i" "+1.0i" "0.+1.i" "0.0+1.0i")
OK                  ("-i" 0.0-1.0i "-1.i" "-1.0i" "0.-1.i" "0.0-1.0i")
OK                  ("0-i" 0.0-1.0i "-1.i" "-1.0i" "0.-1.i" "0.0-1.0i")
OK                  ("0-1i" 0.0-1.0i "-1.i" "-1.0i" "0.-1.i" "0.0-1.0i")
OK                  ("+2i" 0.0+2.0i "+2.0i" "+2.i" "0.+2.i" "0.0+2.0i")
OK                  ("-2i" 0.0-2.0i "-2.0i" "-2.i" "0.-2.i" "0.0-2.0i")
OK                  ("1#+1#i" 10.0+10.0i 15.0+15.0i "10.0+10.0i" "10.+10.i" "15.0+15.0i" "15.+15.i")
OK                  ("2i" #f)
OK                  ("+-i" #f)
OK                  ("i" #f)
OK                  ("1+2i1" #f)
OK                  ("1+2" #f)
OK                  ("1#+#i" #f)

-> Decimal-notation complex numbers (rectangular notation)
-----------------------------------------------------
OK                  ("1.0+2i" 1.0+2.0i "1.0+2.0i" "1.0+2i" "1.+2i" "1.+2.i")
OK                  ("1+2.0i" 1.0+2.0i "1.0+2.0i" "1+2.0i" "1.+2.i" "1+2.i")
OK                  ("1#.+1#.i" 10.0+10.0i 15.0+15.0i "10.0+10.0i" "10.+10.i" "15.0+15.0i" "15.+15.i")
OK                  ("1e2+1.0i" 100.0+1.0i "100.0+1.0i" "100.+1.i")
OK                  ("1s2+1.0i" 100.0+1.0i "100.0+1.0i" "100.+1.i")
OK                  ("1.0+1e2i" 1.0+100.0i "1.0+100.0i" "1.+100.i")
OK                  ("1.0+1s2i" 1.0+100.0i "1.0+100.0i" "1.+100.i")
OK                  ("1#e2+1.0i" 1000.0+1.0i 1500.0+1.0i "1000.0+1.0i" "1000.+1.i" "1500.0+1.0i" "1500.+1.i" "1.0e3+1.0i" "15.0e2+1.0i")
OK                  ("1.0+1#e2i" 1.0+1000.0i 1.0+1500.0i "1.0+1000.0i" "1.+1000.i" "1.0+1500.0i" "1.+1500.i" "1.0+1.0e3i" "1.0+15.0e2i")
OK                  (".i" #f)
OK                  ("+.i" #f)
OK                  (".+i" #f)

-> Fractional complex numbers (rectangular notation)
-----------------------------------------------------
OK                  ("1/2+3/4i" 0.5+0.75i "0.5+0.75i" ".5+.75i" "500.0e-3+750.0e-3i")

-> Mixed fractional/decimal notation complex numbers (rectangular notation)
-----------------------------------------------------
OK                  ("1#/2+3/4i" 5.0+0.75i 7.5+0.75i "5.0+0.75i" "5.+.75i" "7.5+0.75i" "5.0+3/4i" "5.+3/4i" "7.5+3/4i" "5.0+750.0e-3i")
OK                  ("0.5+3/4i" 0.5+0.75i "0.5+0.75i" ".5+.75i" "0.5+3/4i" ".5+3/4i" "500.0e-3+750.0e-3i")
OK                  ("1.5+1#/4i" 1.5+2.5i 1.5+3.75i "1.5+2.5i" "1.5+3.75i")
OK                  ("0.5+1/#i" #f)
OK                  ("0.5+1/1#2i" #f)
OK                  ("1/#+0.5i" #f)
OK                  ("1/1#2+0.5i" #f)

-> Mixed notation with infinity (might fail on mixed exactness compnums)
-----------------------------------------------------

-> Complex NaN, Inf (rectangular notation)
-----------------------------------------------------
OK                  ("+nan.0+nan.0i" +nan.0+nan.0i "+NaN.0+NaN.0i")
OK                  ("+inf.0+inf.0i" +inf.0+inf.0i "+Inf.0+Inf.0i")
OK                  ("-inf.0+inf.0i" -inf.0+inf.0i "-Inf.0+Inf.0i")
OK                  ("-inf.0-inf.0i" -inf.0-inf.0i "-Inf.0-Inf.0i")
OK                  ("+inf.0-inf.0i" +inf.0-inf.0i "+Inf.0-Inf.0i")

-> Complex numbers (polar notation)
-----------------------------------------------------
OK                  ("1@2i" #f)
OK                  ("0.5@1/#" #f)
OK                  ("0.5@1/1#2" #f)
OK                  ("1/#@0.5" #f)
OK                  ("1/1#2@0.5" #f)
OK                  ("1@" #f)
OK                  ("1#@#" #f)
OK                  ("1/@" #f)
OK                  ("@/1" #f)
OK                  ("@1" #f)
OK                  ("1@+" #f)
OK                  ("+@1" #f)
OK                  ("@" #f)

-> Base prefixes
-----------------------------------------------------
OK                  ("#x11" 17 "17")
OK                  ("#X11" 17 "17")
OK                  ("#d11" 11 "11")
OK                  ("#D11" 11 "11")
OK                  ("#o11" 9 "9")
OK                  ("#O11" 9 "9")
OK                  ("#b11" 3 "3")
OK                  ("#B11" 3 "3")
OK                  ("#da1" #f)
OK                  ("#o8" #f)
OK                  ("#b2" #f)
OK                  ("#o7" 7 "7")
OK                  ("#xa" 10 "10")
OK                  ("#xA" 10 "10")
OK                  ("#xf" 15 "15")
OK                  ("#xg" #f)
OK                  ("#x-10" -16 "-16")
OK                  ("#d-10" -10 "-10")
OK                  ("#o-10" -8 "-8")
OK                  ("#b-10" -2 "-2")
OK                  ("-#x10" #f)
OK                  ("-#d10" #f)
OK                  ("-#o10" #f)
OK                  ("-#b10" #f)
OK                  ("#x-" #f)
OK                  ("#x" #f)
OK                  ("#d" #f)
OK                  ("#d-" #f)
OK                  ("#d+" #f)
OK                  ("#o" #f)
OK                  ("#o-" #f)
OK                  ("#b" #f)
OK                  ("#b-" #f)
OK                  ("#e" #f)
OK                  ("#e-" #f)
OK                  ("#i" #f)
OK                  ("#i-" #f)

-> Combination of prefixes
-----------------------------------------------------
OK                  ("#x#x11" #f)
OK                  ("#x#b11" #f)
OK                  ("#b#o11" #f)
OK                  ("#e#x10" 16 "16")
OK                  ("#i#x10" 16.0 "16.0" "16.")
OK                  ("#e#e10" #f)
OK                  ("#e#e#x10" #f)
OK                  ("#E#e#X10" #f)
OK                  ("#i#e#x10" #f)
OK                  ("#e#x#e10" #f)
OK                  ("#x#x#e10" #f)
OK                  ("#x#e#x10" #f)

-> Base prefixes with padding
-----------------------------------------------------
OK                  ("#x1#0" #f)
OK                  ("#d1#0" #f)
OK                  ("#o1#0" #f)
OK                  ("#b1#0" #f)
OK                  ("#x1#" 16.0 24.0 "16.0" "24.0" "16." "24.")
OK                  ("#d1#" 10.0 15.0 "10.0" "15.0" "10." "15.")
OK                  ("#o1#" 8.0 12.0 "8.0" "12.0" "8." "12.")
OK                  ("#b1#" 2.0 3.0 "2.0" "3.0" "2." "3.")

-> (Attempted) decimal notation with base prefixes
-----------------------------------------------------
OK                  ("#x1.0" #f)
OK                  ("#d1.0" 1.0 "1.0" "1.")
OK                  ("#o1.0" #f)
OK                  ("#b1.0" #f)
OK                  ("#x1.#" #f)
OK                  ("#d1.#" 1.0 1.5 "1.0" "1.5" "1.")
OK                  ("#o1.#" #f)
OK                  ("#b1.#" #f)
OK                  ("#x1." #f)
OK                  ("#d1." 1.0 "1.0" "1.")
OK                  ("#o1." #f)
OK                  ("#b1." #f)
OK                  ("#x.1" #f)
OK                  ("#d.1" 0.1 "0.1" ".1" "100.0e-3")
OK                  ("#o.1" #f)
OK                  ("#b.1" #f)
OK                  ("#x1e2" 482 "482")
OK                  ("#d1e2" 100.0 "100.0" "100.")
OK                  ("#o1e2" #f)
OK                  ("#b1e2" #f)

-> Fractions with prefixes
-----------------------------------------------------
OK                  ("#x10/2" 8 "8")
OK                  ("#x11/2" 17/2 "17/2")
OK                  ("#d11/2" 11/2 "11/2")
OK                  ("#o11/2" 9/2 "9/2")
OK                  ("#b11/10" 3/2 "3/2")
OK                  ("#b11/2" #f)
OK                  ("#x10/#o10" #f)
OK                  ("10/#o10" #f)
OK                  ("#x1#/2" 8.0 12.0 "8.0" "8." "12.0" "12.")
OK                  ("#d1#/2" 5.0 7.5 "5.0" "5." "7.5")
OK                  ("#o1#/2" 4.0 6.0 "4.0" "4." "6.0" "6.")
OK                  ("#b1#/2" #f)
OK                  ("#b1#/10" 1.0 1.5 "1.0" "1." "1.5")

-> Complex numbers with prefixes
-----------------------------------------------------
OK                  ("#x1#+1#i" 16.0+16.0i 24.0+24.0i "16.0+16.0i" "16.+16.i" "24.0+24.0i" "24.+24.i")
OK                  ("#x1.0+1.0i" #f)
OK                  ("#d1.0+1.0i" 1.0+1.0i "1.0+1.0i" "1.+1.i")
OK                  ("#o1.0+1.0i" #f)
OK                  ("#b1.0+1.0i" #f)
OK                  ("#x10+#o10i" #f)
OK                  ("10+#o10i" #f)
OK                  ("#x1@#x1" #f)
OK                  ("1@#x1" #f)
OK                  ("#x10+11i" 16.0+17.0i "16.0+17.0i" "16.+17.i")
OK                  ("#d10+11i" 10.0+11.0i "10.0+11.0i" "10.+11.i")
OK                  ("#o10+11i" 8.0+9.0i "8.0+9.0i" "8.+9.i")
OK                  ("#b10+11i" 2.0+3.0i "2.0+3.0i" "2.+3.i")

-----> TOTAL ERRORS: 10

Some deprecated features have been used.  Set the environment
variable GUILE_WARN_DEPRECATED to "detailed" and rerun the
program to get more information.  Set it to "no" to suppress
this message.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#11887: string->number edge cases
  2012-07-09 12:29 bug#11887: string->number edge cases Ian Price
@ 2013-03-05 14:49 ` Andy Wingo
  2013-03-05 19:04   ` Mark H Weaver
  2013-03-05 19:06   ` bug#11887: string->number edge cases Peter Bex
  0 siblings, 2 replies; 8+ messages in thread
From: Andy Wingo @ 2013-03-05 14:49 UTC (permalink / raw)
  To: Ian Price; +Cc: Peter.Bex, 11887

Hi Ian,

On Mon 09 Jul 2012 14:29, Ian Price <ianprice90@googlemail.com> writes:

> PARSE ERROR         ("+InF.0" +inf.0 "+inf.0" "+Inf.0") => #f
> PARSE ERROR         ("-iNF.0" -inf.0 "-inf.0" "-Inf.0") => #f
> PARSE ERROR         ("+NAN.0" +nan.0 "+nan.0" "+NaN.0") => #f

These are not errors.  +NAN.0 is not even not a number :)

> PARSE ERROR         ("+nan.1" #f) => +nan.0
> PARSE ERROR         ("+nan.01" #f) => +nan.0

These are only supported because 2.0.0 was released with +nan.1 parsing
as +nan.0.  It signals a deprecation warning with a note to this effect.
Guile from master should pass these particular tests.

> PARSE ERROR         ("nan.0" #f) => +nan.0
> PARSE ERROR         ("inf.0" #f) => +inf.0
> PARSE ERROR         ("#e+nan.0" #f) => +nan.0
> PARSE ERROR         ("#e+inf.0" #f) => +inf.0
> PARSE ERROR         ("#e-inf.0" #f) => -inf.0

These are errors.

> If the number contains a division by zero, we get a numerical overflow
> error.
>
> scheme@(guile−user)> (string->number "3/0")
> ERROR: In procedure string−>number:
> ERROR: Throw to key `numerical−overflow' with args `("make−ratio" "Numerical overflow" #f #f)'.

This is also an error.  We should plumb through some extra arg to
mem2ureal, I guess, to check for a zero denominator.

Andy
-- 
http://wingolog.org/





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#11887: string->number edge cases
  2013-03-05 14:49 ` Andy Wingo
@ 2013-03-05 19:04   ` Mark H Weaver
  2013-03-05 19:14     ` Peter Bex
  2013-03-05 19:06   ` bug#11887: string->number edge cases Peter Bex
  1 sibling, 1 reply; 8+ messages in thread
From: Mark H Weaver @ 2013-03-05 19:04 UTC (permalink / raw)
  To: Andy Wingo; +Cc: Ian Price, Peter.Bex, 11887

Andy Wingo <wingo@pobox.com> writes:

> On Mon 09 Jul 2012 14:29, Ian Price <ianprice90@googlemail.com> writes:
>
>> If the number contains a division by zero, we get a numerical overflow
>> error.
>>
>> scheme@(guile−user)> (string->number "3/0")
>> ERROR: In procedure string−>number:
>> ERROR: Throw to key `numerical−overflow' with args `("make−ratio" "Numerical overflow" #f #f)'.
>
> This is also an error.  We should plumb through some extra arg to
> mem2ureal, I guess, to check for a zero denominator.

FYI, I produced a simple patch a while back to fix this (see below), but
it had an interesting side effect: it caused the reader to read things
like "3/0" and "4+3/0i" as symbols.  More generally, anything for which
'scm_string_to_number' returns false is treated as a symbol by 'read'.

I'm not sure how I feel about this.  What do you think?

     Mark


diff --git a/libguile/numbers.c b/libguile/numbers.c
index 66c95db..9013f0c 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -5809,7 +5809,7 @@ mem2ureal (SCM mem, unsigned int *p_idx,
             return SCM_BOOL_F;
 
 	  divisor = mem2uinteger (mem, &idx, radix, &implicit_x);
-	  if (scm_is_false (divisor))
+	  if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0))
 	    return SCM_BOOL_F;
 
 	  /* both are int/big here, I assume */





^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#11887: string->number edge cases
  2013-03-05 14:49 ` Andy Wingo
  2013-03-05 19:04   ` Mark H Weaver
@ 2013-03-05 19:06   ` Peter Bex
  1 sibling, 0 replies; 8+ messages in thread
From: Peter Bex @ 2013-03-05 19:06 UTC (permalink / raw)
  To: Andy Wingo; +Cc: Ian Price, 11887

On Tue, Mar 05, 2013 at 03:49:13PM +0100, Andy Wingo wrote:
> Hi Ian,
> 
> On Mon 09 Jul 2012 14:29, Ian Price <ianprice90@googlemail.com> writes:
> 
> > PARSE ERROR         ("+InF.0" +inf.0 "+inf.0" "+Inf.0") => #f
> > PARSE ERROR         ("-iNF.0" -inf.0 "-inf.0" "-Inf.0") => #f
> > PARSE ERROR         ("+NAN.0" +nan.0 "+nan.0" "+NaN.0") => #f
> 
> These are not errors.  +NAN.0 is not even not a number :)

I double-checked, but in the upcoming R7RS it is (see 7.1, paragraph 2 of
draft 7).  It looks like R6RS was case-sensitive in its numerical syntax
and +NAN.0 is disallowed by it.

Cheers,
Peter
-- 
http://www.more-magic.net





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#11887: string->number edge cases
  2013-03-05 19:04   ` Mark H Weaver
@ 2013-03-05 19:14     ` Peter Bex
  2013-03-06 18:11       ` Mark H Weaver
  2013-03-06 18:15       ` bug#11887: [PATCH] Improve standards conformance of string->number (was Re: bug#11887: string->number edge cases) Mark H Weaver
  0 siblings, 2 replies; 8+ messages in thread
From: Peter Bex @ 2013-03-05 19:14 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: Ian Price, 11887

On Tue, Mar 05, 2013 at 02:04:55PM -0500, Mark H Weaver wrote:
> FYI, I produced a simple patch a while back to fix this (see below), but
> it had an interesting side effect: it caused the reader to read things
> like "3/0" and "4+3/0i" as symbols.  More generally, anything for which
> 'scm_string_to_number' returns false is treated as a symbol by 'read'.

I think this is simple and at least internally consistent.  Several
Schemes assume something like 1/0 is a symbol; Chicken does this as
well, with the numbers egg loaded, so does Gambit.

Raising an error is also acceptable.  According to the lexical syntax
1/0 *is* a valid number, so you could argue that it *must* parse as
a number, which is impossible so an error should occur.

This is also related to how string->number deals with it; if it
returns #f it is essentially saying "this is not valid numerical syntax"
and should fail to parse as a number.  Not raising an exception on
(string->number "1/0) but raising an exception on
(with-input-from-string "1/0" read) is a bit odd, I'd say.

Cheers,
Peter
-- 
http://www.more-magic.net





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#11887: string->number edge cases
  2013-03-05 19:14     ` Peter Bex
@ 2013-03-06 18:11       ` Mark H Weaver
  2013-03-06 18:15       ` bug#11887: [PATCH] Improve standards conformance of string->number (was Re: bug#11887: string->number edge cases) Mark H Weaver
  1 sibling, 0 replies; 8+ messages in thread
From: Mark H Weaver @ 2013-03-06 18:11 UTC (permalink / raw)
  To: Peter Bex; +Cc: Ian Price, 11887

Hi Peter,

Peter Bex <Peter.Bex@xs4all.nl> writes:

> On Tue, Mar 05, 2013 at 02:04:55PM -0500, Mark H Weaver wrote:
>> FYI, I produced a simple patch a while back to fix this (see below), but
>> it had an interesting side effect: it caused the reader to read things
>> like "3/0" and "4+3/0i" as symbols.  More generally, anything for which
>> 'scm_string_to_number' returns false is treated as a symbol by 'read'.
>
> I think this is simple and at least internally consistent.  Several
> Schemes assume something like 1/0 is a symbol; Chicken does this as
> well, with the numbers egg loaded, so does Gambit.
[...]
> This is also related to how string->number deals with it; if it
> returns #f it is essentially saying "this is not valid numerical syntax"
> and should fail to parse as a number.  Not raising an exception on
> (string->number "1/0) but raising an exception on
> (with-input-from-string "1/0" read) is a bit odd, I'd say.

Thanks, this makes me feel somewhat better about it.  I guess it's okay.

> On Tue, Mar 05, 2013 at 03:49:13PM +0100, Andy Wingo wrote:
> > Hi Ian,
> > 
> > On Mon 09 Jul 2012 14:29, Ian Price <ianprice90@googlemail.com> writes:
> > 
> > > PARSE ERROR         ("+InF.0" +inf.0 "+inf.0" "+Inf.0") => #f
> > > PARSE ERROR         ("-iNF.0" -inf.0 "-inf.0" "-Inf.0") => #f
> > > PARSE ERROR         ("+NAN.0" +nan.0 "+nan.0" "+NaN.0") => #f
> > 
> > These are not errors.  +NAN.0 is not even not a number :)
> 
> I double-checked, but in the upcoming R7RS it is (see 7.1, paragraph 2 of
> draft 7).  It looks like R6RS was case-sensitive in its numerical syntax
> and +NAN.0 is disallowed by it.

Thanks for looking this up.  Given that it's allowed by the latest R7RS
draft, I think we should allow these too.

I've written a patch to fix these issues, and will send it in my next
email.

    Regards,
      Mark





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#11887: [PATCH] Improve standards conformance of string->number (was Re: bug#11887: string->number edge cases)
  2013-03-05 19:14     ` Peter Bex
  2013-03-06 18:11       ` Mark H Weaver
@ 2013-03-06 18:15       ` Mark H Weaver
  2013-03-07 20:47         ` Mark H Weaver
  1 sibling, 1 reply; 8+ messages in thread
From: Mark H Weaver @ 2013-03-06 18:15 UTC (permalink / raw)
  To: 11887; +Cc: Ian Price

[-- Attachment #1: Type: text/plain, Size: 85 bytes --]

Here's a patch to fix these problems.  Comments and suggestions welcome.

    Mark



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: [PATCH] Improve standards conformance of string->number --]
[-- Type: text/x-diff, Size: 6468 bytes --]

From a1926777b03445d397bb1069b325d243e765f84b Mon Sep 17 00:00:00 2001
From: Mark H Weaver <mhw@netris.org>
Date: Wed, 6 Mar 2013 12:52:39 -0500
Subject: [PATCH] Improve standards conformance of string->number.

Fixes <http://bugs.gnu.org/11887>.

* libguile/numbers.c (mem2ureal): New argument 'allow_inf_or_nan'.
  Accept infinities and NaNs only if 'allow_inf_or_nan' is true and "#e"
  is not present.  Check for "inf.0" or "nan." case-insensitively.  Do
  not accept rationals with zero divisors.

  (mem2complex): Pass new 'allow_inf_or_nan' argument to 'mem2ureal',
  which is set if and only if a explicit sign was present.

* test-suite/tests/numbers.test ("string->number"): Add tests.
---
 libguile/numbers.c            |   76 +++++++++++++++++++++++++++--------------
 test-suite/tests/numbers.test |   12 ++++++-
 2 files changed, 61 insertions(+), 27 deletions(-)

diff --git a/libguile/numbers.c b/libguile/numbers.c
index 66c95db..f9538f5 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -5740,7 +5740,8 @@ mem2decimal_from_point (SCM result, SCM mem,
 
 static SCM
 mem2ureal (SCM mem, unsigned int *p_idx,
-	   unsigned int radix, enum t_exactness forced_x)
+	   unsigned int radix, enum t_exactness forced_x,
+           int allow_inf_or_nan)
 {
   unsigned int idx = *p_idx;
   SCM result;
@@ -5753,30 +5754,53 @@ mem2ureal (SCM mem, unsigned int *p_idx,
   if (idx == len)
     return SCM_BOOL_F;
 
-  if (idx+5 <= len && !scm_i_string_strcmp (mem, idx, "inf.0"))
-    {
-      *p_idx = idx+5;
-      return scm_inf ();
-    }
-
-  if (idx+4 < len && !scm_i_string_strcmp (mem, idx, "nan."))
-    {
-      /* Cobble up the fractional part.  We might want to set the
-	 NaN's mantissa from it. */
-      idx += 4;
-      if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x), SCM_INUM0))
-        {
+  if (allow_inf_or_nan && forced_x != EXACT && idx+5 <= len)
+    switch (scm_i_string_ref (mem, idx))
+      {
+      case 'i': case 'I':
+        switch (scm_i_string_ref (mem, idx + 1))
+          {
+          case 'n': case 'N':
+            switch (scm_i_string_ref (mem, idx + 2))
+              {
+              case 'f': case 'F':
+                if (scm_i_string_ref (mem, idx + 3) == '.'
+                    && scm_i_string_ref (mem, idx + 4) == '0')
+                  {
+                    *p_idx = idx+5;
+                    return scm_inf ();
+                  }
+              }
+          }
+      case 'n': case 'N':
+        switch (scm_i_string_ref (mem, idx + 1))
+          {
+          case 'a': case 'A':
+            switch (scm_i_string_ref (mem, idx + 2))
+              {
+              case 'n': case 'N':
+                if (scm_i_string_ref (mem, idx + 3) == '.')
+                  {
+                    /* Cobble up the fractional part.  We might want to
+                       set the NaN's mantissa from it. */
+                    idx += 4;
+                    if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x),
+                                    SCM_INUM0))
+                      {
 #if SCM_ENABLE_DEPRECATED == 1
-          scm_c_issue_deprecation_warning
-            ("Non-zero suffixes to `+nan.' are deprecated.  Use `+nan.0'.");
+                        scm_c_issue_deprecation_warning
+                          ("Non-zero suffixes to `+nan.' are deprecated.  Use `+nan.0'.");
 #else
-          return SCM_BOOL_F;
+                        return SCM_BOOL_F;
 #endif
-        }
+                      }
           
-      *p_idx = idx;
-      return scm_nan ();
-    }
+                    *p_idx = idx;
+                    return scm_nan ();
+                  }
+              }
+          }
+      }
 
   if (scm_i_string_ref (mem, idx) == '.')
     {
@@ -5809,7 +5833,7 @@ mem2ureal (SCM mem, unsigned int *p_idx,
             return SCM_BOOL_F;
 
 	  divisor = mem2uinteger (mem, &idx, radix, &implicit_x);
-	  if (scm_is_false (divisor))
+	  if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0))
 	    return SCM_BOOL_F;
 
 	  /* both are int/big here, I assume */
@@ -5885,7 +5909,7 @@ mem2complex (SCM mem, unsigned int idx,
   if (idx == len)
     return SCM_BOOL_F;
 
-  ureal = mem2ureal (mem, &idx, radix, forced_x);
+  ureal = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
   if (scm_is_false (ureal))
     {
       /* input must be either +i or -i */
@@ -5954,9 +5978,9 @@ mem2complex (SCM mem, unsigned int idx,
 		  sign = -1;
 		}
 	      else
-		sign = 1;
+		sign = 0;
 
-	      angle = mem2ureal (mem, &idx, radix, forced_x);
+	      angle = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
 	      if (scm_is_false (angle))
 		return SCM_BOOL_F;
 	      if (idx != len)
@@ -5978,7 +6002,7 @@ mem2complex (SCM mem, unsigned int idx,
 	  else
 	    {
 	      int sign = (c == '+') ? 1 : -1;
-	      SCM imag = mem2ureal (mem, &idx, radix, forced_x);
+	      SCM imag = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
 
 	      if (scm_is_false (imag))
 		imag = SCM_I_MAKINUM (sign);
diff --git a/test-suite/tests/numbers.test b/test-suite/tests/numbers.test
index 66aa01a..be378b7 100644
--- a/test-suite/tests/numbers.test
+++ b/test-suite/tests/numbers.test
@@ -1493,7 +1493,9 @@
 		"#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2"
 		"#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc"
 		"#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1"
-		"#i#i1" "12@12+0i"))
+		"#i#i1" "12@12+0i" "3/0" "0/0" "4+3/0i" "4/0-3i" "2+0/0i"
+                "nan.0" "inf.0" "#e+nan.0" "#e+inf.0" "#e-inf.0"
+                "3@inf.0" "4@nan.0"))
     #t)
 
   (pass-if "valid number strings"
@@ -1532,6 +1534,14 @@
                 ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0)
                 ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1)
                 ("#i6/8" 0.75) ("#i1/1" 1.0)
+                ;; Infinities and NaNs:
+                ("+inf.0" ,(inf)) ("-inf.0" ,(- (inf)))
+                ("+Inf.0" ,(inf)) ("-Inf.0" ,(- (inf)))
+                ("+InF.0" ,(inf)) ("-InF.0" ,(- (inf)))
+                ("+INF.0" ,(inf)) ("-INF.0" ,(- (inf)))
+                ("#i+InF.0" ,(inf)) ("#i-InF.0" ,(- (inf)))
+                ("+nan.0" ,(nan)) ("-nan.0" ,(nan))
+                ("#i+nan.0" ,(nan)) ("#i-nan.0" ,(nan))
                 ;; Decimal numbers:
                 ;; * <uinteger 10> <suffix>
                 ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0)
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#11887: [PATCH] Improve standards conformance of string->number (was Re: bug#11887: string->number edge cases)
  2013-03-06 18:15       ` bug#11887: [PATCH] Improve standards conformance of string->number (was Re: bug#11887: string->number edge cases) Mark H Weaver
@ 2013-03-07 20:47         ` Mark H Weaver
  0 siblings, 0 replies; 8+ messages in thread
From: Mark H Weaver @ 2013-03-07 20:47 UTC (permalink / raw)
  To: 11887-done; +Cc: Ian Price

I wrote:
> Here's a patch to fix these problems.  Comments and suggestions welcome.

I went ahead and pushed this to stable-2.0.  I'm closing this bug.

    Thanks,
      Mark





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-03-07 20:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-09 12:29 bug#11887: string->number edge cases Ian Price
2013-03-05 14:49 ` Andy Wingo
2013-03-05 19:04   ` Mark H Weaver
2013-03-05 19:14     ` Peter Bex
2013-03-06 18:11       ` Mark H Weaver
2013-03-06 18:15       ` bug#11887: [PATCH] Improve standards conformance of string->number (was Re: bug#11887: string->number edge cases) Mark H Weaver
2013-03-07 20:47         ` Mark H Weaver
2013-03-05 19:06   ` bug#11887: string->number edge cases Peter Bex

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).