mes: string->number: Support #x-prefixed hex numbers.
* mes/module/mes/scm.mes (string->number): Support "#x"-prefix. * tests/math.test ("string->number #hex"): Test it.
This commit is contained in:
parent
26891251a6
commit
dd34569751
|
@ -229,6 +229,7 @@
|
|||
(equal? (substring string (- length suffix-length)) suffix))))
|
||||
|
||||
(define (string->number s . rest)
|
||||
(if (string-prefix? "#x" s) (string->number (string-drop s 2) 16)
|
||||
(let ((lst (string->list s)))
|
||||
(and (pair? lst)
|
||||
(let* ((radix (if (null? rest) 10 (car rest)))
|
||||
|
@ -256,7 +257,7 @@
|
|||
(if (null? fraction) n
|
||||
(let ((fraction ((compose string->number list->string) fraction)))
|
||||
(and fraction n))))) ; FLOAT as integer
|
||||
(else #f)))))))))
|
||||
(else #f))))))))))
|
||||
|
||||
(define inexact->exact identity)
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ exec ${MES-src/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests
|
|||
;;; -*-scheme-*-
|
||||
|
||||
;;; GNU Mes --- Maxwell Equations of Software
|
||||
;;; Copyright © 2016,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||
;;; Copyright © 2016,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||
;;;
|
||||
;;; This file is part of GNU Mes.
|
||||
;;;
|
||||
|
@ -30,6 +30,7 @@ exec ${MES-src/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests
|
|||
(mes-use-module (mes test))
|
||||
(pass-if-equal "string->number" 42 (string->number "42"))
|
||||
(pass-if-equal "string->number neg" -42 (string->number "-42"))
|
||||
(pass-if-equal "string->number #hex" 170 (string->number "#xaa"))
|
||||
(pass-if-not "string->number hex" (string->number "aa"))
|
||||
(pass-if-equal "string->number hex" 170 (string->number "aa" 16))
|
||||
(pass-if-equal "string->number float" 1 (inexact->exact (string->number "1.0")))
|
||||
|
|
Loading…
Reference in a new issue