Arc Forumnew | comments | leaders | submitlogin
New arc: (coerce "1.234" 'int) doesn't
2 points by conanite 5442 days ago | 4 comments
The latest arc doesn't seem to offer a way to coerce real or complex numbers from a string - the title example raises an error (it didn't previously).

Given that 'num is a standard arc type, it seems fair to allow coercion from string to num. From (xdef coerce ...) in ac.scm:

  ((string? x)    (case type
                    ((sym)    (string->symbol x))
                    ((cons)   (ac-niltree (string->list x)))
                    ((int)    (let ((n (apply string->number x args)))
                                (if n 
                                    (iround n)
                                    (err "Can't coerce" x type))))
  +                 ((num)    (let ((n (apply string->number x args)))
  +                             (if n 
  +                                 n 
  +                                 (err "Can't coerce" x type))))
                    (else     (err "Can't coerce" x type))))

Now,

  (coerce "3.14" 'num)
and

  (coerce "3+4i" 'num)
both work.

Of course (read "3.14") is a workaround, but it still seems wrong that 'coerce isn't doing this.



1 point by CatDancer 5442 days ago | link

Works fine for me in arc3:

  arc> (coerce "1.234" 'int)
  1
or did you mean to say (coerce "1.234" 'num) ?

-----

2 points by conanite 5442 days ago | link

in arc 2,

  arc> (coerce "1.234" 'int)
  1.234
arguably, this is incorrect, because 1.234 obviously isn't an integer, so arc3 improves on this by rounding. But (coerce "1.234" 'num) isn't available (in arc 2 or 3), so there's no way to use coerce for non-integers.

-----

2 points by pg 5441 days ago | link

I added a clause so (coerce "1.234" 'num) works. Thanks.

-----

1 point by conanite 5441 days ago | link

Cool, thanks. My test scripts were unhappy.

-----