-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.rb
More file actions
42 lines (36 loc) · 1014 Bytes
/
float.rb
File metadata and controls
42 lines (36 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Float
MANT_BITS = MANT_DIG - 1
MANT_MASK = (1 << MANT_BITS) - 1
EXP_BITS = MAX_EXP.to_s(2).size
EXP_MASK = (MAX_EXP | (MAX_EXP - 1)) << MANT_BITS
EXP_OFFSET = MAX_EXP - 1
def float_bits
[self].pack('d').unpack('q')[0]
end
def float_decode
x = float_bits
return x < 0,
x & MANT_MASK,
((x & EXP_MASK) >> MANT_BITS) - EXP_OFFSET
end
def float_inspect
s, m, e = float_decode
m = if m.zero?
'1'
else
"1.#{m.to_s(2).rjust(MANT_BITS, '0').sub(/0*$/, '')}"
end
"#{'-' if s}#{m}e#{e}"
end
class << self
def encode(neg, mant, exp)
if exp > MAX_EXP
INFINITY
elsif exp < MIN_EXP
-INFINITY
else
[(neg ? 1 << EXP_BITS + MANT_BITS : 0) | ((exp + EXP_OFFSET) << MANT_BITS) | (mant & MANT_MASK)].pack('q').unpack('d')[0]
end
end
end
end