exact_zero 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 636566ccfbb22ab1fde1f1b0324bfbe39687f581687ff40e28bc21f81f93e6ea
4
+ data.tar.gz: 0ae2acf8e6e217a87c1535bbeae6873c90f39742ae4fab95133ee5b4ee7b1576
5
+ SHA512:
6
+ metadata.gz: 77b753f8c0825c6d313454fde898da643f4b71977a56264baa5a0a10471971c6709ccaeacb5bfb1bf1203eb412784968dfcf758ecdbb7da277b519d0a65897b4
7
+ data.tar.gz: 002f6b7efc28e40ad284c73be0ea2feb0f64987b9eec8fd9a246bddb5eb5e56ab67b7cc0a423359dfa372dba875bc04adb42dbf4486dc53583a0cfc038c3b90d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in exact_zero.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Masahiro Nomoto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # ExactZero
2
+
3
+ This gem provides `Numeric#exact_zero?`, which determine the occurrence of zero division error in advance.
4
+
5
+ There are some ideas of the judgement in the article https://qiita.com/HMMNRST/items/6cf7e7cc853e5d627a72. (Japanese)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'exact_zero'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install exact_zero
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'exact_zero'
27
+
28
+ Integer("0") .exact_zero? #=> true
29
+ Rational("0") .exact_zero? #=> true
30
+ Float("0") .exact_zero? #=> false
31
+ Complex("0") .exact_zero? #=> true
32
+ Complex("0.0i").exact_zero? #=> false
33
+
34
+ require 'bigdecimal'
35
+ BigDecimal("0").exact_zero? #=> false
36
+ ```
37
+
38
+ ### Example 1: avoid ZeroDivisionError
39
+
40
+ ```ruby
41
+ require 'exact_zero'
42
+
43
+ class Numeric
44
+ def reciprocal
45
+ 1.quo(self)
46
+ end
47
+
48
+ def reciprocal_safe
49
+ exact_zero? ? nil : reciprocal
50
+ end
51
+ end
52
+
53
+ num1 = 0.0
54
+ num1.reciprocal #=> Infinity
55
+ num1.reciprocal_safe #=> Infinity
56
+
57
+ num2 = Complex(0, 0/1r)
58
+ num2.reciprocal #=> ZeroDivisionError: divided by 0
59
+ num2.reciprocal_safe #=> nil
60
+ ```
61
+
62
+ ### Example 2: Complex to Real
63
+
64
+ ```ruby
65
+ require 'exact_zero'
66
+
67
+ class Complex
68
+ %w(to_f to_r to_i).each do |name|
69
+ define_method("try_#{name}") do
70
+ imag.exact_zero? ? send(name) : nil
71
+ end
72
+ end
73
+ end
74
+
75
+ num1 = Complex("3+0/1i")
76
+ num1.to_f #=> 3.0
77
+ num1.try_to_f #=> 3.0
78
+
79
+ num2 = Complex("3+0.0i")
80
+ num2.to_f #=> RangeError: can't convert 3+0.0i into Float
81
+ num2.try_to_f #=> nil
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ 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).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hmmnrst/exact_zero.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exact_zero"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "exact_zero"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Masahiro Nomoto"]
9
+ spec.email = ["hmmnrst@users.noreply.github.com"]
10
+
11
+ spec.summary = %q{Provides `Numeric#exact_zero?` to avoid ZeroDivisionError}
12
+ spec.homepage = "https://github.com/hmmnrst/exact_zero"
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/lib/exact_zero.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Numeric
2
+ ##
3
+ # Returns true when +self+ is exactly zero.
4
+ #
5
+ # @example
6
+ # (0/1r).exact_zero? #=> true
7
+ # (-0.0).exact_zero? #=> false
8
+ #
9
+ def exact_zero?
10
+ zero? && 1.0 / (-abs2).to_f > 0
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exact_zero
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masahiro Nomoto
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-25 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - hmmnrst@users.noreply.github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - exact_zero.gemspec
72
+ - lib/exact_zero.rb
73
+ homepage: https://github.com/hmmnrst/exact_zero
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.0.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Provides `Numeric#exact_zero?` to avoid ZeroDivisionError
96
+ test_files: []