hex_values 0.0.3 → 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +28 -0
- data/lib/hex_values.rb +82 -28
- data/lib/hex_values/hexadecimal.rb +130 -0
- data/lib/hex_values/version.rb +1 -1
- data/spec/hex_values/hexadecimal_spec.rb +462 -0
- data/spec/hex_values_spec.rb +27 -19
- metadata +5 -2
data/README.md
CHANGED
@@ -42,6 +42,34 @@ Or transform any hexadecimal to the corresponding float:
|
|
42
42
|
# => 234.25
|
43
43
|
```
|
44
44
|
|
45
|
+
## Hexadecimal
|
46
|
+
|
47
|
+
To be more precise, when you transoform a Float (or a Fixnum) to an hexadecimal, you get a Hexadecimal object. This object is a representation of the Hexadecimal value, and you can operate on this object:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
hexa = 234.25.to_hex
|
51
|
+
result = hexa + 234.25
|
52
|
+
result.to_s
|
53
|
+
# => "1D4.8"
|
54
|
+
```
|
55
|
+
|
56
|
+
also you can operate over a Float (or a Fixnum) using an Hexadecimal value.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
hexa = 234.25.to_hex
|
60
|
+
result = 234.25 + hexa # I know, this is obvious...but is better to explain that works.
|
61
|
+
result.to_s
|
62
|
+
# => "1D4.8"
|
63
|
+
```
|
64
|
+
|
65
|
+
So far, the operations availables are:
|
66
|
+
|
67
|
+
* plus (+)
|
68
|
+
* minus (-)
|
69
|
+
* multiply (*)
|
70
|
+
* division (/)
|
71
|
+
* Power (**)
|
72
|
+
|
45
73
|
### From float to Hex
|
46
74
|
|
47
75
|
The "to_hex" method allows a precision parameter to set the maximum number of decimals to calculate (default 14).
|
data/lib/hex_values.rb
CHANGED
@@ -1,44 +1,30 @@
|
|
1
1
|
require "hex_values/version"
|
2
|
+
require "hex_values/hexadecimal"
|
2
3
|
|
3
4
|
module HexValuesFromFloat
|
4
|
-
|
5
|
-
|
6
|
-
def to_hex(precision = MAX_DECIMALS)
|
7
|
-
precision = MAX_DECIMALS if precision < 0
|
8
|
-
|
9
|
-
num,dec = get_hex_value(self)
|
10
|
-
first = num
|
11
|
-
second = ""
|
12
|
-
iterations = 0
|
13
|
-
while dec > 0 && iterations < precision
|
14
|
-
num, dec = get_hex_value(dec*16)
|
15
|
-
second << num
|
16
|
-
iterations += 1
|
17
|
-
end
|
18
|
-
|
19
|
-
if "".eql? second
|
20
|
-
first
|
21
|
-
else
|
22
|
-
"#{first}.#{second}"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def get_hex_value(float_num)
|
29
|
-
base, remainder = float_num.to_s.split('.')
|
30
|
-
[base.to_i.to_s(16).upcase, "0.#{remainder}".to_f]
|
5
|
+
def to_hex(precision=nil)
|
6
|
+
Hexadecimal.new(self, precision)
|
31
7
|
end
|
32
8
|
end
|
33
9
|
|
34
10
|
module FloatValuesFromString
|
35
11
|
def to_float
|
12
|
+
negative = false
|
13
|
+
str = self
|
14
|
+
|
15
|
+
if self[0].eql? "-"
|
16
|
+
negative = true
|
17
|
+
str = self[1..self.length]
|
18
|
+
end
|
19
|
+
|
36
20
|
number = 0
|
37
21
|
base, remainder = self.split('.')
|
38
22
|
|
39
23
|
get_sum(base.split(//).reverse!) { |element, index| number += element.to_i(16) * (16 ** index) } if base
|
40
24
|
get_sum(remainder.split(//)) { |element, index| number += element.to_i(16).to_f / (16 ** (index + 1)) } if remainder
|
41
25
|
|
26
|
+
number = number * -1 if negative
|
27
|
+
|
42
28
|
number
|
43
29
|
end
|
44
30
|
|
@@ -53,6 +39,37 @@ end
|
|
53
39
|
|
54
40
|
class Float
|
55
41
|
include HexValuesFromFloat
|
42
|
+
|
43
|
+
alias :old_sum :+
|
44
|
+
alias :old_min :-
|
45
|
+
alias :old_multiply :*
|
46
|
+
alias :old_divide :/
|
47
|
+
alias :old_pow :**
|
48
|
+
|
49
|
+
def +(other_obj)
|
50
|
+
return other_obj + self if other_obj.instance_of? Hexadecimal
|
51
|
+
return old_sum(other_obj)
|
52
|
+
end
|
53
|
+
|
54
|
+
def -(other_obj)
|
55
|
+
return self.to_hex - other_obj if other_obj.instance_of? Hexadecimal
|
56
|
+
return old_min(other_obj)
|
57
|
+
end
|
58
|
+
|
59
|
+
def *(other_obj)
|
60
|
+
return other_obj * self if other_obj.instance_of? Hexadecimal
|
61
|
+
return old_multiply(other_obj)
|
62
|
+
end
|
63
|
+
|
64
|
+
def /(other_obj)
|
65
|
+
return self.to_hex / other_obj if other_obj.instance_of? Hexadecimal
|
66
|
+
return old_divide(other_obj)
|
67
|
+
end
|
68
|
+
|
69
|
+
def **(other_obj)
|
70
|
+
return self ** other_obj.to_float if other_obj.instance_of? Hexadecimal
|
71
|
+
return old_pow(other_obj)
|
72
|
+
end
|
56
73
|
end
|
57
74
|
|
58
75
|
class String
|
@@ -60,7 +77,44 @@ class String
|
|
60
77
|
end
|
61
78
|
|
62
79
|
class Fixnum
|
63
|
-
|
80
|
+
alias :old_sum :+
|
81
|
+
alias :old_min :-
|
82
|
+
alias :old_multiply :*
|
83
|
+
alias :old_divide :/
|
84
|
+
alias :old_pow :**
|
85
|
+
|
86
|
+
def to_hex(precision=Hexadecimal::MAX_DECIMALS)
|
64
87
|
self.to_f.to_hex(precision)
|
65
88
|
end
|
89
|
+
|
90
|
+
def +(other_obj)
|
91
|
+
return other_obj + self if other_obj.instance_of? Hexadecimal
|
92
|
+
return old_sum(other_obj)
|
93
|
+
end
|
94
|
+
|
95
|
+
def -(other_obj)
|
96
|
+
return self.to_hex - other_obj if other_obj.instance_of? Hexadecimal
|
97
|
+
return old_min(other_obj)
|
98
|
+
end
|
99
|
+
|
100
|
+
def *(other_obj)
|
101
|
+
return other_obj * self if other_obj.instance_of? Hexadecimal
|
102
|
+
return old_multiply(other_obj)
|
103
|
+
end
|
104
|
+
|
105
|
+
def /(other_obj)
|
106
|
+
return self.to_hex / other_obj if other_obj.instance_of? Hexadecimal
|
107
|
+
return old_divide(other_obj)
|
108
|
+
end
|
109
|
+
|
110
|
+
def **(other_obj)
|
111
|
+
return self ** other_obj.to_float if other_obj.instance_of? Hexadecimal
|
112
|
+
return old_pow(other_obj)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
class NilClass
|
117
|
+
def to_hex(precision=Hexadecimal::MAX_DECIMALS)
|
118
|
+
Hexadecimal.new(0, precision)
|
119
|
+
end
|
66
120
|
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
class Hexadecimal
|
2
|
+
MAX_DECIMALS = 14
|
3
|
+
|
4
|
+
def initialize(float_representation=0.0, precision=MAX_DECIMALS)
|
5
|
+
precision = MAX_DECIMALS if precision.nil? || precision < 0
|
6
|
+
@max_decimals=precision
|
7
|
+
@float_representation=float_representation
|
8
|
+
end
|
9
|
+
|
10
|
+
# Public: return the float value of the Hexadecimal.
|
11
|
+
#
|
12
|
+
# Returns the Float value of the Hexadecimal.
|
13
|
+
def to_float
|
14
|
+
@float_representation
|
15
|
+
end
|
16
|
+
|
17
|
+
# Public: return a String representation.
|
18
|
+
#
|
19
|
+
# Returns a String representation of the Hexadecimal object.
|
20
|
+
def to_s
|
21
|
+
return "Infinity" unless @float_representation.to_f.infinite?.nil?
|
22
|
+
num,dec = get_hex_value(@float_representation)
|
23
|
+
first = num
|
24
|
+
second = ""
|
25
|
+
iterations = 0
|
26
|
+
while dec > 0 && iterations < @max_decimals
|
27
|
+
num, dec = get_hex_value(dec*16)
|
28
|
+
second << num
|
29
|
+
iterations += 1
|
30
|
+
end
|
31
|
+
|
32
|
+
if "".eql? second
|
33
|
+
first
|
34
|
+
else
|
35
|
+
normalize_negativity "#{first}.#{second}", @float_representation
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Public: Sum two values.
|
40
|
+
#
|
41
|
+
# other_obj - An Hexadecimal object, a Fixnum or a Float
|
42
|
+
#
|
43
|
+
# Returns an Hexadecimal object, sum of the two objects.
|
44
|
+
def +(other_obj)
|
45
|
+
return operation(self, other_obj, "+".to_sym)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Public: Substracts two values.
|
49
|
+
#
|
50
|
+
# other_obj - An Hexadecimal object, a Fixnum or a Float
|
51
|
+
#
|
52
|
+
# Returns an Hexadecimal object, the substaction of the second object over the first.
|
53
|
+
def -(other_obj)
|
54
|
+
self + (other_obj * -1)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
# Public: Multiply two values.
|
59
|
+
#
|
60
|
+
# other_obj - An Hexadecimal object, a Fixnum or a Float
|
61
|
+
#
|
62
|
+
# Returns an Hexadecimal object, the multiplication of the operands.
|
63
|
+
def *(other_obj)
|
64
|
+
return operation(self, other_obj, "*".to_sym)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Public: Divide two values.
|
68
|
+
#
|
69
|
+
# other_obj - An Hexadecimal object, a Fixnum or a Float
|
70
|
+
#
|
71
|
+
# Returns an Hexadecimal object, the division of the operands.
|
72
|
+
def /(other_obj)
|
73
|
+
return operation(self, other_obj, "/".to_sym)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Public: Pow two values.
|
77
|
+
#
|
78
|
+
# other_obj - An Hexadecimal object, a Fixnum or a Float
|
79
|
+
#
|
80
|
+
# Returns an Hexadecimal object, the power of the operands.
|
81
|
+
def **(other_obj)
|
82
|
+
return operation(self, other_obj, "**".to_sym)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Public: Transform a given string into a Hexadecimal object.
|
86
|
+
#
|
87
|
+
# hex_representation - A String representing a Hexadecimal value
|
88
|
+
#
|
89
|
+
# Examples:
|
90
|
+
# Hexadecimal.from_string("EA.4")
|
91
|
+
#
|
92
|
+
# Returns an Hexadecimal object. Or throws an exception if the string can't be parsed.
|
93
|
+
def self.from_string(hex_representation)
|
94
|
+
return Hexadecimal.new if hex_representation.nil?
|
95
|
+
Hexadecimal.new(hex_representation.to_float)
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
def max_decimals
|
101
|
+
@max_decimals
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def get_hex_value(float_num)
|
107
|
+
base, remainder = float_num.to_s.split('.')
|
108
|
+
[base.to_i.to_s(16).upcase, "0.#{remainder}".to_f]
|
109
|
+
end
|
110
|
+
|
111
|
+
def normalize_negativity(string, float_num)
|
112
|
+
if string[0] != "-" && float_num < 0
|
113
|
+
"-#{string}"
|
114
|
+
else
|
115
|
+
string
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def operation(hex1, hex2, operation)
|
120
|
+
if hex2.instance_of? Hexadecimal
|
121
|
+
float_result = hex1.to_float.send(operation, hex2.to_float)
|
122
|
+
max_precision = (hex1.max_decimals > hex2.max_decimals) ? hex1.max_decimals : hex2.max_decimals
|
123
|
+
else
|
124
|
+
float_result = hex1.to_float.send(operation, hex2)
|
125
|
+
max_precision = hex1.max_decimals
|
126
|
+
end
|
127
|
+
|
128
|
+
Hexadecimal.new(float_result, max_precision)
|
129
|
+
end
|
130
|
+
end
|
data/lib/hex_values/version.rb
CHANGED
@@ -0,0 +1,462 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hexadecimal do
|
4
|
+
describe "from_string" do
|
5
|
+
it "transform a string into an Hexadecimal object" do
|
6
|
+
hex = Hexadecimal.from_string("EA.4")
|
7
|
+
hex.to_s.must_equal "EA.4"
|
8
|
+
hex.to_float.must_equal 234.25
|
9
|
+
end
|
10
|
+
|
11
|
+
it "transform an empty string into an Hexadecimal object with value 0" do
|
12
|
+
hex = Hexadecimal.from_string("")
|
13
|
+
hex.to_s.must_equal "0"
|
14
|
+
hex.to_float.must_equal 0.0
|
15
|
+
end
|
16
|
+
|
17
|
+
it "transform a negative hex string into an Hexadecimal object" do
|
18
|
+
hex = Hexadecimal.from_string("-EA.4")
|
19
|
+
hex.to_s.must_equal "-EA.4"
|
20
|
+
hex.to_float.must_equal -234.25
|
21
|
+
end
|
22
|
+
|
23
|
+
it "transform a nil into 0" do
|
24
|
+
hex = Hexadecimal.from_string(nil)
|
25
|
+
hex.to_s.must_equal "0"
|
26
|
+
hex.to_float.must_equal 0.0
|
27
|
+
end
|
28
|
+
|
29
|
+
it "transform a nil into 0 (nil as a object)" do
|
30
|
+
hex = nil.to_hex
|
31
|
+
|
32
|
+
hex.to_s.must_equal "0"
|
33
|
+
hex.to_float.must_equal 0.0
|
34
|
+
end
|
35
|
+
|
36
|
+
it "transform a no parseable string into 0" do
|
37
|
+
hex = Hexadecimal.from_string("ZZZZzz")
|
38
|
+
|
39
|
+
hex.to_s.must_equal "0"
|
40
|
+
hex.to_float.must_equal 0.0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "plus" do
|
45
|
+
it "sum two positive hexadecimals objects" do
|
46
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
47
|
+
hex2 = Hexadecimal.from_string "EA.4"
|
48
|
+
|
49
|
+
result = hex1 + hex2
|
50
|
+
|
51
|
+
result.must_be_instance_of Hexadecimal
|
52
|
+
result.to_float.must_equal 468.5
|
53
|
+
result.to_s.must_equal "1D4.8"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "sum one hexadecimal object with a float" do
|
57
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
58
|
+
|
59
|
+
result = hex1 + 234.25
|
60
|
+
|
61
|
+
result.must_be_instance_of Hexadecimal
|
62
|
+
result.to_float.must_equal 468.5
|
63
|
+
result.to_s.must_equal "1D4.8"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "sum one float with an hexadecimal object" do
|
67
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
68
|
+
|
69
|
+
result = 234.25 + hex1
|
70
|
+
|
71
|
+
result.must_be_instance_of Hexadecimal
|
72
|
+
result.to_float.must_equal 468.5
|
73
|
+
result.to_s.must_equal "1D4.8"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "sum two floats (check that + is not broken)" do
|
77
|
+
result = 234.25 + 234.25
|
78
|
+
|
79
|
+
result.must_equal 468.5
|
80
|
+
end
|
81
|
+
|
82
|
+
it "sum one hexadecimal object with an integer" do
|
83
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
84
|
+
|
85
|
+
result = hex1 + 234
|
86
|
+
|
87
|
+
result.must_be_instance_of Hexadecimal
|
88
|
+
result.to_float.must_equal 468.25
|
89
|
+
result.to_s.must_equal "1D4.4"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "sum one integer with an hexadecimal object" do
|
93
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
94
|
+
|
95
|
+
result = 234 + hex1
|
96
|
+
|
97
|
+
result.must_be_instance_of Hexadecimal
|
98
|
+
result.to_float.must_equal 468.25
|
99
|
+
result.to_s.must_equal "1D4.4"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "sum two integer (check that + is not broken)" do
|
103
|
+
result = 234 + 234
|
104
|
+
|
105
|
+
result.must_equal 468
|
106
|
+
end
|
107
|
+
|
108
|
+
it "sum one positive hexadecimal object and one negative hexadecimal object" do
|
109
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
110
|
+
hex2 = Hexadecimal.from_string "-EA.4"
|
111
|
+
|
112
|
+
result = hex1 + hex2
|
113
|
+
|
114
|
+
result.must_be_instance_of Hexadecimal
|
115
|
+
result.to_float.must_equal 0.0
|
116
|
+
result.to_s.must_equal "0"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "minus" do
|
121
|
+
it "substracts two positive hexadecimals objects" do
|
122
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
123
|
+
hex2 = Hexadecimal.from_string "EA.4"
|
124
|
+
|
125
|
+
result = hex1 - hex2
|
126
|
+
|
127
|
+
result.must_be_instance_of Hexadecimal
|
128
|
+
result.to_float.must_equal 0.0
|
129
|
+
result.to_s.must_equal "0"
|
130
|
+
end
|
131
|
+
|
132
|
+
it "substracts one hexadecimal object with a float" do
|
133
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
134
|
+
|
135
|
+
result = hex1 - 234.25
|
136
|
+
|
137
|
+
result.must_be_instance_of Hexadecimal
|
138
|
+
result.to_float.must_equal 0.0
|
139
|
+
result.to_s.must_equal "0"
|
140
|
+
end
|
141
|
+
|
142
|
+
it "substracts one float with an hexadecimal object" do
|
143
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
144
|
+
|
145
|
+
result = 234.25 - hex1
|
146
|
+
|
147
|
+
result.must_be_instance_of Hexadecimal
|
148
|
+
result.to_float.must_equal 0.0
|
149
|
+
result.to_s.must_equal "0"
|
150
|
+
end
|
151
|
+
|
152
|
+
it "substracts two floats (check minus works)" do
|
153
|
+
result = 234.25 - 234.25
|
154
|
+
|
155
|
+
result.must_equal 0.0
|
156
|
+
end
|
157
|
+
|
158
|
+
it "substracts one hexadecimal object with an integer" do
|
159
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
160
|
+
|
161
|
+
result = hex1 - 234
|
162
|
+
|
163
|
+
result.must_be_instance_of Hexadecimal
|
164
|
+
result.to_float.must_equal 0.25
|
165
|
+
result.to_s.must_equal "0.4"
|
166
|
+
end
|
167
|
+
|
168
|
+
it "substracts one integer with an hexadecimal object" do
|
169
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
170
|
+
|
171
|
+
result = 234 - hex1
|
172
|
+
|
173
|
+
result.must_be_instance_of Hexadecimal
|
174
|
+
result.to_float.must_equal -0.25
|
175
|
+
result.to_s.must_equal "-0.4"
|
176
|
+
end
|
177
|
+
|
178
|
+
it "substracts two integers (check minus works)" do
|
179
|
+
result = 234 - 230
|
180
|
+
|
181
|
+
result.must_equal 4
|
182
|
+
end
|
183
|
+
|
184
|
+
it "substracts one positive hexadecimal object and one negative hexadecimal object" do
|
185
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
186
|
+
hex2 = Hexadecimal.from_string "-EA.4"
|
187
|
+
|
188
|
+
result = hex1 - hex2
|
189
|
+
|
190
|
+
result.must_be_instance_of Hexadecimal
|
191
|
+
result.to_float.must_equal 468.5
|
192
|
+
result.to_s.must_equal "1D4.8"
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "multiply" do
|
197
|
+
it "multiply two positive hexadecimals objects" do
|
198
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
199
|
+
hex2 = Hexadecimal.from_string "EA.4"
|
200
|
+
|
201
|
+
result = hex1 * hex2
|
202
|
+
|
203
|
+
result.must_be_instance_of Hexadecimal
|
204
|
+
result.to_float.must_equal 54873.0625
|
205
|
+
result.to_s.must_equal "D659.1"
|
206
|
+
end
|
207
|
+
|
208
|
+
it "multiply one hexadecimal object with a float" do
|
209
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
210
|
+
|
211
|
+
result = hex1 * 234.25
|
212
|
+
|
213
|
+
result.must_be_instance_of Hexadecimal
|
214
|
+
result.to_float.must_equal 54873.0625
|
215
|
+
result.to_s.must_equal "D659.1"
|
216
|
+
end
|
217
|
+
|
218
|
+
it "multiply one float with an hexadecimal object" do
|
219
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
220
|
+
|
221
|
+
result = 234.25 * hex1
|
222
|
+
|
223
|
+
result.must_be_instance_of Hexadecimal
|
224
|
+
result.to_float.must_equal 54873.0625
|
225
|
+
result.to_s.must_equal "D659.1"
|
226
|
+
end
|
227
|
+
|
228
|
+
it "multiply two floats (check multiply is not broken)" do
|
229
|
+
result = 234.25 * 234.25
|
230
|
+
|
231
|
+
result.must_equal 54873.0625
|
232
|
+
end
|
233
|
+
|
234
|
+
it "multiply one hexadecimal object with an integer" do
|
235
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
236
|
+
|
237
|
+
result = hex1 * 1
|
238
|
+
|
239
|
+
result.must_be_instance_of Hexadecimal
|
240
|
+
result.to_float.must_equal 234.25
|
241
|
+
result.to_s.must_equal "EA.4"
|
242
|
+
end
|
243
|
+
|
244
|
+
it "multiply one hexadecimal object with a negative integer" do
|
245
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
246
|
+
|
247
|
+
result = hex1 * -1
|
248
|
+
|
249
|
+
result.must_be_instance_of Hexadecimal
|
250
|
+
result.to_float.must_equal -234.25
|
251
|
+
result.to_s.must_equal "-EA.4"
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
it "multiply one integer with an hexadecimal object" do
|
256
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
257
|
+
|
258
|
+
result = -1 * hex1
|
259
|
+
|
260
|
+
result.must_be_instance_of Hexadecimal
|
261
|
+
result.to_float.must_equal -234.25
|
262
|
+
result.to_s.must_equal "-EA.4"
|
263
|
+
end
|
264
|
+
|
265
|
+
it "multiply two integers (check multiply is not broken)" do
|
266
|
+
result = 2 * 2
|
267
|
+
|
268
|
+
result.must_equal 4
|
269
|
+
end
|
270
|
+
|
271
|
+
it "multiply one positive hexadecimal object and one negative hexadecimal object" do
|
272
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
273
|
+
hex2 = Hexadecimal.from_string "-EA.4"
|
274
|
+
|
275
|
+
result = hex1 * hex2
|
276
|
+
|
277
|
+
result.must_be_instance_of Hexadecimal
|
278
|
+
result.to_float.must_equal -54873.0625
|
279
|
+
result.to_s.must_equal "-D659.1"
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
describe "divide" do
|
284
|
+
it "divides two positive hexadecimals objects" do
|
285
|
+
hex1 = Hexadecimal.from_string("EA.4")
|
286
|
+
hex2 = Hexadecimal.from_string("EA.4")
|
287
|
+
|
288
|
+
result = hex1 / hex2
|
289
|
+
|
290
|
+
result.must_be_instance_of Hexadecimal
|
291
|
+
result.to_float.must_equal 1
|
292
|
+
result.to_s.must_equal "1"
|
293
|
+
end
|
294
|
+
|
295
|
+
it "divides one hexadecimal object with a float" do
|
296
|
+
hex1 = Hexadecimal.from_string("EA.4")
|
297
|
+
|
298
|
+
result = hex1 / 234.25
|
299
|
+
|
300
|
+
result.must_be_instance_of Hexadecimal
|
301
|
+
result.to_float.must_equal 1
|
302
|
+
result.to_s.must_equal "1"
|
303
|
+
end
|
304
|
+
|
305
|
+
it "divides one float with an hexadecimal object" do
|
306
|
+
hex1 = Hexadecimal.from_string("EA.4")
|
307
|
+
|
308
|
+
result = 234.25 / hex1
|
309
|
+
|
310
|
+
result.must_be_instance_of Hexadecimal
|
311
|
+
result.to_float.must_equal 1
|
312
|
+
result.to_s.must_equal "1"
|
313
|
+
end
|
314
|
+
|
315
|
+
it "divides two floats (check divide is not broken)" do
|
316
|
+
result = 234.25 / 234.25
|
317
|
+
|
318
|
+
result.must_equal 1
|
319
|
+
end
|
320
|
+
|
321
|
+
it "divides one hexadecimal object with an integer" do
|
322
|
+
hex1 = Hexadecimal.from_string("EA.4")
|
323
|
+
|
324
|
+
result = hex1 / 1
|
325
|
+
|
326
|
+
result.must_be_instance_of Hexadecimal
|
327
|
+
result.to_float.must_equal 234.25
|
328
|
+
result.to_s.must_equal "EA.4"
|
329
|
+
end
|
330
|
+
|
331
|
+
it "divides one integer with an hexadecimal object" do
|
332
|
+
hex1 = Hexadecimal.from_string("EA.4")
|
333
|
+
|
334
|
+
result = 1 / hex1
|
335
|
+
|
336
|
+
result.must_be_instance_of Hexadecimal
|
337
|
+
result.to_s.must_equal "0.0117C4FC72BFCB"
|
338
|
+
result.to_float.must_equal 0.004268943436499467
|
339
|
+
end
|
340
|
+
|
341
|
+
it "divides two integerse (check divide is not broken)" do
|
342
|
+
result = 1 / 2
|
343
|
+
|
344
|
+
result.must_equal 0
|
345
|
+
end
|
346
|
+
|
347
|
+
it "divides one positive hexadecimal object and one negative hexadecimal object" do
|
348
|
+
hex1 = Hexadecimal.from_string("EA.4")
|
349
|
+
hex2 = Hexadecimal.from_string("-EA.4")
|
350
|
+
|
351
|
+
result = hex1 / hex2
|
352
|
+
|
353
|
+
result.must_be_instance_of Hexadecimal
|
354
|
+
result.to_float.must_equal -1
|
355
|
+
result.to_s.must_equal "-1"
|
356
|
+
end
|
357
|
+
|
358
|
+
it "divide by zero return Infinity" do
|
359
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
360
|
+
|
361
|
+
result = hex1 / 0
|
362
|
+
|
363
|
+
result.must_be_instance_of Hexadecimal
|
364
|
+
result.to_float.must_equal Float::INFINITY
|
365
|
+
result.to_s.must_equal "Infinity"
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
describe "pow" do
|
370
|
+
it "pows two positive hexadecimals objects" do
|
371
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
372
|
+
hex2 = 2.34.to_hex
|
373
|
+
|
374
|
+
result = hex1 ** hex2
|
375
|
+
|
376
|
+
result.must_be_instance_of Hexadecimal
|
377
|
+
result.to_float.must_equal 350792.4704875763
|
378
|
+
result.to_s.must_equal "55A48.7871DFB161FF7C"
|
379
|
+
end
|
380
|
+
|
381
|
+
it "pows two big positive hexadecimals object (should return Infinite)" do
|
382
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
383
|
+
hex2 = Hexadecimal.from_string "EA.4"
|
384
|
+
|
385
|
+
result = hex1 ** hex2
|
386
|
+
|
387
|
+
result.must_be_instance_of Hexadecimal
|
388
|
+
result.to_float.must_equal Float::INFINITY
|
389
|
+
result.to_s.must_equal "Infinity"
|
390
|
+
end
|
391
|
+
|
392
|
+
it "pows one hexadecimal object with a float" do
|
393
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
394
|
+
|
395
|
+
result = hex1 ** 2.34
|
396
|
+
|
397
|
+
result.must_be_instance_of Hexadecimal
|
398
|
+
result.to_float.must_equal 350792.4704875763
|
399
|
+
result.to_s.must_equal "55A48.7871DFB161FF7C"
|
400
|
+
end
|
401
|
+
|
402
|
+
it "pows one float with an hexadecimal object" do
|
403
|
+
hex1 = Hexadecimal.from_string "A"
|
404
|
+
|
405
|
+
result = 2.34 ** hex1
|
406
|
+
|
407
|
+
result.must_be_instance_of Float
|
408
|
+
result.must_equal 4922.19227058666
|
409
|
+
end
|
410
|
+
|
411
|
+
it "pows two float (check pow is not broken)" do
|
412
|
+
result = 2.34 ** 10.0
|
413
|
+
|
414
|
+
result.must_equal 4922.19227058666
|
415
|
+
end
|
416
|
+
|
417
|
+
it "pows one hexadecimal object with an integer" do
|
418
|
+
hex1 = Hexadecimal.from_string "EA.4"
|
419
|
+
|
420
|
+
result = hex1 ** 2
|
421
|
+
|
422
|
+
result.must_be_instance_of Hexadecimal
|
423
|
+
result.to_float.must_equal 54873.0625
|
424
|
+
result.to_s.must_equal "D659.1"
|
425
|
+
end
|
426
|
+
|
427
|
+
it "pows one integer with an hexadecimal object" do
|
428
|
+
hex1 = Hexadecimal.from_string "A"
|
429
|
+
|
430
|
+
result = 2 ** hex1
|
431
|
+
|
432
|
+
result.must_be_instance_of Fixnum
|
433
|
+
result = 1024
|
434
|
+
end
|
435
|
+
|
436
|
+
it "pows one integer with an hexadecimal (with comma) object" do
|
437
|
+
hex1 = Hexadecimal.from_string "A.1"
|
438
|
+
|
439
|
+
result = 2 ** hex1
|
440
|
+
|
441
|
+
result.must_be_instance_of Float
|
442
|
+
result.must_equal 1069.3363532056717
|
443
|
+
end
|
444
|
+
|
445
|
+
it "pows two integer (check pot is not broken)" do
|
446
|
+
result = 2 ** 3
|
447
|
+
|
448
|
+
result.must_equal 8
|
449
|
+
end
|
450
|
+
|
451
|
+
it "pows one positive hexadecimal object and one negative hexadecimal object" do
|
452
|
+
hex1 = Hexadecimal.from_string "A.1"
|
453
|
+
hex2 = Hexadecimal.from_string "-A.2"
|
454
|
+
|
455
|
+
result = hex1 ** hex2
|
456
|
+
|
457
|
+
result.must_be_instance_of Hexadecimal
|
458
|
+
result.to_s.must_equal "7.67161712141568"
|
459
|
+
result.to_float.must_equal 7.040489085609713e-11
|
460
|
+
end
|
461
|
+
end
|
462
|
+
end
|
data/spec/hex_values_spec.rb
CHANGED
@@ -3,72 +3,76 @@ require 'spec_helper'
|
|
3
3
|
describe 'HexValues' do
|
4
4
|
describe "float to hex" do
|
5
5
|
it "converts 234.25 to EA.4" do
|
6
|
-
234.25.to_hex.must_equal "EA.4"
|
6
|
+
234.25.to_hex.to_s.must_equal "EA.4"
|
7
7
|
end
|
8
8
|
|
9
9
|
it "converts 1875.37 to 753.5EB851EB85" do
|
10
|
-
1875.37.to_hex.must_equal "753.5EB851EB851EB8"
|
10
|
+
1875.37.to_hex.to_s.must_equal "753.5EB851EB851EB8"
|
11
11
|
end
|
12
12
|
|
13
13
|
it "converts 5476.28 to 1564.47AE147AE1" do
|
14
|
-
5476.28.to_hex.must_equal "1564.47AE147AE147AE"
|
14
|
+
5476.28.to_hex.to_s.must_equal "1564.47AE147AE147AE"
|
15
15
|
end
|
16
16
|
|
17
17
|
it "converts 7763783.89 to 767747.E3D70A3C" do
|
18
|
-
7763783.89.to_hex.must_equal "767747.E3D70A3D70A3D7"
|
18
|
+
7763783.89.to_hex.to_s.must_equal "767747.E3D70A3D70A3D7"
|
19
19
|
end
|
20
20
|
|
21
21
|
it "converts 1 to 1" do
|
22
|
-
1.to_hex.must_equal "1"
|
22
|
+
1.to_hex.to_s.must_equal "1"
|
23
23
|
end
|
24
24
|
|
25
25
|
it "converts 10 to A" do
|
26
|
-
10.to_hex.must_equal "A"
|
26
|
+
10.to_hex.to_s.must_equal "A"
|
27
27
|
end
|
28
28
|
|
29
29
|
it "converts 0.25 to 0.4" do
|
30
|
-
0.25.to_hex.must_equal "0.4"
|
30
|
+
0.25.to_hex.to_s.must_equal "0.4"
|
31
31
|
end
|
32
32
|
|
33
33
|
it "converts 0.21 to 0.35C28F5C28F5C2" do
|
34
|
-
0.21.to_hex.must_equal "0.35C28F5C28F5C2"
|
34
|
+
0.21.to_hex.to_s.must_equal "0.35C28F5C28F5C2"
|
35
35
|
end
|
36
36
|
|
37
37
|
it "converts 0 to 0" do
|
38
|
-
0.to_hex.must_equal "0"
|
38
|
+
0.to_hex.to_s.must_equal "0"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "converts -234.25 to -EA.4" do
|
42
|
+
-234.25.to_hex.to_s.must_equal "-EA.4"
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
42
46
|
describe "float to hex with precision" do
|
43
47
|
it "converts 234.25 to EA.4 (no matter precision)" do
|
44
|
-
result = 234.25.to_hex(140)
|
48
|
+
result = 234.25.to_hex(140).to_s
|
45
49
|
result.must_equal "EA.4"
|
46
50
|
num, dec = result.split('.')
|
47
51
|
dec.size.must_equal 1
|
48
52
|
end
|
49
53
|
|
50
54
|
it "converts 1875.37 with 5 numbers after period" do
|
51
|
-
result = 1875.37.to_hex(5)
|
55
|
+
result = 1875.37.to_hex(5).to_s
|
52
56
|
result.must_equal "753.5EB85"
|
53
57
|
num, dec = result.split('.')
|
54
58
|
dec.size.must_equal 5
|
55
59
|
end
|
56
60
|
|
57
61
|
it "converts 1875.37 with 50 numbers after period" do
|
58
|
-
result = 1875.37.to_hex(50)
|
62
|
+
result = 1875.37.to_hex(50).to_s
|
59
63
|
num, dec = result.split('.')
|
60
64
|
dec.size.must_equal 50
|
61
65
|
end
|
62
66
|
|
63
67
|
it "converts 1875.37 with 0 numbers after period" do
|
64
|
-
result = 1875.37.to_hex(0)
|
68
|
+
result = 1875.37.to_hex(0).to_s
|
65
69
|
result.must_equal "753"
|
66
70
|
num, dec = result.split('.')
|
67
71
|
dec.must_be_nil
|
68
72
|
end
|
69
73
|
|
70
74
|
it "converts 1875.37 with 14 numbers after period (if a negative precision is provided)" do
|
71
|
-
result = 1875.37.to_hex(-1)
|
75
|
+
result = 1875.37.to_hex(-1).to_s
|
72
76
|
result.must_equal "753.5EB851EB851EB8"
|
73
77
|
num, dec = result.split('.')
|
74
78
|
dec.size.must_equal 14
|
@@ -77,23 +81,23 @@ describe 'HexValues' do
|
|
77
81
|
|
78
82
|
describe "integer to hex with precision" do
|
79
83
|
it "converts 1 to 1 (without parameter)" do
|
80
|
-
1.to_hex.must_equal "1"
|
84
|
+
1.to_hex.to_s.must_equal "1"
|
81
85
|
end
|
82
86
|
|
83
87
|
it "converts 1 to 1 (with precision = 40)" do
|
84
|
-
1.to_hex(14).must_equal "1"
|
88
|
+
1.to_hex(14).to_s.must_equal "1"
|
85
89
|
end
|
86
90
|
|
87
91
|
it "converts 1 to 1 (with precision = 0)" do
|
88
|
-
1.to_hex(0).must_equal "1"
|
92
|
+
1.to_hex(0).to_s.must_equal "1"
|
89
93
|
end
|
90
94
|
|
91
95
|
it "converts 1 to 1 (with precision = -1)" do
|
92
|
-
1.to_hex(-1).must_equal "1"
|
96
|
+
1.to_hex(-1).to_s.must_equal "1"
|
93
97
|
end
|
94
98
|
|
95
99
|
it "converts 13 to D (with precision = 12)" do
|
96
|
-
13.to_hex(12).must_equal "D"
|
100
|
+
13.to_hex(12).to_s.must_equal "D"
|
97
101
|
end
|
98
102
|
end
|
99
103
|
|
@@ -133,5 +137,9 @@ describe 'HexValues' do
|
|
133
137
|
it "converts an empty string to 0" do
|
134
138
|
"".to_float.must_equal 0
|
135
139
|
end
|
140
|
+
|
141
|
+
it "converts a no parseable string into 0" do
|
142
|
+
"ZZZZZZZ".to_float.must_equal 0
|
143
|
+
end
|
136
144
|
end
|
137
145
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hex_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.1'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -60,8 +60,10 @@ files:
|
|
60
60
|
- Rakefile
|
61
61
|
- hex_values.gemspec
|
62
62
|
- lib/hex_values.rb
|
63
|
+
- lib/hex_values/hexadecimal.rb
|
63
64
|
- lib/hex_values/version.rb
|
64
65
|
- script/benchmark
|
66
|
+
- spec/hex_values/hexadecimal_spec.rb
|
65
67
|
- spec/hex_values_spec.rb
|
66
68
|
- spec/performance_hex_values_test.rb
|
67
69
|
- spec/spec_helper.rb
|
@@ -91,6 +93,7 @@ specification_version: 3
|
|
91
93
|
summary: Convert any float or fixnum to the correct representation in hexadecimal,
|
92
94
|
and convert any hexadecimal string in their corresponding float representation.
|
93
95
|
test_files:
|
96
|
+
- spec/hex_values/hexadecimal_spec.rb
|
94
97
|
- spec/hex_values_spec.rb
|
95
98
|
- spec/performance_hex_values_test.rb
|
96
99
|
- spec/spec_helper.rb
|