File tree Expand file tree Collapse file tree 4 files changed +19
-16
lines changed Expand file tree Collapse file tree 4 files changed +19
-16
lines changed Original file line number Diff line number Diff line change 8989 (make-representation #:name 'posit8
9090 #:bf->repr (compose double->posit8* bigfloat->flonum)
9191 #:repr->bf (compose bf-inf->nan posit8->double)
92- #:ordinal->repr (shift 7 ordinal->posit8)
93- #:repr->ordinal (unshift 7 posit8->ordinal)
92+ #:ordinal->repr ordinal->posit8
93+ #:repr->ordinal posit8->ordinal
9494 #:total-bits 8
9595 #:special-value? (curry posit8= (posit8-nar))))
9696
9797(define <posit16>
9898 (make-representation #:name 'posit16
9999 #:bf->repr (compose double->posit16* bigfloat->flonum)
100100 #:repr->bf (compose bf-inf->nan posit16->double)
101- #:ordinal->repr (shift 15 ordinal->posit16)
102- #:repr->ordinal (unshift 15 posit16->ordinal)
101+ #:ordinal->repr ordinal->posit16
102+ #:repr->ordinal posit16->ordinal
103103 #:total-bits 16
104104 #:special-value? (curry posit16= (posit16-nar))))
105105
106106(define <posit32>
107107 (make-representation #:name 'posit32
108108 #:bf->repr (compose double->posit32* bigfloat->flonum)
109109 #:repr->bf (compose bf-inf->nan posit32->double)
110- #:ordinal->repr (shift 31 ordinal->posit32)
111- #:repr->ordinal (unshift 31 posit32->ordinal)
110+ #:ordinal->repr ordinal->posit32
111+ #:repr->ordinal posit32->ordinal
112112 #:total-bits 32
113113 #:special-value? (curry posit32= (posit32-nar))))
114114
Original file line number Diff line number Diff line change 9191
9292;; Does not use make-representation to define a repr of bool
9393(define <bool>
94- (representation 'bool 'bool identity identity (curry = 0 ) (lambda (x) (if x 1 0 )) 1 (const #f )))
94+ (representation 'bool 'bool identity identity (curry = 0 ) (lambda (x) (if x 0 -1 )) 1 (const #f )))
9595
9696(define <binary32>
9797 (make-representation #:name 'binary32
9898 #:bf->repr bigfloat->float32
9999 #:repr->bf (lambda (x)
100100 (parameterize ([bf-precision 24 ])
101101 (bf x)))
102- #:ordinal->repr (shift 31 ordinal->float32)
103- #:repr->ordinal (unshift 31 float32->ordinal)
102+ #:ordinal->repr ordinal->float32
103+ #:repr->ordinal float32->ordinal
104104 #:total-bits 32
105105 #:special-value? nan?))
106106
110110 #:repr->bf (lambda (x)
111111 (parameterize ([bf-precision 53 ])
112112 (bf x)))
113- #:ordinal->repr (shift 63 ordinal->flonum)
114- #:repr->ordinal (unshift 63 flonum->ordinal)
113+ #:ordinal->repr ordinal->flonum
114+ #:repr->ordinal flonum->ordinal
115115 #:total-bits 64
116116 #:special-value? nan?))
117117
Original file line number Diff line number Diff line change 5050 (real->double-flonum (log x 2 )))
5151
5252(define (random-generate repr)
53- ((representation-ordinal->repr repr) (random-bits (representation-total-bits repr))))
53+ (define bits (sub1 (representation-total-bits repr)))
54+ ((representation-ordinal->repr repr) (random-integer (- (expt 2 bits)) (expt 2 bits))))
5455
5556(define (=/total x1 x2 repr)
5657 (define ->ordinal (representation-repr->ordinal repr))
Original file line number Diff line number Diff line change @@ -46,8 +46,9 @@ <h2>Defining representations</h2>
4646 < p > Specifically, a representation value needs to be convertible to
4747 Racket < a href ="https://docs.racket-lang.org/math/bigfloat.html "> < code > bigfloat</ code > </ a >
4848 values (which are basically MPFR floats) and also
49- to < em > ordinals</ em > , meaning integers between 0 and
50- 2< sup > < var > w</ var > </ sup > for some bit width < var > w</ var > .</ p >
49+ to < em > ordinals</ em > , meaning integers between
50+ -2< sup > < var > w</ var > −1</ sup > and
51+ 2< sup > < var > w</ var > −1</ sup > −1 for some bit width < var > w</ var > .</ p >
5152
5253 < p > Create representations with < code > make-representation</ code > :</ p >
5354
@@ -92,8 +93,9 @@ <h2>Defining representations</h2>
9293 instead.</ p >
9394
9495 < p > The < code > #:ordinal-> repr</ code > and < code > #:repr-> ordinal</ code >
95- functions represent ordinals as Racket integers between 0
96- (inclusive) and 2< sup > < var > width</ var > </ sup > (exclusive). Ordinals
96+ functions represent ordinals as Racket integers between
97+ -2< sup > < var > width</ var > −1</ sup > (inclusive) and
98+ 2< sup > < var > width</ var > −1</ sup > (exclusive). Ordinals
9799 must be in real-number order; that is, if < code > (repr-> bf x)</ code >
98100 is less than < code > (repr-> bf y)</ code > , then < code > (repr-> ordinal
99101 x)</ code > should be less than < code > (repr-> ordinal y)</ code > .</ p >
You can’t perform that action at this time.
0 commit comments