Skip to content

Commit 299389e

Browse files
committed
stuff
1 parent 675a883 commit 299389e

File tree

3 files changed

+299
-47
lines changed

3 files changed

+299
-47
lines changed

src/core/explain.rkt

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,14 @@
161161
[(list (or '+.f64 '+.f32) x-ex y-ex)
162162
#:when (or (list? x-ex) (list? y-ex))
163163

164+
(define x (exacts-ref x-ex))
165+
(define y (exacts-ref y-ex))
166+
164167
(define xlog (lf-normalize (logfls-ref x-ex)))
165168
(match-define (logfl xfl xs xe) xlog)
166169
(define ylog (lf-normalize (logfls-ref y-ex)))
167170
(match-define (logfl yfl ys ye) ylog)
168171

169-
(define cond-x (abs (/ xfl (+ xfl yfl))))
170-
(define cond-y (abs (/ yfl (+ xfl yfl))))
171172
(define cond-x.l (logabs (log/ xlog (log+ xlog ylog))))
172173
(define cond-y.l (logabs (log/ ylog (log+ xlog ylog))))
173174

@@ -188,13 +189,13 @@
188189
; nan rescue:
189190
; R(+-inf) + R(-+inf) = nan, but should actually
190191
; be inf
191-
[(and (overflow? xlog) (overflow? ylog) (not (same-sign?* xfl yfl)))
192+
[(and (overflowed? xlog) (overflowed? ylog) (not (same-sign?* xfl yfl)))
192193
(mark-erroneous! subexpr 'nan-rescue)]
193194

194195
; inf rescue:
195196
; R(inf) + y = non inf value (inf rescue)
196-
[(and (overflow? xlog) (<= (abs se) MAX-EXP)) (mark-erroneous! subexpr 'oflow-left)]
197-
[(and (overflow? ylog) (<= (abs se) MAX-EXP)) (mark-erroneous! subexpr 'oflow-right)]
197+
[(and (overflowed? xlog) (not (overflowed? slog))) (mark-erroneous! subexpr 'oflow-left)]
198+
[(and (overflowed? ylog) (not (overflowed? slog))) (mark-erroneous! subexpr 'oflow-right)]
198199

199200
; High condition number:
200201
; CN(+, x, y) = |x / x + y|
@@ -231,14 +232,14 @@
231232

232233
; nan rescue:
233234
; inf - inf = nan but should actually get an inf
234-
[(and (overflow? xlog) (overflow? ylog) (same-sign?* xfl yfl))
235+
[(and (overflowed? xlog) (overflowed? ylog) (same-sign?* xfl yfl))
235236
(mark-erroneous! subexpr 'nan-rescue)]
236237

237238
; inf rescue
238239
; If x or y overflow and the other arg rescues
239240
; it
240-
[(and (overflow? xlog) (<= (abs se) MAX-EXP)) (mark-erroneous! subexpr 'oflow-left)]
241-
[(and (overflow? ylog) (<= (abs se) MAX-EXP)) (mark-erroneous! subexpr 'oflow-right)]
241+
[(and (overflowed? xlog) (not (overflowed? slog))) (mark-erroneous! subexpr 'oflow-left)]
242+
[(and (overflowed? ylog) (not (overflowed? slog))) (mark-erroneous! subexpr 'oflow-right)]
242243

243244
; High condition number:
244245
; CN(+, x, y) = |x / x - y|
@@ -258,7 +259,7 @@
258259
(define cot-x.l (logabs (log/ 1.l (logtan xlog))))
259260
(define cond-no.l (log* (logabs xlog) cot-x.l))
260261
(cond
261-
[(overflow? xlog) (mark-erroneous! subexpr 'oflow-rescue)]
262+
[(and (overflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'oflow-rescue)]
262263

263264
[(and (log> cond-no.l 100.l) (log> (logabs xlog) 100.l)) (mark-erroneous! subexpr 'sensitivity)]
264265

@@ -282,7 +283,7 @@
282283

283284
(cond
284285
;[(and (bfinfinite? x) (not (bfnan? subexpr-val))) (mark-erroneous! subexpr 'oflow-rescue)]
285-
[(overflow? xlog) (mark-erroneous! subexpr 'oflow-rescue)]
286+
[(and (overflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'oflow-rescue)]
286287

287288
; [(and (bf> cond-no cond-thres) (bf> (bfabs x) cond-thres))
288289
; (mark-erroneous! subexpr 'sensitivity)]
@@ -317,7 +318,7 @@
317318
(define cond-no.l (log* (logabs xlog) cond-hlf.l))
318319

319320
(cond
320-
[(overflow? xlog) (mark-erroneous! subexpr 'oflow-rescue)]
321+
[(and (overflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'oflow-rescue)]
321322
[(and (log> cond-no.l 100.l) (log> (logabs xlog) 100.l)) (mark-erroneous! subexpr 'sensitivity)]
322323
[(and (log> cond-no.l 100.l) (log> cond-hlf.l 100.l)) (mark-erroneous! subexpr 'cancellation)]
323324

@@ -328,16 +329,18 @@
328329
[(list (or 'sqrt.f64 'sqrt.f32) x-ex)
329330
#:when (list? x-ex)
330331
(define xlog (logfls-ref x-ex))
332+
(define x (bigfloat->flonum (exacts-ref x-ex)))
331333
(match-define (logfl xfl xs xe) xlog)
332334

333335
(cond
334-
[(and (underflow? xlog) (< (/ (abs xe) 2.0) MAX-EXP))
335-
(mark-erroneous! subexpr 'uflow-rescue)]
336336
;; Underflow rescue:
337+
[(and (underflowed? xlog) (not (underflowed? slog)))
338+
(mark-erroneous! subexpr 'uflow-rescue)]
337339

338340
;; Overflow rescue:
339-
[(and (overflow? xlog) (< (/ (abs xe) 2.0) MAX-EXP))
340-
(mark-erroneous! subexpr 'oflow-rescue)])]
341+
[(and (overflowed? xlog) (not (overflowed? slog)))
342+
(mark-erroneous! subexpr 'oflow-rescue)]
343+
[else #f])]
341344

342345
[(list (or 'cbrt.f64 'cbrt.f32) x-ex)
343346
#:when (list? x-ex)
@@ -348,12 +351,12 @@
348351
(cond
349352
;; Underflow rescue:
350353
;; [(and (bfzero? x) (not (bf= subexpr-val x))) (mark-erroneous! subexpr 'uflow-rescue)]
351-
[(and (underflow? xlog) (< (/ (abs xe) 2.0) MAX-EXP))
354+
[(and (underflowed? xlog) (not (underflowed? slog)))
352355
(mark-erroneous! subexpr 'uflow-rescue)]
353356

354357
;; Overflow rescue:
355358
;;[(and (bfinfinite? x) (not (bf= subexpr-val x))) (mark-erroneous! subexpr 'oflow-rescue)])]
356-
[(and (overflow? xlog) (< (/ (abs xe) 2.0) MAX-EXP))
359+
[(and (overflowed? xlog) (not (overflowed? slog)))
357360
(mark-erroneous! subexpr 'oflow-rescue)])]
358361

359362
[(list (or '/.f64 '/.f32) x-ex y-ex)
@@ -368,25 +371,26 @@
368371
(cond
369372
;; if the numerator underflows and the denominator:
370373
;; - underflows, nan could be rescued
371-
[(and (underflow? xlog) (underflow? ylog)) (mark-erroneous! subexpr 'u/u)]
374+
[(and (underflowed? xlog) (underflowed? ylog) (representable? slog))
375+
(mark-erroneous! subexpr 'u/u)]
372376
;; - is small enough, 0 underflow could be rescued
373-
[(and (underflow? xlog) (<= (abs (- xe ye)) MAX-EXP)) (mark-erroneous! subexpr 'u/n)]
377+
[(and (underflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'u/n)]
374378
;; - overflows, no rescue is possible
375379

376380
;; if the numerator overflows and the denominator:
377381
;; - overflows, nan could be rescued
378-
[(and (overflow? xlog) (overflow? ylog)) (mark-erroneous! subexpr 'o/o)]
382+
[(and (overflowed? xlog) (overflowed? ylog) (representable? slog)) (mark-erroneous! subexpr 'o/o)]
379383
;; - is large enough, inf overflow can be rescued
380-
[(and (overflow? xlog) (<= (abs (- xe ye)) MAX-EXP)) (mark-erroneous! subexpr 'o/n)]
384+
[(and (overflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'o/n)]
381385
;; - underflow, no rescue is possible
382386

383387
;; if the numerator is normal and the denominator:
384388
;; - overflows, then a rescue is possible
385389
; [(and (bfinfinite? y) (not (bfzero? subexpr-val))) (mark-erroneous! subexpr 'n/o)]
386-
[(and (overflow? ylog) (<= (abs (- xe ye)) MAX-EXP)) (mark-erroneous! subexpr 'n/o)]
390+
[(and (overflowed? ylog) (representable? slog)) (mark-erroneous! subexpr 'n/o)]
387391
;; - underflows, then a rescue is possible
388392
;; [(and (bfzero? y) (not (bfinfinite? subexpr-val))) (mark-erroneous! subexpr 'n/u)]
389-
[(and (underflow? ylog) (<= (abs (- xe ye)) MAX-EXP)) (mark-erroneous! subexpr 'n/u)]
393+
[(and (underflowed? ylog) (representable? slog)) (mark-erroneous! subexpr 'n/u)]
390394
;; - is normal, then no rescue is possible
391395
[else #f])]
392396

@@ -402,14 +406,14 @@
402406
(cond
403407
;; if one operand underflows and the other overflows, then nan must
404408
;; be rescued.
405-
[(and (overflow? xlog) (underflow? ylog)) (mark-erroneous! subexpr 'o*u)]
406-
[(and (underflow? xlog) (overflow? ylog)) (mark-erroneous! subexpr 'o*u)]
409+
[(and (overflowed? xlog) (underflowed? ylog) (representable? slog)) (mark-erroneous! subexpr 'o*u)]
410+
[(and (underflowed? xlog) (overflowed? ylog) (representable? slog)) (mark-erroneous! subexpr 'o*u)]
407411

408412
;; If one operand is normal and the other overflows then, inf rescue
409413
;; could occur
410-
[(and (or (overflow? xlog) (overflow? ylog)) (<= (abs (+ xe ye)) MAX-EXP))
414+
[(and (or (overflowed? xlog) (overflowed? ylog)) (representable? slog))
411415
(mark-erroneous! subexpr 'n*o)]
412-
[(and (or (underflow? xlog) (underflow? ylog)) (<= (abs (+ xe ye)) MAX-EXP))
416+
[(and (or (underflowed? xlog) (underflowed? ylog)) (representable? slog))
413417
(mark-erroneous! subexpr 'n*u)]
414418
;; If both normal then no error
415419
[else #f])]
@@ -419,8 +423,8 @@
419423
(define x (exacts-ref x-ex))
420424
(define xlog (logfls-ref x-ex))
421425
(match-define (logfl xfl xs xe) xlog)
422-
(define cond-num.l (logabs (log/ 1.l xlog)))
423-
(define cond-num (abs (/ 1.0 xfl)))
426+
(define cond-num.l (logabs (log/ 1.l (logln xlog))))
427+
(define cond-num (abs (/ 1.0 (log xfl))))
424428

425429
(cond
426430
; Condition number hallucination:
@@ -429,10 +433,10 @@
429433
; [(and (bf= x 1.bf) (bfzero? subexpr-val)) #f]
430434

431435
; overflow rescue:
432-
[(overflow? xlog) (mark-erroneous! subexpr 'oflow-rescue)]
436+
[(and (overflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'oflow-rescue)]
433437

434438
; underflow rescue:
435-
[(underflow? xlog) (mark-erroneous! subexpr 'uflow-rescue)]
439+
[(and (underflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'uflow-rescue)]
436440

437441
; High Condition Number:
438442
; CN(log, x) = |1 / log(x)|
@@ -455,7 +459,7 @@
455459
; Condition Number Hallucination:
456460
; When x is large enough (negative) that exp(x)
457461
; underflows, condition number is also high
458-
[(and (> (abs (* xfl (fllog2 euler.0))) MAX-EXP)) #f]
462+
[(not (representable? slog)) #f]
459463

460464
; High Condition Number:
461465
; CN(exp, x) = |x|
@@ -502,23 +506,23 @@
502506
;; Hallucination:
503507
;; if x is large enough that x^y overflows, the condition number also
504508
;; is very large, but the answer correctly overflows
505-
[(and (> yfl 1.0) (overflow? slog)) #f]
509+
[(and (> yfl 1.0) (overflowed? slog)) #f]
506510

507511
;[(and (bf< y -1.bf) (zero? x^y) (bfzero? subexpr-val)) #f]
508-
[(and (< yfl -1.0) (overflow? slog)) #f]
512+
[(and (< yfl -1.0) (overflowed? slog)) #f]
509513

510514
;; if x is small enough and y is large enough that x^y underflows,
511515
;; the condition number also gets very large, but the answer
512516
;; correctly underflows
513517
;[(and (bf> y 1.bf) (zero? x^y) (bfzero? subexpr-val)) #f]
514-
[(and (> yfl 1.0) (underflow? slog)) #f]
518+
[(and (> yfl 1.0) (underflowed? slog)) #f]
515519

516520
;[(and (bf< y -1.bf) (infinite? x^y) (bfinfinite? subexpr-val)) #f]
517-
[(and (< yfl -1.0) (underflow? slog)) #f]
521+
[(and (< yfl -1.0) (underflowed? slog)) #f]
518522

519-
[(and (underflow? xlog) (<= se MAX-EXP)) (mark-erroneous! subexpr 'uflow-rescue)]
523+
[(and (underflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'uflow-rescue)]
520524

521-
[(and (overflow? xlog) (<= se MAX-EXP)) (mark-erroneous! subexpr 'oflow-rescue)]
525+
[(and (overflowed? xlog) (representable? slog)) (mark-erroneous! subexpr 'oflow-rescue)]
522526

523527
[(and (or (log> cond-x.l 100.l) (log> cond-y.l 100.l)) (not (constant? y-ex)))
524528
(mark-erroneous! subexpr 'sensitivity)]
@@ -679,8 +683,10 @@
679683
(for/list ([(pt _) (in-pcontext pcontext)])
680684
(define error-actual? (> (hash-ref actual-error pt) 16))
681685
(define error-predicted? (hash-ref predicted-error pt false))
686+
#;(when (and error-predicted? error-actual?)
687+
(printf "true-pos ~a\n" pt))
682688
#;(when (and error-predicted? (not error-actual?))
683-
(eprintf "~a\n" pt))
689+
(printf "false-pos ~a\n" pt))
684690
(cons error-actual? error-predicted?)))
685691

686692
(define groups (group-by identity outcomes))

src/core/logspace.rkt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
(provide (all-defined-out))
1010

1111
(define MAX-EXP 1023)
12-
(define (representable? e)
13-
(and (not (nan? e)) (< (abs e) (MAX-EXP . + . 1))))
12+
1413
(define (set-sign s num)
1514
(if s num (- num)))
1615

@@ -35,17 +34,38 @@
3534

3635
(define lf flonum->logfl)
3736

37+
(define max.lf (lf +max.0))
38+
(define min.lf (lf +min.0))
39+
3840
(define (lf-normalize lf)
3941
(match-define (logfl x s e) lf)
4042
(if (or (nan? x) (zero? x) (infinite? x)) (logfl (set-sign s (flexp2 e)) s e) lf))
4143

42-
(define (overflow? xl)
43-
(match-define (logfl x s e) xl)
44-
(and (or (infinite? x) (nan? x)) (> (abs e) MAX-EXP)))
44+
; (define (overflow? xl [threshold MAX-EXP])
45+
; (match-define (logfl x s e) xl)
46+
; (and (or (infinite? x) (nan? x)) (< (abs e) threshold)))
4547

46-
(define (underflow? xl)
47-
(match-define (logfl x s e) xl)
48-
(and (zero? x) (> (abs e) MAX-EXP)))
48+
; (define (underflow? xl [threshold MAX-EXP])
49+
; (match-define (logfl x s e) xl)
50+
; (and (zero? x) (< (abs e) threshold)))
51+
52+
; (define (overflow-cnd? xl cnd)
53+
; (match-define (logfl x s e) xl)
54+
; (and (or (infinite? x) (nan? x)) cnd))
55+
56+
; (define (underflow-cnd? xl cnd)
57+
; (match-define (logfl x s e) xl)
58+
; (and (zero? x) cnd))
59+
60+
(define (overflowed? X)
61+
(log> (logabs X) max.lf))
62+
63+
(define (underflowed? X)
64+
(log< (logabs X) min.lf))
65+
66+
(define (representable? X)
67+
(and (not (overflowed? X))
68+
(not (underflowed? X))))
4969

5070
(define (exact-zero? xl)
5171
(match-define (logfl x s e) xl)

0 commit comments

Comments
 (0)