Class: Hexadecimal
- Inherits:
-
Object
- Object
- Hexadecimal
- Defined in:
- lib/hex_values/hexadecimal.rb
Constant Summary collapse
- MAX_DECIMALS =
14
Class Method Summary collapse
-
.from_string(hex_representation) ⇒ Object
Public: Transform a given string into a Hexadecimal object.
Instance Method Summary collapse
-
#*(other_obj) ⇒ Object
Public: Multiply two values.
-
#**(other_obj) ⇒ Object
Public: Pow two values.
-
#+(other_obj) ⇒ Object
Public: Sum two values.
-
#-(other_obj) ⇒ Object
Public: Substracts two values.
-
#/(other_obj) ⇒ Object
Public: Divide two values.
-
#initialize(float_representation = 0.0, precision = MAX_DECIMALS) ⇒ Hexadecimal
constructor
A new instance of Hexadecimal.
-
#to_float ⇒ Object
Public: return the float value of the Hexadecimal.
-
#to_s ⇒ Object
Public: return a String representation.
Constructor Details
#initialize(float_representation = 0.0, precision = MAX_DECIMALS) ⇒ Hexadecimal
Returns a new instance of Hexadecimal.
4 5 6 7 8 |
# File 'lib/hex_values/hexadecimal.rb', line 4 def initialize(float_representation=0.0, precision=MAX_DECIMALS) precision = MAX_DECIMALS if precision.nil? || precision < 0 @max_decimals=precision @float_representation=float_representation end |
Class Method Details
.from_string(hex_representation) ⇒ Object
Public: Transform a given string into a Hexadecimal object.
hex_representation - A String representing a Hexadecimal value
Examples:
Hexadecimal.from_string("EA.4")
Returns an Hexadecimal object. Or throws an exception if the string can’t be parsed.
93 94 95 96 |
# File 'lib/hex_values/hexadecimal.rb', line 93 def self.from_string(hex_representation) return Hexadecimal.new if hex_representation.nil? Hexadecimal.new(hex_representation.to_float) end |
Instance Method Details
#*(other_obj) ⇒ Object
Public: Multiply two values.
other_obj - An Hexadecimal object, a Fixnum or a Float
Returns an Hexadecimal object, the multiplication of the operands.
63 64 65 |
# File 'lib/hex_values/hexadecimal.rb', line 63 def *(other_obj) return operation(self, other_obj, "*".to_sym) end |
#**(other_obj) ⇒ Object
Public: Pow two values.
other_obj - An Hexadecimal object, a Fixnum or a Float
Returns an Hexadecimal object, the power of the operands.
81 82 83 |
# File 'lib/hex_values/hexadecimal.rb', line 81 def **(other_obj) return operation(self, other_obj, "**".to_sym) end |
#+(other_obj) ⇒ Object
Public: Sum two values.
other_obj - An Hexadecimal object, a Fixnum or a Float
Returns an Hexadecimal object, sum of the two objects.
44 45 46 |
# File 'lib/hex_values/hexadecimal.rb', line 44 def +(other_obj) return operation(self, other_obj, "+".to_sym) end |
#-(other_obj) ⇒ Object
Public: Substracts two values.
other_obj - An Hexadecimal object, a Fixnum or a Float
Returns an Hexadecimal object, the substaction of the second object over the first.
53 54 55 |
# File 'lib/hex_values/hexadecimal.rb', line 53 def -(other_obj) self + (other_obj * -1) end |
#/(other_obj) ⇒ Object
Public: Divide two values.
other_obj - An Hexadecimal object, a Fixnum or a Float
Returns an Hexadecimal object, the division of the operands.
72 73 74 |
# File 'lib/hex_values/hexadecimal.rb', line 72 def /(other_obj) return operation(self, other_obj, "/".to_sym) end |
#to_float ⇒ Object
Public: return the float value of the Hexadecimal.
Returns the Float value of the Hexadecimal.
13 14 15 |
# File 'lib/hex_values/hexadecimal.rb', line 13 def to_float @float_representation end |
#to_s ⇒ Object
Public: return a String representation.
Returns a String representation of the Hexadecimal object.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hex_values/hexadecimal.rb', line 20 def to_s return "Infinity" unless @float_representation.to_f.infinite?.nil? num,dec = get_hex_value(@float_representation) first = num second = "" iterations = 0 while dec > 0 && iterations < @max_decimals num, dec = get_hex_value(dec*16) second << num iterations += 1 end if "".eql? second first else normalize_negativity "#{first}.#{second}", @float_representation end end |