hex_values 0.0.1 → 0.0.2
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/.travis.yml +3 -0
- data/Gemfile +6 -0
- data/README.md +5 -0
- data/Rakefile +2 -0
- data/lib/hex_values/version.rb +1 -1
- data/lib/hex_values.rb +10 -15
- data/script/benchmark +6 -0
- data/spec/performance_hex_values_test.rb +32 -0
- data/spec/spec_helper.rb +3 -0
- metadata +6 -2
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# HexValues
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/hex_values)
|
4
|
+
[](https://codeclimate.com/github/gianu/hex_values)
|
5
|
+
[](https://travis-ci.org/gianu/hex_values)
|
6
|
+
[](https://coveralls.io/r/gianu/hex_values)
|
7
|
+
|
3
8
|
Transform your float to hexadecimals and viceversa!
|
4
9
|
|
5
10
|
## Installation
|
data/Rakefile
CHANGED
data/lib/hex_values/version.rb
CHANGED
data/lib/hex_values.rb
CHANGED
@@ -34,24 +34,19 @@ module FloatValuesFromString
|
|
34
34
|
number = 0
|
35
35
|
base, remainder = self.split('.')
|
36
36
|
|
37
|
-
if base
|
38
|
-
|
39
|
-
base.each_char { |c| arr << c }
|
40
|
-
arr.reverse!.each_index do |index|
|
41
|
-
number += arr[index].to_i(16) * (16 ** index)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
if remainder
|
46
|
-
arr=[]
|
47
|
-
remainder.each_char { |c| arr << c }
|
48
|
-
arr.each_index do |index|
|
49
|
-
number += arr[index].to_i(16).to_f / (16 ** (index + 1))
|
50
|
-
end
|
51
|
-
end
|
37
|
+
get_sum(base.split(//).reverse!) { |element, index| number += element.to_i(16) * (16 ** index) } if base
|
38
|
+
get_sum(remainder.split(//)) { |element, index| number += element.to_i(16).to_f / (16 ** (index + 1)) } if remainder
|
52
39
|
|
53
40
|
number
|
54
41
|
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def get_sum(array, &block)
|
46
|
+
array.each_index do |index|
|
47
|
+
block.call(array[index], index)
|
48
|
+
end
|
49
|
+
end
|
55
50
|
end
|
56
51
|
|
57
52
|
class Float
|
data/script/benchmark
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/benchmark'
|
3
|
+
|
4
|
+
require 'hex_values'
|
5
|
+
|
6
|
+
# This test is only useful to compare differents implementations of
|
7
|
+
# the algorithm used to transformt floats to hex and vicecersa.
|
8
|
+
class BenchHexValues < MiniTest::Unit::TestCase
|
9
|
+
def bench_float_to_hex
|
10
|
+
assert_performance_linear 0.9999 do |n|
|
11
|
+
n.times do
|
12
|
+
(98789879890.87).to_hex #O(1)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def bench_integer_to_hex
|
18
|
+
assert_performance_linear 0.999 do |n|
|
19
|
+
n.times do
|
20
|
+
9878987608.to_hex #O(1)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def bench_hex_to_float
|
26
|
+
assert_performance_linear 0.9999 do |n|
|
27
|
+
n.times do
|
28
|
+
"5f2f9e39.42".to_float #O(1)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.0.
|
4
|
+
version: 0.0.2
|
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-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -53,6 +53,7 @@ extra_rdoc_files: []
|
|
53
53
|
files:
|
54
54
|
- .gitignore
|
55
55
|
- .ruby-version
|
56
|
+
- .travis.yml
|
56
57
|
- Gemfile
|
57
58
|
- LICENSE
|
58
59
|
- README.md
|
@@ -60,7 +61,9 @@ files:
|
|
60
61
|
- hex_values.gemspec
|
61
62
|
- lib/hex_values.rb
|
62
63
|
- lib/hex_values/version.rb
|
64
|
+
- script/benchmark
|
63
65
|
- spec/hex_values_spec.rb
|
66
|
+
- spec/performance_hex_values_test.rb
|
64
67
|
- spec/spec_helper.rb
|
65
68
|
homepage: http://github.com/gianu/hex_values
|
66
69
|
licenses: []
|
@@ -89,4 +92,5 @@ summary: Convert any float or fixnum to the correct representation in hexadecima
|
|
89
92
|
and convert any hexadecimal string in their corresponding float representation.
|
90
93
|
test_files:
|
91
94
|
- spec/hex_values_spec.rb
|
95
|
+
- spec/performance_hex_values_test.rb
|
92
96
|
- spec/spec_helper.rb
|