Class: Floatboat::Oracle

Inherits:
Object
  • Object
show all
Defined in:
lib/floatboat/oracle.rb

Constant Summary collapse

SIGN_BITSIZE =

From IEEE 754 double-precision floats.

1
EXPONENT_BITSIZE =
11
MANTISSA_BITSIZE =
53
EXPONENT_BIAS =
(2**(EXPONENT_BITSIZE - 1)) - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ Oracle

Returns a new instance of Oracle.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/floatboat/oracle.rb', line 12

def initialize(f)
  @value = f
  bits = ::Floatboat::Converter.to_bits(@value)

  @sign     = bits[0]
  @exponent = bits[1..11]
  @mantissa = bits[12..63]

  @n_sign     = @sign == "0" ? 1 : -1
  @n_exponent = @exponent.to_i(2) - EXPONENT_BIAS
  @n_mantissa = ::Floatboat::Converter.binary_mantissa_to_decimal(@mantissa).to_f
end

Instance Attribute Details

#exponentObject (readonly)

Returns the value of attribute exponent.



9
10
11
# File 'lib/floatboat/oracle.rb', line 9

def exponent
  @exponent
end

#mantissaObject (readonly)

Returns the value of attribute mantissa.



9
10
11
# File 'lib/floatboat/oracle.rb', line 9

def mantissa
  @mantissa
end

#n_exponentObject (readonly)

Returns the value of attribute n_exponent.



10
11
12
# File 'lib/floatboat/oracle.rb', line 10

def n_exponent
  @n_exponent
end

#n_mantissaObject (readonly)

Returns the value of attribute n_mantissa.



10
11
12
# File 'lib/floatboat/oracle.rb', line 10

def n_mantissa
  @n_mantissa
end

#n_signObject (readonly)

Returns the value of attribute n_sign.



10
11
12
# File 'lib/floatboat/oracle.rb', line 10

def n_sign
  @n_sign
end

#signObject (readonly)

Returns the value of attribute sign.



9
10
11
# File 'lib/floatboat/oracle.rb', line 9

def sign
  @sign
end

Instance Method Details

#compute_float_valueObject



45
46
47
48
49
50
51
52
# File 'lib/floatboat/oracle.rb', line 45

def compute_float_value
  case @value
  when Float::INFINITY
    @value
  else
    (@value.nan?) ? Float::NAN : 2**n_exponent * n_mantissa * n_sign
  end
end

#to_bit_componentsObject



25
26
27
28
29
30
31
# File 'lib/floatboat/oracle.rb', line 25

def to_bit_components
  [
    sign,
    exponent,
    mantissa
  ]
end

#to_fObject



41
42
43
# File 'lib/floatboat/oracle.rb', line 41

def to_f
  @to_f ||= compute_float_value
end

#to_numerical_componentsObject



33
34
35
36
37
38
39
# File 'lib/floatboat/oracle.rb', line 33

def to_numerical_components
  [
    n_sign,
    n_exponent,
    n_mantissa
  ]
end