Skip to content

Commit 523c59a

Browse files
committed
Split epfn helper into unary and binary variants
1 parent c89fbe7 commit 523c59a

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

ops/core.rkt

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
new-ival
1616
ival-exact-fabs
1717
ival-maybe
18-
epfn
18+
epfn1
19+
epfn2
1920
split-ival
2021
ival-max-prec
2122
ival-exact-neg
@@ -206,16 +207,22 @@
206207
(or (ival-err? x) (ival-err? y))
207208
(and (ival-err x) (ival-err y)))]
208209
[(boolean? (ival-lo-val x))
209-
(ival (epfn and-fn (ival-lo x) (ival-lo y))
210-
(epfn or-fn (ival-hi x) (ival-hi y))
210+
(ival (epfn2 and-fn (ival-lo x) (ival-lo y))
211+
(epfn2 or-fn (ival-hi x) (ival-hi y))
211212
(or (ival-err? x) (ival-err? y))
212213
(and (ival-err x) (ival-err y)))]))
213214

214-
;; This function computes and propagates the immovable? flag for endpoints
215-
(define (epfn op . args)
216-
(define args-bf (map endpoint-val args))
217-
(define-values (result exact?) (bf-return-exact? op args-bf))
218-
(endpoint result (and (andmap endpoint-immovable? args) exact?)))
215+
;; These functions compute and propagate the immovable? flag for endpoints
216+
(define (epfn1 op arg)
217+
(define-values (result exact?) (bf-return-exact? op (list (endpoint-val arg))))
218+
(endpoint result (and (endpoint-immovable? arg) exact?)))
219+
220+
(define (epfn2 op arg0 arg1)
221+
(define-values (result exact?)
222+
(bf-return-exact? op (list (endpoint-val arg0) (endpoint-val arg1))))
223+
(endpoint result (and (endpoint-immovable? arg0)
224+
(endpoint-immovable? arg1)
225+
exact?)))
219226

220227
;; Helpers for defining interval functions
221228

@@ -224,11 +231,11 @@
224231

225232
(define ((monotonic bffn) x)
226233
(match-define (ival lo hi err? err) x)
227-
(ival (rnd 'down epfn bffn lo) (rnd 'up epfn bffn hi) err? err))
234+
(ival (rnd 'down epfn1 bffn lo) (rnd 'up epfn1 bffn hi) err? err))
228235

229236
(define ((comonotonic bffn) x)
230237
(match-define (ival lo hi err? err) x)
231-
(ival (rnd 'down epfn bffn hi) (rnd 'up epfn bffn lo) err? err))
238+
(ival (rnd 'down epfn1 bffn hi) (rnd 'up epfn1 bffn lo) err? err))
232239

233240
(define ((close-enough->ival bffn) x)
234241
(match-define (ival (endpoint lo lo!) (endpoint hi hi!) err? err) x)
@@ -302,7 +309,7 @@
302309
[0
303310
(match-define (ival (endpoint xlo xlo!) (endpoint xhi xhi!) xerr? xerr) x)
304311
(ival (endpoint (bf 0) (and xlo! xhi!))
305-
(rnd 'up endpoint-max2 (epfn bfabs (ival-lo x)) (ival-hi x))
312+
(rnd 'up endpoint-max2 (epfn1 bfabs (ival-lo x)) (ival-hi x))
306313
(ival-err? x)
307314
(ival-err x))]))
308315

@@ -355,7 +362,7 @@
355362
(ormap ival-err as)))
356363

357364
(define (ival-not x)
358-
(ival (epfn not (ival-hi x)) (epfn not (ival-lo x)) (ival-err? x) (ival-err x)))
365+
(ival (epfn1 not (ival-hi x)) (epfn1 not (ival-lo x)) (ival-err? x) (ival-err x)))
359366

360367
(define* ival-asin (compose (monotonic bfasin) (clamp -1.bf 1.bf)))
361368
(define* ival-acos (compose (comonotonic bfacos) (clamp -1.bf 1.bf)))
@@ -369,7 +376,7 @@
369376
(define err (or (ival-err x) (ival-err y)))
370377

371378
(define (mkatan a b c d)
372-
(ival (rnd 'down epfn bfatan2 a b) (rnd 'up epfn bfatan2 c d) err? err))
379+
(ival (rnd 'down epfn2 bfatan2 a b) (rnd 'up epfn2 bfatan2 c d) err? err))
373380

374381
(match* ((classify-ival-strict x) (classify-ival-strict y))
375382
[(-1 -1) (mkatan yhi xlo ylo xhi)]
@@ -404,10 +411,10 @@
404411
(define* ival-erfc (comonotonic bferfc))
405412

406413
(define (ival-cmp x y)
407-
(define can-< (epfn bflt? (ival-lo x) (ival-hi y)))
408-
(define must-< (epfn bflt? (ival-hi x) (ival-lo y)))
409-
(define can-> (epfn bfgt? (ival-hi x) (ival-lo y)))
410-
(define must-> (epfn bfgt? (ival-lo x) (ival-hi y)))
414+
(define can-< (epfn2 bflt? (ival-lo x) (ival-hi y)))
415+
(define must-< (epfn2 bflt? (ival-hi x) (ival-lo y)))
416+
(define can-> (epfn2 bfgt? (ival-hi x) (ival-lo y)))
417+
(define must-> (epfn2 bfgt? (ival-lo x) (ival-hi y)))
411418
(values can-< must-< can-> must->))
412419

413420
(define (ival-<2 x y)
@@ -416,20 +423,20 @@
416423

417424
(define (ival-<=2 x y)
418425
(define-values (c< m< c> m>) (ival-cmp x y))
419-
(ival (epfn not c>) (epfn not m>) (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
426+
(ival (epfn1 not c>) (epfn1 not m>) (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
420427

421428
(define (ival->2 x y)
422429
(define-values (c< m< c> m>) (ival-cmp x y))
423430
(ival m> c> (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
424431

425432
(define (ival->=2 x y)
426433
(define-values (c< m< c> m>) (ival-cmp x y))
427-
(ival (epfn not c<) (epfn not m<) (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
434+
(ival (epfn1 not c<) (epfn1 not m<) (or (ival-err? x) (ival-err? y)) (or (ival-err x) (ival-err y))))
428435

429436
(define (ival-==2 x y)
430437
(define-values (c< m< c> m>) (ival-cmp x y))
431-
(ival (epfn and-fn (epfn not c<) (epfn not c>))
432-
(epfn and-fn (epfn not m<) (epfn not m>))
438+
(ival (epfn2 and-fn (epfn1 not c<) (epfn1 not c>))
439+
(epfn2 and-fn (epfn1 not m<) (epfn1 not m>))
433440
(or (ival-err? x) (ival-err? y))
434441
(or (ival-err x) (ival-err y))))
435442

@@ -449,8 +456,8 @@
449456

450457
(define (ival-!=2 x y)
451458
(define-values (c< m< c> m>) (ival-cmp x y))
452-
(ival (epfn or-fn m< m>)
453-
(epfn or-fn c< c>)
459+
(ival (epfn2 or-fn m< m>)
460+
(epfn2 or-fn c< c>)
454461
(or (ival-err? x) (ival-err? y))
455462
(or (ival-err x) (ival-err y))))
456463

@@ -524,8 +531,8 @@
524531
(define err? (or (ival-err? y) xerr?))
525532
(define err (or (ival-err y) xerr))
526533
(match* (can-neg can-pos)
527-
[(#t #t) (ival (rnd 'down epfn bfneg xhi) (rnd 'up epfn bfcopy xhi) err? err)]
528-
[(#t #f) (ival (rnd 'down epfn bfneg xhi) (rnd 'up epfn bfneg xlo) err? err)]
534+
[(#t #t) (ival (rnd 'down epfn1 bfneg xhi) (rnd 'up epfn1 bfcopy xhi) err? err)]
535+
[(#t #f) (ival (rnd 'down epfn1 bfneg xhi) (rnd 'up epfn1 bfneg xlo) err? err)]
529536
[(#f #t) (ival xlo xhi err? err)]
530537
[(#f #f)
531538
(unless (ival-err y)

ops/gamma.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
; Purely decreasing
4545
[(bflt? (endpoint-val hi) xmin) ((comonotonic->ival fn) i)]
4646
[else
47-
(ival-union (ival (endpoint ymin #f) (rnd 'up epfn fn lo) err? err)
48-
(ival (endpoint ymin #f) (rnd 'up epfn fn hi) err? err))]))
47+
(ival-union (ival (endpoint ymin #f) (rnd 'up epfn1 fn lo) err? err)
48+
(ival (endpoint ymin #f) (rnd 'up epfn1 fn hi) err? err))]))
4949

5050
; Optimized version of `ival-lgamma-basin` for positive values, adds a cache
5151
(define lgamma-pos-xmin #f)

ops/trig.rkt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
[1 ((comonotonic->ival (curry bfcosu n)) x)]
4343
[else
4444
(ival (rnd 'down
45-
epfn
45+
epfn2
4646
bfmin2
47-
(epfn (curry bfcosu n) (ival-lo x))
48-
(epfn (curry bfcosu n) (ival-hi x)))
47+
(epfn1 (curry bfcosu n) (ival-lo x))
48+
(epfn1 (curry bfcosu n) (ival-hi x)))
4949
(endpoint 1.bf #f)
5050
(ival-err? x)
5151
(ival-err x))])]
@@ -59,18 +59,18 @@
5959
[(and (bf=? (bfsub b a) 1.bf) (bfeven? a))
6060
(ival (endpoint -1.bf #f)
6161
(rnd 'up
62-
epfn
62+
epfn2
6363
bfmax2
64-
(epfn (curry bfcosu n) (ival-lo x))
65-
(epfn (curry bfcosu n) (ival-hi x)))
64+
(epfn1 (curry bfcosu n) (ival-lo x))
65+
(epfn1 (curry bfcosu n) (ival-hi x)))
6666
(ival-err? x)
6767
(ival-err x))]
6868
[(and (bf=? (bfsub b a) 1.bf) (bfodd? a))
6969
(ival (rnd 'down
70-
epfn
70+
epfn2
7171
bfmin2
72-
(epfn (curry bfcosu n) (ival-lo x))
73-
(epfn (curry bfcosu n) (ival-hi x)))
72+
(epfn1 (curry bfcosu n) (ival-lo x))
73+
(epfn1 (curry bfcosu n) (ival-hi x)))
7474
(endpoint 1.bf #f)
7575
(ival-err? x)
7676
(ival-err x))]
@@ -86,7 +86,7 @@
8686
[-1 ((monotonic->ival bfcos) x)]
8787
[1 ((comonotonic->ival bfcos) x)]
8888
[else
89-
(ival (rnd 'down epfn bfmin2 (epfn bfcos (ival-lo x)) (epfn bfcos (ival-hi x)))
89+
(ival (rnd 'down epfn2 bfmin2 (epfn1 bfcos (ival-lo x)) (epfn1 bfcos (ival-hi x)))
9090
(endpoint 1.bf #f)
9191
(ival-err? x)
9292
(ival-err x))])]
@@ -99,11 +99,11 @@
9999
[(and (bf=? a b) (bfodd? a)) ((monotonic->ival bfcos) x)]
100100
[(and (bf=? (bfsub b a) 1.bf) (bfeven? a))
101101
(ival (endpoint -1.bf #f)
102-
(rnd 'up epfn bfmax2 (epfn bfcos (ival-lo x)) (epfn bfcos (ival-hi x)))
102+
(rnd 'up epfn2 bfmax2 (epfn1 bfcos (ival-lo x)) (epfn1 bfcos (ival-hi x)))
103103
(ival-err? x)
104104
(ival-err x))]
105105
[(and (bf=? (bfsub b a) 1.bf) (bfodd? a))
106-
(ival (rnd 'down epfn bfmin2 (epfn bfcos (ival-lo x)) (epfn bfcos (ival-hi x)))
106+
(ival (rnd 'down epfn2 bfmin2 (epfn1 bfcos (ival-lo x)) (epfn1 bfcos (ival-hi x)))
107107
(endpoint 1.bf #f)
108108
(ival-err? x)
109109
(ival-err x))]
@@ -126,18 +126,18 @@
126126
[(and (bf=? (bfsub b a) 1.bf) (bfodd? a))
127127
(ival (endpoint -1.bf #f)
128128
(rnd 'up
129-
epfn
129+
epfn2
130130
bfmax2
131-
(epfn (curry bfsinu n) (ival-lo x))
132-
(epfn (curry bfsinu n) (ival-hi x)))
131+
(epfn1 (curry bfsinu n) (ival-lo x))
132+
(epfn1 (curry bfsinu n) (ival-hi x)))
133133
(ival-err? x)
134134
(ival-err x))]
135135
[(and (bf=? (bfsub b a) 1.bf) (bfeven? a))
136136
(ival (rnd 'down
137-
epfn
137+
epfn2
138138
bfmin2
139-
(epfn (curry bfsinu n) (ival-lo x))
140-
(epfn (curry bfsinu n) (ival-hi x)))
139+
(epfn1 (curry bfsinu n) (ival-lo x))
140+
(epfn1 (curry bfsinu n) (ival-hi x)))
141141
(endpoint 1.bf #f)
142142
(ival-err? x)
143143
(ival-err x))]
@@ -159,11 +159,11 @@
159159
[(and (bf=? a b) (bfeven? a)) ((monotonic->ival bfsin) x)]
160160
[(and (bf=? (bfsub b a) 1.bf) (bfodd? a))
161161
(ival (endpoint -1.bf #f)
162-
(rnd 'up epfn bfmax2 (epfn bfsin (ival-lo x)) (epfn bfsin (ival-hi x)))
162+
(rnd 'up epfn2 bfmax2 (epfn1 bfsin (ival-lo x)) (epfn1 bfsin (ival-hi x)))
163163
(ival-err? x)
164164
(ival-err x))]
165165
[(and (bf=? (bfsub b a) 1.bf) (bfeven? a))
166-
(ival (rnd 'down epfn bfmin2 (epfn bfsin (ival-lo x)) (epfn bfsin (ival-hi x)))
166+
(ival (rnd 'down epfn2 bfmin2 (epfn1 bfsin (ival-lo x)) (epfn1 bfsin (ival-hi x)))
167167
(endpoint 1.bf #f)
168168
(ival-err? x)
169169
(ival-err x))]

0 commit comments

Comments
 (0)