Skip to content

Commit 6636281

Browse files
authored
Merge pull request #1399 from herbie-fp/faster-dump-rival
Make `dump:rival` faster and more correct
2 parents 745d147 + a9d1d52 commit 6636281

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/core/rival.rkt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
machine
9595
dump-file))
9696

97+
(define (bigfloat->readable-string x)
98+
(define real (bigfloat->real x)) ; Exact rational unless inf/nan
99+
(define float (real->double-flonum real))
100+
(if (= real float)
101+
(format "#i~a" float) ; The #i explicitly means nearest float
102+
(number->string real))) ; Backup is print as rational
103+
97104
;; Runs a Rival machine on an input point.
98105
(define (real-apply compiler pt [hint #f])
99106
(match-define (real-compiler _ vars var-reprs _ _ machine dump-file) compiler)
@@ -104,9 +111,8 @@
104111
[repr (in-vector var-reprs)])
105112
((representation-repr->bf repr) val)))
106113
(when dump-file
107-
(define args (map bigfloat->rational (vector->list pt*)))
108-
;; convert to rational, because Rival reads as exact
109-
(pretty-print `(eval f ,@args) dump-file 1))
114+
(define args (map bigfloat->readable-string (vector->list pt*)))
115+
(fprintf dump-file "(eval f ~a)\n" (string-join args " ")))
110116
(define-values (status value)
111117
(with-handlers ([exn:rival:invalid? (lambda (e) (values 'invalid #f))]
112118
[exn:rival:unsamplable? (lambda (e) (values 'exit #f))])
@@ -136,7 +142,7 @@
136142
(timeline-push!/unsafe 'outcomes
137143
(- (current-inexact-milliseconds) start)
138144
(rival-profile machine 'iterations)
139-
(~a status)
145+
(symbol->string status)
140146
1)
141147
(values status value))
142148

0 commit comments

Comments
 (0)