Unbiased Rounding
Function:
(defun re (x) (declare (xargs :guard (real/rationalp x))) (- x (fl x)))
Function:
(defun rne (x n) (declare (xargs :guard (and (real/rationalp x) (integerp n)))) (let ((z (fl (* (expt 2 (1- n)) (sig x)))) (f (re (* (expt 2 (1- n)) (sig x))))) (if (< f 1/2) (rtz x n) (if (> f 1/2) (raz x n) (if (evenp z) (rtz x n) (raz x n))))))
Theorem:
(defthm rne-choice (or (= (rne x n) (rtz x n)) (= (rne x n) (raz x n))) :rule-classes nil)
Theorem:
(defthm sgn-rne (implies (and (rationalp x) (integerp n) (> n 0)) (equal (sgn (rne x n)) (sgn x))))
Theorem:
(defthm rne-positive (implies (and (< 0 x) (< 0 n) (rationalp x) (integerp n)) (< 0 (rne x n))) :rule-classes (:type-prescription :linear))
Theorem:
(defthm rne-negative (implies (and (< x 0) (< 0 n) (rationalp x) (integerp n)) (< (rne x n) 0)) :rule-classes (:type-prescription :linear))
Theorem:
(defthm rne-0 (equal (rne 0 n) 0))
Theorem:
(defthm rne-exactp-a (implies (< 0 n) (exactp (rne x n) n)))
Theorem:
(defthm rne-exactp-b (implies (and (rationalp x) (integerp n) (> n 0)) (iff (exactp x n) (= x (rne x n)))))
Theorem:
(defthm rne-exactp-c (implies (and (exactp a n) (>= a x) (rationalp x) (integerp n) (> n 0) (rationalp a)) (>= a (rne x n))) :rule-classes nil)
Theorem:
(defthm rne-exactp-d (implies (and (rationalp x) (integerp n) (> n 0) (rationalp a) (exactp a n) (<= a x)) (<= a (rne x n))) :rule-classes nil)
Theorem:
(defthm expo-rne (implies (and (rationalp x) (> n 0) (integerp n) (not (= (abs (rne x n)) (expt 2 (1+ (expo x)))))) (equal (expo (rne x n)) (expo x))))
Theorem:
(defthm rne<=raz (implies (and (rationalp x) (> x 0) (integerp n) (> n 0)) (<= (rne x n) (raz x n))) :rule-classes nil)
Theorem:
(defthm rne>=rtz (implies (and (rationalp x) (> x 0) (integerp n) (> n 0)) (>= (rne x n) (rtz x n))) :rule-classes nil)
Theorem:
(defthm rne-shift (implies (and (rationalp x) (integerp n) (integerp k)) (= (rne (* x (expt 2 k)) n) (* (rne x n) (expt 2 k)))) :rule-classes nil)
Theorem:
(defthm rne-minus (equal (rne (- x) n) (- (rne x n))))
Theorem:
(defthm rne-rtz (implies (and (< (abs (- x (rtz x n))) (abs (- (raz x n) x))) (rationalp x) (integerp n)) (equal (rne x n) (rtz x n))))
Theorem:
(defthm rne-raz (implies (and (> (abs (- x (rtz x n))) (abs (- (raz x n) x))) (rationalp x) (integerp n)) (equal (rne x n) (raz x n))))
Theorem:
(defthm rne-down (implies (and (rationalp x) (rationalp a) (>= x a) (> a 0) (not (zp n)) (exactp a n) (< x (+ a (expt 2 (- (expo a) n))))) (equal (rne x n) a)))
Theorem:
(defthm rne-up (implies (and (rationalp x) (rationalp a) (> a 0) (not (zp n)) (exactp a n) (< x (fp+ a n)) (> x (+ a (expt 2 (- (expo a) n))))) (equal (rne x n) (fp+ a n))))
Theorem:
(defthm rne-up-2 (implies (and (rationalp x) (integerp k) (not (zp n)) (not (zp m)) (< m n) (< (abs x) (expt 2 k)) (= (abs (rne x n)) (expt 2 k))) (equal (abs (rne x m)) (expt 2 k))))
Theorem:
(defthm rne-nearest (implies (and (exactp y n) (rationalp x) (rationalp y) (integerp n) (> n 0)) (>= (abs (- x y)) (abs (- x (rne x n))))) :rule-classes nil)
Theorem:
(defthm rne-diff (implies (and (integerp n) (> n 0) (rationalp x)) (<= (abs (- x (rne x n))) (expt 2 (- (expo x) n)))) :rule-classes nil)
Theorem:
(defthm rne-diff-cor (implies (and (integerp n) (> n 0) (rationalp x)) (<= (abs (- x (rne x n))) (* (abs x) (expt 2 (- n))))) :rule-classes nil)
Theorem:
(defthm rne-force (implies (and (integerp n) (> n 0) (rationalp x) (not (= x 0)) (rationalp y) (exactp y n) (< (abs (- x y)) (expt 2 (- (expo x) n)))) (= y (rne x n))) :rule-classes nil)
Theorem:
(defthm rne-monotone (implies (and (<= x y) (rationalp x) (rationalp y) (integerp n) (> n 0)) (<= (rne x n) (rne y n))) :rule-classes nil)
Function:
(defun rne-witness (x y n) (if (= (expo x) (expo y)) (/ (+ (rne x n) (rne y n)) 2) (expt 2 (expo y))))
Theorem:
(defthm rne-rne-lemma (implies (and (rationalp x) (rationalp y) (< 0 x) (< x y) (integerp n) (> n 0) (not (= (rne x n) (rne y n)))) (and (<= x (rne-witness x y n)) (<= (rne-witness x y n) y) (exactp (rne-witness x y n) (1+ n)))) :rule-classes nil)
Theorem:
(defthm rne-rne (implies (and (rationalp x) (rationalp y) (rationalp a) (integerp n) (integerp k) (> k 0) (>= n k) (< 0 a) (< a x) (< 0 y) (< y (fp+ a (1+ n))) (exactp a (1+ n))) (<= (rne y k) (rne x k))) :rule-classes nil)
Theorem:
(defthm rne-boundary (implies (and (rationalp x) (rationalp y) (rationalp a) (< 0 x) (< x a) (< a y) (integerp n) (> n 0) (exactp a (1+ n)) (not (exactp a n))) (< (rne x n) (rne y n))) :rule-classes nil)
Theorem:
(defthm rne-midpoint (implies (and (rationalp x) (integerp n) (> n 1) (exactp x (1+ n)) (not (exactp x n))) (exactp (rne x n) (1- n))) :rule-classes nil)
Function:
(defun xfp (k m x0) (+ x0 (* (expt 2 (- (1+ (expo x0)) m)) k)))
Function:
(defun err-rne (k m n x0) (- (rne (xfp k m x0) n) (xfp k m x0)))
Function:
(defun sum-err-rne (i j m n x0) (if (and (natp i) (natp j) (<= i j)) (+ (sum-err-rne i (1- j) m n x0) (err-rne j m n x0)) 0))
Theorem:
(defthm rne-unbiased (implies (and (natp m) (natp n) (< 1 n) (< n m) (rationalp x0) (> x0 0) (exactp x0 (1- n))) (equal (sum-err-rne 0 (1- (expt 2 (- (1+ m) n))) m n x0) 0)))
Function:
(defun rna (x n) (declare (xargs :guard (and (real/rationalp x) (integerp n)))) (if (< (re (* (expt 2 (1- n)) (sig x))) 1/2) (rtz x n) (raz x n)))
Theorem:
(defthm rna-choice (or (= (rna x n) (rtz x n)) (= (rna x n) (raz x n))) :rule-classes nil)
Theorem:
(defthm sgn-rna (implies (and (rationalp x) (integerp n) (> n 0)) (equal (sgn (rna x n)) (sgn x))))
Theorem:
(defthm rna-positive (implies (and (rationalp x) (> x 0) (integerp n) (> n 0)) (> (rna x n) 0)) :rule-classes :linear)
Theorem:
(defthm rna-negative (implies (and (< x 0) (rationalp x) (integerp n) (> n 0)) (< (rna x n) 0)) :rule-classes :linear)
Theorem:
(defthm rna-0 (equal (rna 0 n) 0))
Theorem:
(defthm rna-exactp-a (implies (> n 0) (exactp (rna x n) n)))
Theorem:
(defthm rna-exactp-b (implies (and (rationalp x) (integerp n) (> n 0)) (iff (exactp x n) (= x (rna x n)))))
Theorem:
(defthm rna-exactp-c (implies (and (rationalp x) (integerp n) (> n 0) (rationalp a) (exactp a n) (>= a x)) (>= a (rna x n))) :rule-classes nil)
Theorem:
(defthm rna-exactp-d (implies (and (rationalp x) (integerp n) (> n 0) (rationalp a) (exactp a n) (<= a x)) (<= a (rna x n))) :rule-classes nil)
Theorem:
(defthm expo-rna (implies (and (rationalp x) (natp n) (not (= (abs (rna x n)) (expt 2 (1+ (expo x)))))) (equal (expo (rna x n)) (expo x))))
Theorem:
(defthm rna<=raz (implies (and (rationalp x) (> x 0) (integerp n) (> n 0)) (<= (rna x n) (raz x n))) :rule-classes nil)
Theorem:
(defthm rna>=rtz (implies (and (rationalp x) (> x 0) (integerp n) (> n 0)) (>= (rna x n) (rtz x n))) :rule-classes nil)
Theorem:
(defthm rna-shift (implies (and (rationalp x) (integerp n) (integerp k)) (= (rna (* x (expt 2 k)) n) (* (rna x n) (expt 2 k)))) :rule-classes nil)
Theorem:
(defthm rna-minus (equal (rna (- x) n) (- (rna x n))))
Theorem:
(defthm rna-rtz (implies (and (rationalp x) (integerp n) (< (abs (- x (rtz x n))) (abs (- (raz x n) x)))) (equal (rna x n) (rtz x n))))
Theorem:
(defthm rna-raz (implies (and (rationalp x) (integerp n) (> (abs (- x (rtz x n))) (abs (- (raz x n) x)))) (equal (rna x n) (raz x n))))
Theorem:
(defthm rna-down (implies (and (rationalp x) (rationalp a) (>= x a) (> a 0) (not (zp n)) (exactp a n) (< x (+ a (expt 2 (- (expo a) n))))) (equal (rna x n) a)))
Theorem:
(defthm rna-up (implies (and (rationalp x) (rationalp a) (> a 0) (not (zp n)) (exactp a n) (< x (fp+ a n)) (> x (+ a (expt 2 (- (expo a) n))))) (equal (rna x n) (fp+ a n))))
Theorem:
(defthm rna-up-2 (implies (and (rationalp x) (integerp k) (not (zp n)) (not (zp m)) (< m n) (< (abs x) (expt 2 k)) (= (abs (rna x n)) (expt 2 k))) (equal (abs (rna x m)) (expt 2 k))))
Theorem:
(defthm rna-nearest (implies (and (exactp y n) (rationalp x) (rationalp y) (integerp n) (> n 0)) (>= (abs (- x y)) (abs (- x (rna x n))))) :rule-classes nil)
Theorem:
(defthm rna-force (implies (and (integerp n) (> n 0) (rationalp x) (not (= x 0)) (rationalp y) (exactp y n) (< (abs (- x y)) (expt 2 (- (expo x) n)))) (= y (rna x n))) :rule-classes nil)
Theorem:
(defthm rna-diff (implies (and (integerp n) (> n 0) (rationalp x)) (<= (abs (- x (rna x n))) (expt 2 (- (expo x) n)))) :rule-classes nil)
Theorem:
(defthm rna-monotone (implies (and (<= x y) (rationalp x) (rationalp y) (natp n)) (<= (rna x n) (rna y n))) :rule-classes nil)
Function:
(defun rna-witness (x y n) (if (= (expo x) (expo y)) (/ (+ (rna x n) (rna y n)) 2) (expt 2 (expo y))))
Theorem:
(defthm rna-rna-lemma (implies (and (rationalp x) (rationalp y) (< 0 x) (< x y) (integerp n) (> n 0) (not (= (rna x n) (rna y n)))) (and (<= x (rna-witness x y n)) (<= (rna-witness x y n) y) (exactp (rna-witness x y n) (1+ n)))) :rule-classes nil)
Theorem:
(defthm rna-rna (implies (and (rationalp x) (rationalp y) (rationalp a) (integerp n) (integerp k) (> k 0) (>= n k) (< 0 a) (< a x) (< 0 y) (< y (fp+ a (1+ n))) (exactp a (1+ n))) (<= (rna y k) (rna x k))) :rule-classes nil)
Theorem:
(defthm rna-midpoint (implies (and (rationalp x) (integerp n) (exactp x (1+ n)) (not (exactp x n))) (equal (rna x n) (raz x n))))
Theorem:
(defthm rne-pow-2 (implies (and (rationalp x) (> x 0) (not (zp n)) (>= (+ x (expt 2 (- (expo x) n))) (expt 2 (1+ (expo x))))) (= (rne x n) (expt 2 (1+ (expo x))))) :rule-classes nil)
Theorem:
(defthm rtz-pow-2 (implies (and (rationalp x) (> x 0) (not (zp n)) (>= (+ x (expt 2 (- (expo x) n))) (expt 2 (1+ (expo x))))) (= (rtz (+ x (expt 2 (- (expo x) n))) n) (expt 2 (1+ (expo x))))) :rule-classes nil)
Theorem:
(defthm rna-pow-2 (implies (and (rationalp x) (> x 0) (not (zp n)) (>= (+ x (expt 2 (- (expo x) n))) (expt 2 (1+ (expo x))))) (= (rna x n) (expt 2 (1+ (expo x))))) :rule-classes nil)
Theorem:
(defthm plus-rne (implies (and (exactp x (1- (+ k (- (expo x) (expo y))))) (rationalp x) (>= x 0) (rationalp y) (>= y 0) (integerp k)) (= (+ x (rne y k)) (rne (+ x y) (+ k (- (expo (+ x y)) (expo y)))))) :rule-classes nil)
Theorem:
(defthm plus-rna (implies (and (exactp x (+ k (- (expo x) (expo y)))) (rationalp x) (>= x 0) (rationalp y) (>= y 0) (integerp k)) (= (+ x (rna y k)) (rna (+ x y) (+ k (- (expo (+ x y)) (expo y)))))) :rule-classes nil)
Theorem:
(defthm rne-impl (implies (and (rationalp x) (> x 0) (not (zp n))) (equal (rne x n) (if (and (> n 1) (exactp x (1+ n)) (not (exactp x n))) (rtz (+ x (expt 2 (- (expo x) n))) (1- n)) (rtz (+ x (expt 2 (- (expo x) n))) n)))))
Theorem:
(defthm rna-impl (implies (and (rationalp x) (> x 0) (not (zp n))) (equal (rna x n) (rtz (+ x (expt 2 (- (expo x) n))) n))))
Theorem:
(defthm rna-imp-cor (implies (and (rationalp x) (integerp m) (integerp n) (> n m) (> m 0)) (equal (rna (rtz x n) m) (rna x m))))