Check numeric argument to real->decimal-string

Closes #3565
This commit is contained in:
David Van Horn 2020-12-30 07:09:14 -05:00 committed by GitHub
parent 15bda57d77
commit 001ad13297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -1132,7 +1132,7 @@ that the @racket[read-single-flonum] parameter affects @racket[read].
#:changed "7.3.0.5" @elem{Added the @racket[single-mode] argument.}]}
@defproc[(real->decimal-string [n real?] [decimal-digits exact-nonnegative-integer? 2])
@defproc[(real->decimal-string [n rational?] [decimal-digits exact-nonnegative-integer? 2])
string?]{
Prints @racket[n] into a string and returns the string. The printed
@ -1147,6 +1147,12 @@ process is an exact number whose decimal representation has no more
than @racket[decimal-digits] digits after the decimal (and it is
padded with trailing zeros if necessary).
If @racket[n] is a real number with no decimal representation (e.g.
@racket[+nan.0], @racket[+inf.0]), then the @exnraise[exn:fail:contract].
(Any real number that is convertible to decimal notation is rational,
so @racket[n] must be @racket[rational?], despite the name of the
function.)
@mz-examples[
#:eval math-eval
(real->decimal-string pi)

View File

@ -15,6 +15,9 @@
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (real->decimal-string n [digits 2])
(unless (rational? n)
(raise-argument-error 'real->decimal-string "rational?"
n))
(unless (exact-nonnegative-integer? digits)
(raise-argument-error 'real->decimal-string "exact-nonnegative-integer?"
digits))