frac 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ # Gemstuff
2
+ Gemfile.lock
3
+
4
+ # compiled
5
+ lib/frac_ext.so
6
+ tmp
7
+
8
+ # rcov generated
9
+ coverage
10
+
11
+ # rdoc generated
12
+ rdoc
13
+
14
+ # yard generated
15
+ doc
16
+ .yardoc
17
+
18
+ # bundler
19
+ .bundle
20
+
21
+ # jeweler generated
22
+ pkg
23
+
24
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
25
+ #
26
+ # * Create a file at ~/.gitignore
27
+ # * Include files you want ignored
28
+ # * Run: git config --global core.excludesfile ~/.gitignore
29
+ #
30
+ # After doing this, these files will be ignored in all your git projects,
31
+ # saving you from having to 'pollute' every project you touch with them
32
+ #
33
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
34
+
35
+ # For MacOS:
36
+
37
+ .DS_Store
38
+
39
+ # For TextMate
40
+ *.tmproj
41
+ tmtags
42
+
43
+ # For emacs:
44
+ *~
45
+ \#*
46
+ .\#*
47
+
48
+ # For vim:
49
+ *.swp
@@ -0,0 +1,18 @@
1
+ 0.9.6
2
+ ============
3
+ * Added JRuby support - [@dblock](https://github.com/dblock).
4
+
5
+ 0.9.5 (June 18, 2011)
6
+ =====================
7
+ * Added bundler support - [@dblock](https://github.com/dblock).
8
+ * Added `Math::Fraction` with string parsing support - [@dblock](https://github.com/dblock).
9
+ * Improved negative number precision - [@dblock](https://github.com/dblock).
10
+
11
+ 0.9.3 (May 25, 2010)
12
+ =====================
13
+ * Added JavaScript implementation - [@valodzka](https://github.com/valodzka).
14
+
15
+ 0.9.0 (May 24, 2010)
16
+ =====================
17
+
18
+ * Initial public release - [@valodzka](https://github.com/valodzka).
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require 'rake'
13
+
14
+ if RUBY_PLATFORM =~ /java/
15
+ require 'rake/javaextensiontask'
16
+ Rake::JavaExtensionTask.new do |ext|
17
+ ext.name = 'frac_ext'
18
+ end
19
+ else
20
+ require 'rake/extensiontask'
21
+ Rake::ExtensionTask.new do |ext|
22
+ ext.ext_dir = 'ext'
23
+ ext.lib_dir = 'lib'
24
+ ext.name = 'frac_ext'
25
+ end
26
+ end
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/*.rb'
39
+ test.verbose = true
40
+ end
41
+
42
+ task :default => [ :compile, :test ]
43
+
44
+ require 'rdoc/task'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "frac #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
53
+
data/VERSION CHANGED
@@ -1,2 +1,2 @@
1
- 0.9.5
1
+ 0.9.6
2
2
 
@@ -37,9 +37,8 @@
37
37
  # define N2R LONG2NUM
38
38
  #endif
39
39
 
40
- static VALUE find_fracs(VALUE mod, VALUE rv, VALUE dv)
40
+ static VALUE find_fracs(VALUE mod, VALUE rv, VALUE dv)
41
41
  {
42
- VALUE ret;
43
42
  N_TYPE m[2][2], ai, maxden = R2N(rb_Integer(dv));
44
43
  double startx, x = RFLOAT_VALUE(rb_Float(rv));
45
44
  int sign = 1;
@@ -70,7 +69,7 @@ static VALUE find_fracs(VALUE mod, VALUE rv, VALUE dv)
70
69
  if(x==(double)ai) break; // AF: division by zero
71
70
  x = 1/(x - (double) ai);
72
71
  if(x>(double)0x7FFFFFFF) break; // AF: representation failure
73
- }
72
+ }
74
73
 
75
74
  {
76
75
  /* now remaining x is between 0 and 1/ai */
@@ -95,7 +94,7 @@ static VALUE find_fracs(VALUE mod, VALUE rv, VALUE dv)
95
94
  }
96
95
  }
97
96
 
98
- void Init_frac_ext()
97
+ void Init_frac_ext()
99
98
  {
100
99
  rb_define_module_function(rb_mMath, "find_fracs", find_fracs, 2);
101
100
  }
@@ -0,0 +1,73 @@
1
+ package com.github.valodzka.frac;
2
+
3
+ import java.io.IOException;
4
+ import java.lang.IllegalArgumentException;
5
+
6
+ import org.jruby.Ruby;
7
+ import org.jruby.anno.JRubyMethod;
8
+
9
+ public class frac_ext {
10
+
11
+ @JRubyMethod
12
+ public static double[] find_fracs(double rv, long dv)
13
+ {
14
+ long m[][] = new long[2][2];
15
+ long ai;
16
+ long maxden = dv;
17
+ double startx = rv;
18
+ double x = rv;
19
+
20
+ int sign = 1;
21
+
22
+ if (maxden <= 0) {
23
+ throw new IllegalArgumentException("maximum denominator should be > 0");
24
+ }
25
+
26
+ if (x < 0) {
27
+ sign = -1;
28
+ x = -x;
29
+ }
30
+
31
+ startx = x;
32
+
33
+ /* initialize matrix */
34
+ m[0][0] = m[1][1] = 1;
35
+ m[0][1] = m[1][0] = 0;
36
+
37
+ /* loop finding terms until denom gets too big */
38
+ while (m[1][0] * ( ai = (long) x ) + m[1][1] <= maxden) {
39
+ long t;
40
+ t = m[0][0] * ai + m[0][1];
41
+ m[0][1] = m[0][0];
42
+ m[0][0] = t;
43
+ t = m[1][0] * ai + m[1][1];
44
+ m[1][1] = m[1][0];
45
+ m[1][0] = t;
46
+ if(x == (long) ai) break; // AF: division by zero
47
+ x = 1/(x - ai);
48
+ if(x > (double) 0x7FFFFFFF) break; // AF: representation failure
49
+ }
50
+
51
+ {
52
+ /* now remaining x is between 0 and 1/ai */
53
+ /* approx as either 0 or 1/m where m is max that will fit in maxden */
54
+ /* first try zero */
55
+ double num1 = sign*m[0][0];
56
+ double den1 = m[1][0];
57
+ double err1 = startx - (m[0][0] / m[1][0]);
58
+
59
+ /* now try other possibility */
60
+ ai = (maxden - m[1][1]) / m[1][0];
61
+ m[0][0] = m[0][0] * ai + m[0][1];
62
+ m[1][0] = m[1][0] * ai + m[1][1];
63
+
64
+ double num2 = sign*m[0][0];
65
+ double den2 = m[1][0];
66
+ double err2 = startx - (m[0][0] / m[1][0]);
67
+
68
+ return new double[]{ num1, den1, err1, num2, den2, err2 };
69
+ }
70
+
71
+ }
72
+
73
+ };
@@ -1,53 +1,45 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
5
3
 
6
4
  Gem::Specification.new do |s|
7
- s.name = %q{frac}
8
- s.version = "0.9.5"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Pavel Valodzka"]
12
- s.date = %q{2011-06-19}
13
- s.email = %q{pavel@valodzka.name}
14
- s.extensions = ["ext/extconf.rb"]
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- "README.rdoc",
20
- "VERSION",
21
- "ext/extconf.rb",
22
- "ext/frac_ext.c",
23
- "frac.gemspec",
24
- "lib/frac.js",
25
- "lib/frac.rb",
26
- "test/frac_test.html",
27
- "test/frac_test.rb"
28
- ]
29
- s.homepage = %q{https://github.com/valodzka/frac}
30
- s.licenses = ["MIT license"]
31
- s.require_paths = ["lib"]
32
- s.rubygems_version = %q{1.6.2}
33
- s.summary = %q{Find rational approximation to given real number. Based on the theory of continued fractions if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...))) then best approximation is found by truncating this series (with some adjustments in the last term). Note the fraction can be recovered as the first column of the matrix ( a1 1 ) ( a2 1 ) ( a3 1 ) ... ( 1 0 ) ( 1 0 ) ( 1 0 ) Instead of keeping the sequence of continued fraction terms, we just keep the last partial product of these matrices.}
34
-
35
- if s.respond_to? :specification_version then
36
- s.specification_version = 3
37
-
38
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
- s.add_development_dependency(%q<rake>, [">= 0"])
40
- s.add_development_dependency(%q<jeweler>, [">= 0"])
41
- s.add_development_dependency(%q<rcov>, [">= 0"])
42
- else
43
- s.add_dependency(%q<rake>, [">= 0"])
44
- s.add_dependency(%q<jeweler>, [">= 0"])
45
- s.add_dependency(%q<rcov>, [">= 0"])
46
- end
5
+ s.name = "frac"
6
+ s.version = "0.9.6"
7
+
8
+ s.authors = [ "Pavel Valodzka" ]
9
+ s.email = "pavel@valodzka.name"
10
+ s.homepage = "https://github.com/valodzka/frac"
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ if RUBY_PLATFORM =~ /java/
14
+ s.platform = "java"
15
+ s.files << "lib/frac_ext.jar"
47
16
  else
48
- s.add_dependency(%q<rake>, [">= 0"])
49
- s.add_dependency(%q<jeweler>, [">= 0"])
50
- s.add_dependency(%q<rcov>, [">= 0"])
17
+ s.extensions = [ "ext/extconf.rb" ]
51
18
  end
52
- end
53
19
 
20
+ s.licenses = [ "MIT" ]
21
+ s.require_paths = [ "lib" ]
22
+ s.rubygems_version = "1.6.2"
23
+ s.summary = <<-SUMMARY
24
+ Find rational approximation to given real number.
25
+
26
+ Based on the theory of continued fractions
27
+
28
+ if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...)))
29
+
30
+ then best approximation is found by truncating this series
31
+ (with some adjustments in the last term).
32
+ Note the fraction can be recovered as the first column of the matrix
33
+
34
+ ( a1 1 ) ( a2 1 ) ( a3 1 ) ...
35
+ ( 1 0 ) ( 1 0 ) ( 1 0 )
36
+
37
+ Instead of keeping the sequence of continued fraction terms,
38
+ we just keep the last partial product of these matrices.
39
+ SUMMARY
40
+
41
+ s.add_development_dependency("rake", "~> 10.0")
42
+ s.add_development_dependency("rake-compiler")
43
+ s.add_development_dependency("rcov", "~> 0.9")
44
+ s.add_development_dependency("rdoc")
45
+ end
@@ -5,14 +5,28 @@ require File.join(File.dirname(__FILE__), %w{.. lib frac_ext})
5
5
  module Math
6
6
 
7
7
  class << self
8
- private :find_fracs
9
-
8
+
10
9
  def frac(float, maxden)
11
10
  arr = find_fracs(float, maxden)
12
11
  arr[2].abs > arr[5].abs ? Rational(arr[3], arr[4]) : Rational(arr[0], arr[1])
13
12
  end
13
+
14
+ if RUBY_PLATFORM =~ /java/
15
+ require 'jruby'
16
+ private
17
+ def find_fracs(float, maxden)
18
+ begin
19
+ com.github.valodzka.frac.frac_ext.find_fracs Float(float), Integer(maxden)
20
+ rescue Java::JavaLang::IllegalArgumentException => e
21
+ raise ArgumentError, e.message
22
+ end
23
+ end
24
+ else
25
+ private :find_fracs
26
+ end
27
+
14
28
  end
15
-
29
+
16
30
  class Fraction
17
31
 
18
32
  def initialize(float, maxden = 0x100)
@@ -31,13 +45,13 @@ module Math
31
45
  @r = Math.frac(float, maxden)
32
46
  end
33
47
  end
34
-
48
+
35
49
  def to_a
36
50
  i = @r.to_i
37
51
  sign = i >= 0 ? 1 : -1
38
52
  [ i, (@r.numerator - i * @r.denominator) * sign, @r.denominator ]
39
53
  end
40
-
54
+
41
55
  def to_r
42
56
  @r
43
57
  end
@@ -45,7 +59,7 @@ module Math
45
59
  def to_f
46
60
  @r.to_f
47
61
  end
48
-
62
+
49
63
  def to_s
50
64
  n = to_a
51
65
  if n[1] == 0
@@ -54,9 +68,9 @@ module Math
54
68
  "#{n[1]}/#{n[2]}"
55
69
  else
56
70
  "#{n[0]} #{n[1]}/#{n[2]}"
57
- end
58
- end
59
-
71
+ end
72
+ end
73
+
60
74
  end
61
75
  end
62
76
 
Binary file
metadata CHANGED
@@ -1,116 +1,130 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: frac
3
- version: !ruby/object:Gem::Version
4
- hash: 49
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 9
9
- - 5
10
- version: 0.9.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.6
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Pavel Valodzka
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-19 00:00:00 +03:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '10.0'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: jeweler
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '10.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake-compiler
32
+ requirement: !ruby/object:Gem::Requirement
39
33
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
47
38
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
50
47
  name: rcov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ type: :development
51
55
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
53
65
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
61
70
  type: :development
62
- version_requirements: *id003
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
63
78
  description:
64
79
  email: pavel@valodzka.name
65
80
  executables: []
66
-
67
- extensions:
81
+ extensions:
68
82
  - ext/extconf.rb
69
- extra_rdoc_files:
70
- - README.rdoc
71
- files:
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - CHANGELOG.md
87
+ - Gemfile
72
88
  - README.rdoc
89
+ - Rakefile
73
90
  - VERSION
74
91
  - ext/extconf.rb
75
92
  - ext/frac_ext.c
93
+ - ext/frac_ext.java
76
94
  - frac.gemspec
77
95
  - lib/frac.js
78
96
  - lib/frac.rb
97
+ - lib/frac_ext.jar
79
98
  - test/frac_test.html
80
99
  - test/frac_test.rb
81
- has_rdoc: true
82
100
  homepage: https://github.com/valodzka/frac
83
- licenses:
84
- - MIT license
101
+ licenses:
102
+ - MIT
85
103
  post_install_message:
86
104
  rdoc_options: []
87
-
88
- require_paths:
105
+ require_paths:
89
106
  - lib
90
- required_ruby_version: !ruby/object:Gem::Requirement
107
+ required_ruby_version: !ruby/object:Gem::Requirement
91
108
  none: false
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- hash: 3
96
- segments:
97
- - 0
98
- version: "0"
99
- required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
114
  none: false
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- hash: 3
105
- segments:
106
- - 0
107
- version: "0"
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
108
119
  requirements: []
109
-
110
120
  rubyforge_project:
111
- rubygems_version: 1.3.7
121
+ rubygems_version: 1.8.23
112
122
  signing_key:
113
123
  specification_version: 3
114
- summary: Find rational approximation to given real number. Based on the theory of continued fractions if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...))) then best approximation is found by truncating this series (with some adjustments in the last term). Note the fraction can be recovered as the first column of the matrix ( a1 1 ) ( a2 1 ) ( a3 1 ) ... ( 1 0 ) ( 1 0 ) ( 1 0 ) Instead of keeping the sequence of continued fraction terms, we just keep the last partial product of these matrices.
124
+ summary: Find rational approximation to given real number. Based on the theory of
125
+ continued fractions if x = a1 + 1/(a2 + 1/(a3 + 1/(a4 + ...))) then best approximation
126
+ is found by truncating this series (with some adjustments in the last term). Note
127
+ the fraction can be recovered as the first column of the matrix ( a1 1 ) ( a2 1
128
+ ) ( a3 1 ) ... ( 1 0 ) ( 1 0 ) ( 1 0 ) Instead of keeping the sequence of continued
129
+ fraction terms, we just keep the last partial product of these matrices.
115
130
  test_files: []
116
-