floatboat 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +2 -0
- data/floatboat.gemspec +22 -0
- data/lib/floatboat.rb +6 -0
- data/lib/floatboat/converter.rb +25 -0
- data/lib/floatboat/oracle.rb +54 -0
- data/lib/floatboat/version.rb +3 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 84ac7cbf207f57333aac8860fc5ac05766f23a9d
|
4
|
+
data.tar.gz: 03b008b4a11db3b6535038c690cccc1ff8ce7f60
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d64a4f6b9eeb35f260711b01cc98cfc8ceff8b8126ab3a76002827d6313392b379205ea1af1c91a2899f53f3c79ddf07d2b0fe4f84c2f61f4bb86c8f3fb459c5
|
7
|
+
data.tar.gz: 9f318e7a690fdbba94118bf3dbe19d9eef4024ca8ae1ed1f9e05e90e98a909dda77b5cd123176619c9fbe4467a10ca5d692b51d5150f043a27e5e64429a63e52
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 John Feminella
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# floatboat
|
2
|
+
|
3
|
+
Floatboat is a tiny Ruby library for peeking inside Ruby's IEEE 754 floating point representation.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
f = 12.125
|
9
|
+
|
10
|
+
o = Floatboat::Oracle.new(f)
|
11
|
+
# => #<Floatboat::Oracle:0x007fb1f1a9f1f0
|
12
|
+
# @exponent="10000000010",
|
13
|
+
# @mantissa="1000010000000000000000000000000000000000000000000000",
|
14
|
+
# @n_exponent=3,
|
15
|
+
# @n_mantissa=1.515625,
|
16
|
+
# @n_sign=1,
|
17
|
+
# @sign="0",
|
18
|
+
# @value=12.125>
|
19
|
+
|
20
|
+
o.to_bit_components
|
21
|
+
# => ["0", "10000000010", "1000010000000000000000000000000000000000000000000000"]
|
22
|
+
|
23
|
+
o.to_numerical_components
|
24
|
+
# => [1, 3, 1.515625]
|
25
|
+
|
26
|
+
f == o.n_sign * 2**o.n_exponent * o.n_mantissa
|
27
|
+
# => true
|
28
|
+
|
29
|
+
f == o.to_f
|
30
|
+
# => true
|
31
|
+
|
32
|
+
o.to_f.nan?
|
33
|
+
# => false
|
34
|
+
|
35
|
+
o.to_f.infinite?
|
36
|
+
# => false
|
37
|
+
|
38
|
+
nan = Floatboat::Oracle.new(Float::NAN)
|
39
|
+
!!nan.to_f.nan?
|
40
|
+
# => true
|
41
|
+
|
42
|
+
inf = Floatboat::Oracle.new(Float::INFINITY)
|
43
|
+
!!inf.to_f.infinite?
|
44
|
+
# => true
|
45
|
+
```
|
46
|
+
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
gem 'floatboat'
|
53
|
+
|
54
|
+
And then execute:
|
55
|
+
|
56
|
+
$ bundle
|
57
|
+
|
58
|
+
Or install it yourself as:
|
59
|
+
|
60
|
+
$ gem install floatboat
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it ( https://github.com/[my-github-username]/floatboat/fork )
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/floatboat.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'floatboat/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "floatboat"
|
8
|
+
spec.version = Floatboat::VERSION
|
9
|
+
spec.authors = ["John Feminella"]
|
10
|
+
spec.email = ["jxf+rubygems@jxf.me"]
|
11
|
+
spec.summary = "Floatboat is a tiny Ruby library for interacting with Ruby's IEEE-754 floats."
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
data/lib/floatboat.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Floatboat
|
2
|
+
class Converter
|
3
|
+
def self.to_bits(float)
|
4
|
+
float_as_bytes = [float].pack('G').bytes
|
5
|
+
float_as_bytes.map { |n| "%08b" % n }.join
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.from_bits(bitstring)
|
9
|
+
[bitstring].pack('B*').unpack('G').first
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.binary_mantissa_to_decimal(binary_mantissa)
|
13
|
+
explicit_mantissa = "1.#{binary_mantissa}"
|
14
|
+
int, fraction = explicit_mantissa.split(".")
|
15
|
+
minus = int.delete!("-")
|
16
|
+
dec = (minus ? "-" : "") + int.to_i(2).to_s
|
17
|
+
|
18
|
+
if fraction
|
19
|
+
dec << (fraction.to_i(2) / 2.0**(fraction.size)).to_s[1..-1]
|
20
|
+
else
|
21
|
+
dec << ".0"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Floatboat
|
2
|
+
class Oracle
|
3
|
+
# From IEEE 754 double-precision floats.
|
4
|
+
SIGN_BITSIZE = 1
|
5
|
+
EXPONENT_BITSIZE = 11
|
6
|
+
MANTISSA_BITSIZE = 53
|
7
|
+
EXPONENT_BIAS = (2**(EXPONENT_BITSIZE - 1)) - 1
|
8
|
+
|
9
|
+
attr_reader :sign, :exponent, :mantissa
|
10
|
+
attr_reader :n_sign, :n_exponent, :n_mantissa
|
11
|
+
|
12
|
+
def initialize(f)
|
13
|
+
@value = f
|
14
|
+
bits = ::Floatboat::Converter.to_bits(@value)
|
15
|
+
|
16
|
+
@sign = bits[0]
|
17
|
+
@exponent = bits[1..11]
|
18
|
+
@mantissa = bits[12..63]
|
19
|
+
|
20
|
+
@n_sign = @sign == "0" ? 1 : -1
|
21
|
+
@n_exponent = @exponent.to_i(2) - EXPONENT_BIAS
|
22
|
+
@n_mantissa = ::Floatboat::Converter.binary_mantissa_to_decimal(@mantissa).to_f
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_bit_components
|
26
|
+
[
|
27
|
+
sign,
|
28
|
+
exponent,
|
29
|
+
mantissa
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_numerical_components
|
34
|
+
[
|
35
|
+
n_sign,
|
36
|
+
n_exponent,
|
37
|
+
n_mantissa
|
38
|
+
]
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_f
|
42
|
+
@to_f ||= compute_float_value
|
43
|
+
end
|
44
|
+
|
45
|
+
def compute_float_value
|
46
|
+
case @value
|
47
|
+
when Float::INFINITY
|
48
|
+
@value
|
49
|
+
else
|
50
|
+
(@value.nan?) ? Float::NAN : 2**n_exponent * n_mantissa * n_sign
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: floatboat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Feminella
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- jxf+rubygems@jxf.me
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- floatboat.gemspec
|
54
|
+
- lib/floatboat.rb
|
55
|
+
- lib/floatboat/converter.rb
|
56
|
+
- lib/floatboat/oracle.rb
|
57
|
+
- lib/floatboat/version.rb
|
58
|
+
homepage: ''
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.2.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Floatboat is a tiny Ruby library for interacting with Ruby's IEEE-754 floats.
|
82
|
+
test_files: []
|