Changeset 36953 in project


Ignore:
Timestamp:
12/02/18 22:19:59 (8 days ago)
Author:
kon
Message:

fp~= was good enough 4 me but better now

Location:
release/5/fp-utils/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • release/5/fp-utils/trunk/fp-inlines.scm

    r36641 r36953  
    1818  fplog2 fplog10
    1919  fpdegree->radian fpradian->degree
    20   fp~= fp~<= fp~>=
    2120  fpprecision-factor)
    2221
     
    127126;;
    128127
    129 (: fp~= (float float #!optional float --> boolean))
    130 ;
    131 (define (fp~= x y #!optional (eps flonum-epsilon)) (fp<= (fpabs (fp- x y)) eps))
    132 
    133 (: fp~<= (float float #!optional float --> boolean))
    134 ;
    135 (define (fp~<= x y #!optional (eps flonum-epsilon)) (or (fp< x y) (fp~= x y eps)))
    136 
    137 (: fp~>= (float float #!optional float --> boolean))
    138 ;
    139 (define (fp~>= x y #!optional (eps flonum-epsilon)) (or (fp> x y) (fp~= x y eps)))
    140 
    141 ;;
    142 
    143128(: fpprecision-factor ((or float fixnum) #!optional float --> float))
    144129;
  • release/5/fp-utils/trunk/fp-utils.scm

    r36639 r36953  
    2222  fpdistance fpdistance*
    2323  ;
    24   fpmax-and-min)
     24  fpmax-and-min
     25  ;
     26  fp~= fp~<= fp~>=)
    2527
    2628(import scheme
     
    134136      (fp/ 1.0 (exact->inexact (fxrandom lim low))) ) ) )
    135137
     138;;
     139
     140(: fp~= (float float #!optional float --> boolean))
     141;
     142(define (fp~= x y #!optional (eps flonum-epsilon))
     143  #; ;enough 4 how i used it but ...
     144  (fp<= (fpabs (fp- x y)) eps)
     145  ;better
     146  (or
     147    (fp= x y)
     148    (let (
     149      (abs-x (fpabs x))
     150      (abs-y (fpabs y))
     151      (abs-d (fpabs (fp- x y))) )
     152      ;very small relative error?
     153      ;(minimum/maximum-flonum is smallest/largest positive normal flonum)
     154      (if (or (fpzero? abs-x) (fpzero? abs-y) (fp< abs-d minimum-flonum))
     155        (fp< abs-d (fp* eps minimum-flonum))
     156        (fp< (fp/ abs-d (fpmin (fp+ abs-x abs-y) maximum-flonum)) eps) ) ) ) )
     157
     158(: fp~<= (float float #!optional float --> boolean))
     159;
     160(define (fp~<= x y #!optional (eps flonum-epsilon)) (or (fp< x y) (fp~= x y eps)))
     161
     162(: fp~>= (float float #!optional float --> boolean))
     163;
     164(define (fp~>= x y #!optional (eps flonum-epsilon)) (or (fp> x y) (fp~= x y eps)))
     165
    136166) ;fp-utils
  • release/5/fp-utils/trunk/tests/fp-utils-test.scm

    r36641 r36953  
    1010(import (chicken base) (chicken flonum))
    1111
    12 (import fp-inlines)
     12(import fp-inlines fp-utils)
    1313
    1414(test-group "Fp Inlines"
     
    4444;;
    4545
    46 (import fp-utils)
     46(define-constant 5eps (fp/ 9.0 1e06))
    4747
    48 (define-constant 5eps (fp/ 9.0 1e06))
     48(test-group "Fp Approximate ="
     49        (test-assert (fp~= 0.123456 0.123457 5eps))
     50        (test-assert (fp~<= 0.123456 0.123457 5eps))
     51        (test-assert (fp~>= 0.123456 0.123457 5eps))
     52        (test-assert (fp~<= 0.123456 0.12346 5eps))
     53        (test-assert (fp~>= 0.123456 0.12344 5eps))
     54)
     55
     56;;
     57
    4958(define-constant 4eps (fp/ 9.0 1e05))
    5059
     
    5968        (test-assert (flonum? (fprandom)))
    6069        (test-assert (flonum? (fprandom 2456)))
    61 
    62         (test-assert (fp~= 0.123456 0.123457 5eps))
    63         (test-assert (fp~<= 0.123456 0.123457 5eps))
    64         (test-assert (fp~>= 0.123456 0.123457 5eps))
    65         (test-assert (fp~<= 0.123456 0.12346 5eps))
    66         (test-assert (fp~>= 0.123456 0.12344 5eps))
    6770
    6871  (parameterize ((current-test-epsilon 4eps))
Note: See TracChangeset for help on using the changeset viewer.