Type:
Class

mathn

mathn is a library for changing the way Ruby does math. If you need more precise rounding with multiple division or exponentiation operations, then mathn is the right tool.

Without mathn:

1
3 / 2 => 1 # Integer

With mathn:

1
3 / 2 => 3/2 # Rational

mathn features late rounding and lacks truncation of intermediate results:

Without mathn:

1
20 / 9 * 3 * 14 / 7 * 3 / 2 # => 18

With mathn:

1
20 / 9 * 3 * 14 / 7 * 3 / 2 # => 20

When you require 'mathn', the libraries for Prime, CMath, Matrix and Vector are also loaded.

Copyright

Author: Keiju ISHITSUKA (SHL Japan Inc.)

Document-class: FloatDomainError

Raised when attempting to convert special float values (in particular infinite or NaN) to numerical classes which don't support them.

1
Float::INFINITY.to_r

raises the exception:

1
FloatDomainError: Infinity
real
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.real â self Instance Public methods Returns self.

2025-01-10 15:47:30
div
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.div(numeric) â integer Instance Public methods Uses / to

2025-01-10 15:47:30
divmod
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.divmod(numeric) â array Instance Public methods Returns an array containing

2025-01-10 15:47:30
%
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.modulo(numeric) â real Instance Public methods

2025-01-10 15:47:30
remainder
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.remainder(numeric) â real Instance Public methods

2025-01-10 15:47:30
magnitude
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.magnitude â numeric Instance Public methods Returns the absolute value

2025-01-10 15:47:30
imag
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.imag â 0num.imaginary â 0 Instance Public methods Returns

2025-01-10 15:47:30
numerator
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.numerator â integer Instance Public methods Returns the numerator.

2025-01-10 15:47:30
real?
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.real? â true or false Instance Public methods Returns true

2025-01-10 15:47:30
step
  • References/Ruby on Rails/Ruby/Classes/Numeric

num.step(limit[, step]) {|i| block } â selfnum.step(limit[, step]) â an_enumerator Instance

2025-01-10 15:47:30