Skip to content
Merged

Docs #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .document
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BSDL
COPYING
LEGAL
README.md
lib/
2 changes: 2 additions & 0 deletions .rdoc_options
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
main_page: README.md
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ using Math::N
20 / 9 * 3 * 14 / 7 * 3 / 2 # => 20
```

## Global Behavioral changes
## Global Behavioral Changes

While older version of 'mathn', just by required, caused changes to
the behavior (and even the types) of operations on classes like
Integer, newer `mathn` introduces the refinements `Math::N`, and you
`Integer`, newer `mathn` introduces the refinements `Math::N`, and you
have to enable it.

Before ruby 2.5, `mathn` was part of the ruby standard library. It was
was [deprecated in ruby 2.2.0](https://github.com/ruby/ruby/blob/v2_2_0/NEWS#stdlib-compatibility-issues-excluding-feature-bug-fixes),
and [removed from ruby 2.5.0](https://github.com/ruby/ruby/blob/ruby_2_5/NEWS#stdlib-compatibility-issues-excluding-feature-bug-fixes).
In order to use the library with a current version of ruby,
you must install it as a gem.
Before ruby 2.5, `mathn` was part of the ruby standard library. It was
[deprecated in ruby 2.2.0], and [removed from ruby 2.5.0]. In order to
use the library with a current version of ruby, you must install it as a
gem.

[deprecated in ruby 2.2.0]: https://github.com/ruby/ruby/blob/v2_2_0/NEWS#stdlib-compatibility-issues-excluding-feature-bug-fixes
[removed from ruby 2.5.0]: https://github.com/ruby/ruby/blob/ruby_2_5/NEWS#stdlib-compatibility-issues-excluding-feature-bug-fixes

## Installation

Expand All @@ -57,11 +59,15 @@ gem 'mathn'

And then execute:

$ bundle
```console
$ bundle
```

Or install it yourself as:

$ gem install mathn
```console
$ gem install mathn
```

## Usage

Expand Down
45 changes: 28 additions & 17 deletions lib/mathn.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# frozen_string_literal: true

# The built-in module for mathematical functions
module Math end if false # for RDoc

require "cmath"
require_relative "mathn/complex"
require_relative "mathn/rational"

##
# = mathn
#
Expand Down Expand Up @@ -34,26 +41,27 @@
#
# Author: Keiju ISHITSUKA (SHL Japan Inc.)

require "cmath"
require_relative "mathn/complex"
require_relative "mathn/rational"

module Math::N
# The version string
VERSION = "0.2.0"

refine ::Numeric do
##
# Returns the canonicalized result.
def canonicalize; itself; end if false # for RDoc
alias canonicalize itself
end

using self # for canonicalize methods

# :stopdoc:
def +(other) super.canonicalize end
def -(other) super.canonicalize end
def *(other) super.canonicalize end
def /(other) super.canonicalize end
def quo(other) super.canonicalize end
def **(other) super.canonicalize end
# :startdoc:

# Transplant per methods.
canon = public_instance_methods(false).map do |n, h|
Expand All @@ -65,11 +73,9 @@ def **(other) super.canonicalize end
end
end

##
# Enhance Integer's division to return more precise values from
# Integer's division is enhanced to return more precise values from
# mathematical expressions.
refine ::Integer do

class ::Integer
##
# +/+ defines the Rational division for Integer.
#
Expand All @@ -80,28 +86,33 @@ def **(other) super.canonicalize end
# using Math::N
# 2/3*3 # => 2
# (2**72) / ((2**70) * 3) # => 4/3
def quo(other) super.canonicalize end
alias / quo
end if false # for RDoc

refine ::Integer do
alias / quo
end

math = refine ::Math do
module_function

##
# Computes the square root of +a+. It makes use of Complex and
# Rational to have no rounding errors if possible.
# Computes the square root of +a+. It makes use of +Complex+ and
# +Rational+ to have no rounding errors if possible.
#
# Standard Math module behaviour:
# Math.sqrt(4/9) # => 0.0
# Math.sqrt(4.0/9.0) # => 0.6666666666666666
# Math.sqrt(- 4/9) # => Errno::EDOM: Numerical argument out of domain - sqrt
# Math.sqrt(-4/9) # => Errno::EDOM: Numerical argument out of domain - sqrt
#
# When using 'Math::N', this is changed to:
# When using +Math::N+, this is changed to:
#
# require 'mathn'
# using Math::N
# Math.sqrt(4/9) # => 2/3
# Math.sqrt(4.0/9.0) # => 0.666666666666666
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
# Math.sqrt(-4/9) # => Complex(0, 2/3)

def sqrt(a)
return super unless a.respond_to?(:negative?)
Expand All @@ -122,15 +133,15 @@ def sqrt(a)
end

##
# Computes the cubic root of +a+. It makes use of Complex and
# Rational to have no rounding errors if possible.
# Computes the cubic root of +a+. It makes use of +Complex+ and
# +Rational+ to have no rounding errors if possible.
#
# Standard Math module behaviour:
# Math.cbrt(8/27) # => 0.0
# Math.cbrt(8.0/27.0) # => 0.666666666666666
# Math.cbrt(- 8/27) # => -1.0
# Math.cbrt(-8/27) # => -1.0
#
# When using 'Math::N', this is changed to:
# When using +Math::N+, this is changed to:
#
# require 'mathn'
# using Math::N
Expand Down
4 changes: 3 additions & 1 deletion lib/mathn/complex.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

# mathn/complex
;# mathn/complex
module Math::N
refine ::Complex do
# Returns the canonicalized real number if the imaginary part is
# zero. Otherwise returns +self+.
def canonicalize
if imag.zero?
real.canonicalize
Expand Down
4 changes: 3 additions & 1 deletion lib/mathn/rational.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

# mathn/rational
;# mathn/rational
module Math::N
refine ::Rational do
# Returns the canonicalized numerator if the denominator is one.
# Otherwise returns +self+.
def canonicalize
if denominator == 1
numerator.canonicalize
Expand Down