Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/eymiha/math/rectify.rb,
lib/eymiha/math/fraction.rb,
lib/eymiha/math/approximately_equals.rb

Overview

Behavor added to Numeric by eymiha_math.

Constant Summary collapse

@@epsilon =

The initial default distance from a Numeric below which it is considered to be approximately equal to a another Numeric.

0.0000000001

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.epsilonObject

The default distance from a Numeric below which it is considered to be approximately equal to a another Numeric.



21
22
23
# File 'lib/eymiha/math/approximately_equals.rb', line 21

def Numeric.epsilon
  @@epsilon
end

.epsilon=(epsilon) ⇒ Object

Sets the default distance for approximately equals comparisons.



26
27
28
# File 'lib/eymiha/math/approximately_equals.rb', line 26

def Numeric.epsilon=(epsilon)
  @@epsilon = epsilon.abs
end

Instance Method Details

#approximately_equals?(value, epsilon = @@epsilon) ⇒ Boolean Also known as: =~

returns true if the difference between the instance and the given Numeric is less than the given distance.

Returns:

  • (Boolean)


32
33
34
# File 'lib/eymiha/math/approximately_equals.rb', line 32

def approximately_equals?(value,epsilon=@@epsilon)
  (self - value).abs < epsilon
end

#at_least(low = 0) ⇒ Object

Constrains a value to a lower limit.



42
43
44
# File 'lib/eymiha/math/rectify.rb', line 42

def at_least(low=0)
  self < low ? low : self
end

#at_most(high = 0) ⇒ Object

Constrains a value to an upper limit.



37
38
39
# File 'lib/eymiha/math/rectify.rb', line 37

def at_most(high=0)
  self > high ? high : self
end

#cut_rectify(high = 1, low = 0) ⇒ Object

Treats the low and high as limits, returning low or high if the instance is respectively below or above these values, or the instance if between the two.



31
32
33
34
# File 'lib/eymiha/math/rectify.rb', line 31

def cut_rectify(high=1,low=0)
  low,high = high,low if low > high
  (self < low)? low : (self > high)? high : self
end

#round_to_nearest(denominator = 1) ⇒ Object

Returns a numeric value such that any remainder is evenly divided by the given denominator.



27
28
29
# File 'lib/eymiha/math/fraction.rb', line 27

def round_to_nearest(denominator=1)
  ((self*denominator).round)/(1.0*denominator)
end

#with_fraction(denominator = 1, separator = '-', show_zero = false) ⇒ Object

Renders a numeric value as a whole number and a fraction with a given denominator, separated with a given separator, and optionally showing fractions with a numerator of zero.



16
17
18
19
20
21
22
23
# File 'lib/eymiha/math/fraction.rb', line 16

def with_fraction(denominator=1,separator='-',show_zero=false)
  sign = self <=> 0
  unsigned = self*sign
  whole = (((unsigned*denominator).round)/denominator).floor
  numerator = ((unsigned-whole)*denominator).round
  "#{sign*whole}#{(numerator != 0)||show_zero ?
      "#{separator}#{numerator}/#{denominator}" : ""}"
end

#wrap_rectify(high = 1, low = 0) ⇒ Object

Treats the interval between low and high as a cycle, returning the result of wrapping the instance around this interval.



18
19
20
21
22
23
24
25
26
# File 'lib/eymiha/math/rectify.rb', line 18

def wrap_rectify(high=1,low=0)
  if (high != low)
    low,high = high,low if low > high
    diff = high-low
    self-diff*((self-low)/diff).floor
  else
    low
  end
end