ratapprox 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.idea/encodings.xml +4 -0
- data/.idea/misc.xml +27 -0
- data/.idea/modules.xml +8 -0
- data/.idea/ratapprox.iml +19 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/lib/ratapprox.rb +77 -0
- data/lib/ratapprox/version.rb +3 -0
- data/ratapprox.gemspec +23 -0
- metadata +93 -0
data/.gitignore
ADDED
data/.idea/encodings.xml
ADDED
data/.idea/misc.xml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="DaemonCodeAnalyzer">
|
4
|
+
<disable_hints />
|
5
|
+
</component>
|
6
|
+
<component name="DependencyValidationManager">
|
7
|
+
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
|
8
|
+
</component>
|
9
|
+
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
|
10
|
+
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
|
11
|
+
<OptionsSetting value="true" id="Add" />
|
12
|
+
<OptionsSetting value="true" id="Remove" />
|
13
|
+
<OptionsSetting value="true" id="Checkout" />
|
14
|
+
<OptionsSetting value="true" id="Update" />
|
15
|
+
<OptionsSetting value="true" id="Status" />
|
16
|
+
<OptionsSetting value="true" id="Edit" />
|
17
|
+
<ConfirmationsSetting value="0" id="Add" />
|
18
|
+
<ConfirmationsSetting value="0" id="Remove" />
|
19
|
+
</component>
|
20
|
+
<component name="ProjectModuleManager">
|
21
|
+
<modules />
|
22
|
+
</component>
|
23
|
+
<component name="ProjectRootManager" version="2" project-jdk-name="ruby-1.9.3-p545" project-jdk-type="RUBY_SDK" />
|
24
|
+
<component name="RunManager">
|
25
|
+
<list size="0" />
|
26
|
+
</component>
|
27
|
+
</project>
|
data/.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/ratapprox.iml" filepath="$PROJECT_DIR$/.idea/ratapprox.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
data/.idea/ratapprox.iml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="FacetManager">
|
4
|
+
<facet type="gem" name="Ruby Gem">
|
5
|
+
<configuration>
|
6
|
+
<option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
|
7
|
+
<option name="GEM_APP_TEST_PATH" value="" />
|
8
|
+
<option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
|
9
|
+
</configuration>
|
10
|
+
</facet>
|
11
|
+
</component>
|
12
|
+
<component name="NewModuleRootManager">
|
13
|
+
<content url="file://$MODULE_DIR$" />
|
14
|
+
<orderEntry type="inheritedJdk" />
|
15
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
16
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.7.2, ruby-1.9.3-p545) [gem]" level="application" />
|
17
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v10.3.2, ruby-1.9.3-p545) [gem]" level="application" />
|
18
|
+
</component>
|
19
|
+
</module>
|
data/.idea/vcs.xml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Gregory H. Halverson
|
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,31 @@
|
|
1
|
+
# Ratapprox
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ratapprox'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ratapprox
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/ratapprox/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/ratapprox.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Gregory H. Halverson
|
2
|
+
# 6 March 2015
|
3
|
+
|
4
|
+
# Improved rational approximation algorithm that overrides the to_r method of Float
|
5
|
+
|
6
|
+
require "ratapprox/version"
|
7
|
+
|
8
|
+
module Math
|
9
|
+
DEFAULT_MAXDEN = 1000000
|
10
|
+
|
11
|
+
# takes floating point value and maximum denominator
|
12
|
+
# returns rational approximation as integer numerator and limited denominator and difference with input value
|
13
|
+
# ported from David Eppstein's algorithm
|
14
|
+
def self.approx(val, maxden=DEFAULT_MAXDEN)
|
15
|
+
sign = 1
|
16
|
+
m11 = 1
|
17
|
+
m22 = 1
|
18
|
+
m12 = 0
|
19
|
+
m21 = 0
|
20
|
+
|
21
|
+
if val < 0.0
|
22
|
+
sign = -1
|
23
|
+
val *= -1.0
|
24
|
+
end
|
25
|
+
|
26
|
+
x = val
|
27
|
+
count = 0
|
28
|
+
|
29
|
+
# loop finding terms until denominator is too big
|
30
|
+
ai = x.floor
|
31
|
+
|
32
|
+
# loop until denominator is too big
|
33
|
+
while m21 * ai + m22 <= maxden
|
34
|
+
# count iterations
|
35
|
+
count += 1
|
36
|
+
|
37
|
+
# limit iterations
|
38
|
+
break if count > 50000000
|
39
|
+
|
40
|
+
t = m11 * ai + m12
|
41
|
+
m12 = m11
|
42
|
+
m11 = t
|
43
|
+
t = m21 * ai + m22
|
44
|
+
m22 = m21
|
45
|
+
m21 = t
|
46
|
+
|
47
|
+
break if x == ai
|
48
|
+
|
49
|
+
x = 1 / (x - ai.to_f)
|
50
|
+
|
51
|
+
break if x > 0x7FFFFFFF
|
52
|
+
|
53
|
+
ai = x.floor
|
54
|
+
end
|
55
|
+
|
56
|
+
numerator = m11 * sign
|
57
|
+
denominator = m21
|
58
|
+
imprecision = val - m11.to_f / m21.to_f
|
59
|
+
|
60
|
+
return numerator, denominator, imprecision
|
61
|
+
end
|
62
|
+
|
63
|
+
# convert float to whole and fractional part
|
64
|
+
def self.modf(number)
|
65
|
+
number.divmod 1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Float
|
70
|
+
def modf
|
71
|
+
self.divmod 1
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_r
|
75
|
+
Rational(*Math.approx(self)[0, 2])
|
76
|
+
end
|
77
|
+
end
|
data/ratapprox.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ratapprox/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ratapprox"
|
8
|
+
spec.version = Ratapprox::VERSION
|
9
|
+
spec.authors = ["Gregory H. Halverson"]
|
10
|
+
spec.email = ["gregory.halverson@gmail.com"]
|
11
|
+
spec.summary = %q{Pure Ruby Rational Approximation with Limited Denominator}
|
12
|
+
spec.description = %q{Improved rational approximation algorithm that overrides the to_r method of Float}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ratapprox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gregory H. Halverson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-03-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.7'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.7'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '10.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '10.0'
|
46
|
+
description: Improved rational approximation algorithm that overrides the to_r method
|
47
|
+
of Float
|
48
|
+
email:
|
49
|
+
- gregory.halverson@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .idea/encodings.xml
|
56
|
+
- .idea/misc.xml
|
57
|
+
- .idea/modules.xml
|
58
|
+
- .idea/ratapprox.iml
|
59
|
+
- .idea/scopes/scope_settings.xml
|
60
|
+
- .idea/vcs.xml
|
61
|
+
- Gemfile
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/ratapprox.rb
|
66
|
+
- lib/ratapprox/version.rb
|
67
|
+
- ratapprox.gemspec
|
68
|
+
homepage: ''
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.28
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Pure Ruby Rational Approximation with Limited Denominator
|
93
|
+
test_files: []
|