mathn 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +16 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/mathn/complex/complex.c +7 -0
- data/ext/mathn/complex/extconf.rb +4 -0
- data/ext/mathn/rational/extconf.rb +4 -0
- data/ext/mathn/rational/rational.c +7 -0
- data/lib/mathn.rb +170 -0
- data/mathn.gemspec +23 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 818cb0283d813753a2e05a18e40578392a029844
|
4
|
+
data.tar.gz: 4f20b1e0d3244f4f8b3c85b98d95fffb969f55b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a068c15a92ce14c3213a2a2cfcb76f4c70fc9894f7095079c00c3fb304b9ea9d3637ec2ef25747f20db98c85a26b375da4e6ba2f995672e992ae95daa6e0cee1
|
7
|
+
data.tar.gz: 7e5873f7aff9454fbf2d98f565f13d9186b4615768f08fb30e8abbd6a9fbc65638ccd00fc3e9bff443ae48ce5c49c0b1cc1c2bbc5812a68192811b66fecf62f9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Mathn
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mathn`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mathn'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mathn
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hsbt/mathn.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test" << "test/lib"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList['test/**/test_*.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
ENV['RUBYOPT'] = "-w"
|
11
|
+
|
12
|
+
require 'rake/extensiontask'
|
13
|
+
Rake::ExtensionTask.new("mathn/complex")
|
14
|
+
Rake::ExtensionTask.new("mathn/rational")
|
15
|
+
|
16
|
+
task :default => [:compile, :test]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mathn"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/mathn.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
#--
|
3
|
+
# $Release Version: 0.5 $
|
4
|
+
# $Revision: 1.1.1.1.4.1 $
|
5
|
+
|
6
|
+
##
|
7
|
+
# = mathn
|
8
|
+
#
|
9
|
+
# mathn serves to make mathematical operations more precise in Ruby
|
10
|
+
# and to integrate other mathematical standard libraries.
|
11
|
+
#
|
12
|
+
# Without mathn:
|
13
|
+
#
|
14
|
+
# 3 / 2 => 1 # Integer
|
15
|
+
#
|
16
|
+
# With mathn:
|
17
|
+
#
|
18
|
+
# 3 / 2 => 3/2 # Rational
|
19
|
+
#
|
20
|
+
# mathn keeps value in exact terms.
|
21
|
+
#
|
22
|
+
# Without mathn:
|
23
|
+
#
|
24
|
+
# 20 / 9 * 3 * 14 / 7 * 3 / 2 # => 18
|
25
|
+
#
|
26
|
+
# With mathn:
|
27
|
+
#
|
28
|
+
# 20 / 9 * 3 * 14 / 7 * 3 / 2 # => 20
|
29
|
+
#
|
30
|
+
#
|
31
|
+
# When you require 'mathn', the libraries for Prime, CMath, Matrix and Vector
|
32
|
+
# are also loaded.
|
33
|
+
#
|
34
|
+
# == Copyright
|
35
|
+
#
|
36
|
+
# Author: Keiju ISHITSUKA (SHL Japan Inc.)
|
37
|
+
#--
|
38
|
+
# class Numeric follows to make this documentation findable in a reasonable
|
39
|
+
# location
|
40
|
+
|
41
|
+
warn('lib/mathn.rb is deprecated') if $VERBOSE
|
42
|
+
|
43
|
+
class Numeric; end
|
44
|
+
|
45
|
+
require "cmath.rb"
|
46
|
+
require "matrix.rb"
|
47
|
+
require "prime.rb"
|
48
|
+
|
49
|
+
require "mathn/rational"
|
50
|
+
require "mathn/complex"
|
51
|
+
|
52
|
+
unless defined?(Math.exp!)
|
53
|
+
Object.instance_eval{remove_const :Math}
|
54
|
+
Math = CMath # :nodoc:
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# When mathn is required, Integer's division is enhanced to
|
59
|
+
# return more precise values from mathematical expressions.
|
60
|
+
#
|
61
|
+
# 2/3*3 # => 0
|
62
|
+
# require 'mathn'
|
63
|
+
# 2/3*3 # => 2
|
64
|
+
#
|
65
|
+
# (2**72) / ((2**70) * 3) # => 4/3
|
66
|
+
|
67
|
+
class Integer
|
68
|
+
remove_method :/
|
69
|
+
|
70
|
+
##
|
71
|
+
# +/+ defines the Rational division for Bignum.
|
72
|
+
#
|
73
|
+
# (2**72) / ((2**70) * 3) # => 4/3
|
74
|
+
|
75
|
+
alias / quo
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# When mathn is required, the Math module changes as follows:
|
80
|
+
#
|
81
|
+
# Standard Math module behaviour:
|
82
|
+
# Math.sqrt(4/9) # => 0.0
|
83
|
+
# Math.sqrt(4.0/9.0) # => 0.666666666666667
|
84
|
+
# Math.sqrt(- 4/9) # => Errno::EDOM: Numerical argument out of domain - sqrt
|
85
|
+
#
|
86
|
+
# After require 'mathn', this is changed to:
|
87
|
+
#
|
88
|
+
# require 'mathn'
|
89
|
+
# Math.sqrt(4/9) # => 2/3
|
90
|
+
# Math.sqrt(4.0/9.0) # => 0.666666666666667
|
91
|
+
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
|
92
|
+
|
93
|
+
module Math
|
94
|
+
remove_method(:sqrt)
|
95
|
+
|
96
|
+
##
|
97
|
+
# Computes the square root of +a+. It makes use of Complex and
|
98
|
+
# Rational to have no rounding errors if possible.
|
99
|
+
#
|
100
|
+
# Math.sqrt(4/9) # => 2/3
|
101
|
+
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
|
102
|
+
# Math.sqrt(4.0/9.0) # => 0.666666666666667
|
103
|
+
|
104
|
+
def sqrt(a)
|
105
|
+
if a.kind_of?(Complex)
|
106
|
+
sqrt!(a)
|
107
|
+
elsif a.respond_to?(:nan?) and a.nan?
|
108
|
+
a
|
109
|
+
elsif a >= 0
|
110
|
+
rsqrt(a)
|
111
|
+
else
|
112
|
+
Complex(0,rsqrt(-a))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
##
|
117
|
+
# Compute square root of a non negative number. This method is
|
118
|
+
# internally used by +Math.sqrt+.
|
119
|
+
|
120
|
+
def rsqrt(a) # :nodoc:
|
121
|
+
if a.kind_of?(Float)
|
122
|
+
sqrt!(a)
|
123
|
+
elsif a.kind_of?(Rational)
|
124
|
+
rsqrt(a.numerator)/rsqrt(a.denominator)
|
125
|
+
else
|
126
|
+
src = a
|
127
|
+
max = 2 ** 32
|
128
|
+
byte_a = [src & 0xffffffff]
|
129
|
+
# ruby's bug
|
130
|
+
while (src >= max) and (src >>= 32)
|
131
|
+
byte_a.unshift src & 0xffffffff
|
132
|
+
end
|
133
|
+
|
134
|
+
answer = 0
|
135
|
+
main = 0
|
136
|
+
side = 0
|
137
|
+
for elm in byte_a
|
138
|
+
main = (main << 32) + elm
|
139
|
+
side <<= 16
|
140
|
+
if answer != 0
|
141
|
+
if main * 4 < side * side
|
142
|
+
applo = main.div(side)
|
143
|
+
else
|
144
|
+
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
|
145
|
+
end
|
146
|
+
else
|
147
|
+
applo = sqrt!(main).to_i + 1
|
148
|
+
end
|
149
|
+
|
150
|
+
while (x = (side + applo) * applo) > main
|
151
|
+
applo -= 1
|
152
|
+
end
|
153
|
+
main -= x
|
154
|
+
answer = (answer << 16) + applo
|
155
|
+
side += applo * 2
|
156
|
+
end
|
157
|
+
if main == 0
|
158
|
+
answer
|
159
|
+
else
|
160
|
+
sqrt!(a)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class << self
|
166
|
+
remove_method(:sqrt)
|
167
|
+
end
|
168
|
+
module_function :sqrt
|
169
|
+
module_function :rsqrt
|
170
|
+
end
|
data/mathn.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = "mathn"
|
4
|
+
spec.version = "0.0.1"
|
5
|
+
spec.authors = ["Keiju ISHITSUKA"]
|
6
|
+
spec.email = [nil]
|
7
|
+
|
8
|
+
spec.summary = "Deprecated library that extends math operations."
|
9
|
+
spec.description = "Deprecated library that extends math operations."
|
10
|
+
spec.homepage = "https://github.com/ruby/mathn"
|
11
|
+
spec.license = "BSD-2-Clause"
|
12
|
+
|
13
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
14
|
+
f.match(%r{^(test|spec|features)/})
|
15
|
+
end
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rake-compiler"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mathn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keiju ISHITSUKA
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-11 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Deprecated library that extends math operations.
|
56
|
+
email:
|
57
|
+
-
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- ext/mathn/complex/complex.c
|
71
|
+
- ext/mathn/complex/extconf.rb
|
72
|
+
- ext/mathn/rational/extconf.rb
|
73
|
+
- ext/mathn/rational/rational.c
|
74
|
+
- lib/mathn.rb
|
75
|
+
- mathn.gemspec
|
76
|
+
homepage: https://github.com/ruby/mathn
|
77
|
+
licenses:
|
78
|
+
- BSD-2-Clause
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.12
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Deprecated library that extends math operations.
|
100
|
+
test_files: []
|