Class: Floatboat::Oracle
- Inherits:
-
Object
- Object
- Floatboat::Oracle
- 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
-
#exponent ⇒ Object
readonly
Returns the value of attribute exponent.
-
#mantissa ⇒ Object
readonly
Returns the value of attribute mantissa.
-
#n_exponent ⇒ Object
readonly
Returns the value of attribute n_exponent.
-
#n_mantissa ⇒ Object
readonly
Returns the value of attribute n_mantissa.
-
#n_sign ⇒ Object
readonly
Returns the value of attribute n_sign.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
Instance Method Summary collapse
- #compute_float_value ⇒ Object
-
#initialize(f) ⇒ Oracle
constructor
A new instance of Oracle.
- #to_bit_components ⇒ Object
- #to_f ⇒ Object
- #to_numerical_components ⇒ Object
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
#exponent ⇒ Object (readonly)
Returns the value of attribute exponent.
9 10 11 |
# File 'lib/floatboat/oracle.rb', line 9 def exponent @exponent end |
#mantissa ⇒ Object (readonly)
Returns the value of attribute mantissa.
9 10 11 |
# File 'lib/floatboat/oracle.rb', line 9 def mantissa @mantissa end |
#n_exponent ⇒ Object (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_mantissa ⇒ Object (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_sign ⇒ Object (readonly)
Returns the value of attribute n_sign.
10 11 12 |
# File 'lib/floatboat/oracle.rb', line 10 def n_sign @n_sign end |
#sign ⇒ Object (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_value ⇒ Object
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_components ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/floatboat/oracle.rb', line 25 def to_bit_components [ sign, exponent, mantissa ] end |
#to_f ⇒ Object
41 42 43 |
# File 'lib/floatboat/oracle.rb', line 41 def to_f @to_f ||= compute_float_value end |
#to_numerical_components ⇒ Object
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 |