Class: Numeric

Inherits:
Object show all
Includes:
Multipliers, Random::NumericExtensions
Defined in:
lib/core/facets/boolean.rb,
lib/core/facets/object/dup.rb,
lib/core/facets/kernel/blank.rb,
lib/core/facets/numeric/range.rb,
lib/core/facets/numeric/approx.rb,
lib/core/facets/numeric/length.rb,
lib/core/facets/numeric/delimit.rb,
lib/core/facets/numeric/spacing.rb,
lib/core/facets/numeric/distance.rb,
lib/core/facets/numeric/positive.rb,
lib/core/facets/numeric/round_to.rb,
lib/standard/facets/random.rb,
lib/standard/facets/multipliers.rb

Defined Under Namespace

Modules: Multipliers

Instance Method Summary collapse

Methods included from Multipliers

#atto, #centi, #deci, #deka, #exa, #exbi, #femto, #gibi, #giga, #hecto, #kibi, #kilo, #mebi, #mega, #micro, #milli, #nano, #pebi, #peta, #pico, #tebi, #tera

Methods included from Random::NumericExtensions

#random_delta

Instance Method Details

#approx?(x, n = 0.0000001) ⇒ Boolean

Determines if another number is approximately equal within a given _n_th degree. Defaults to 100ths if the degree is not specified.

Currently defaults to 1/10,000,000 if the degree is not specified. But this may change once a “most commonly useful” factor is determined.

This is the same a #close? but has a different defualt.

Returns:

  • (Boolean)

Author:

  • Gavin Sinclair



13
14
15
# File 'lib/core/facets/numeric/approx.rb', line 13

def approx?(x, n=0.0000001)
  close?(x, n)
end

#blank?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/core/facets/kernel/blank.rb', line 80

def blank?
  false
end

#clone?Boolean

Returns:

  • (Boolean)


78
# File 'lib/core/facets/object/dup.rb', line 78

def clone? ; false ; end

#close?(number, epsilon = 0.01) ⇒ Boolean

Determines if another number is approximately equal within a given epsilon.

This is the same a #approx? but has a different default. In this case it is 1/100th.

Returns:

  • (Boolean)

Author:

  • Gavin Sinclair



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/core/facets/numeric/approx.rb', line 24

def close?(number, epsilon=0.01)
  return(self == number) if epsilon.zero?

  a, b = self.to_f, number.to_f
  if a.zero? or b.zero?
    ## There's no scale, so we can only go on difference.
    (a - b).abs < epsilon
  else
    ## We go by ratio. The ratio of two equal numbers is one, so the ratio
    ## of two practically-equal floats will be very nearly one.
    (a/b - 1).abs < epsilon
  end
end

#delimit(options = {}) ⇒ Object

Returns a string representation of the number e.g.

1000.delimit # => "1,000"
1000000.delimit # => "1,000,000"
(1000000.1234).delimit # => "1,000,000.1234"

Can take a hash of options:

  • delimiter - defaults to “,” but can be any string

  • separator - defaults to “.” but can be any string

    1000.delimit(:delimiter => “_”) # => “1_000” (1000.00).delimit(:delimiter => “.”, :separator => “,”) # => “1.000,00”



13
14
15
16
17
18
19
# File 'lib/core/facets/numeric/delimit.rb', line 13

def delimit(options = {})
  opts = { :delimiter => ',', :separator => '.' }.merge(options)
  digits, decimals = self.to_s.split('.')
  digits = digits.reverse.chars.each_slice(3).map(&:join).join(opts[:delimiter]).reverse
  return digits unless decimals
  [digits, decimals].join(opts[:separator])
end

#distance(other) ⇒ Object

Returns the distance between self an another value. This is the same as #- but it provides an alternative for common naming between variant classes.

4.distance(3)  #=> 1


9
10
11
# File 'lib/core/facets/numeric/distance.rb', line 9

def distance(other)
  self - other
end

#dup!Object

Since Numeric is immutable it cannot be duplicated. For this reason #try_dup returns self.

1.dup!  #=> 1


76
# File 'lib/core/facets/object/dup.rb', line 76

def dup!   ; self  ; end

#dup?Boolean

Returns:

  • (Boolean)


77
# File 'lib/core/facets/object/dup.rb', line 77

def dup?   ; false ; end

#lengthObject

Returns self, useful for polymorphic cases.



5
6
7
# File 'lib/core/facets/numeric/length.rb', line 5

def length
  self
end

#negative?Boolean

Is a number less than zero.

Returns:

  • (Boolean)


11
12
13
# File 'lib/core/facets/numeric/positive.rb', line 11

def negative?
  self < 0
end

#positive?Boolean

Is a number greater than zero.

Returns:

  • (Boolean)


5
6
7
# File 'lib/core/facets/numeric/positive.rb', line 5

def positive?
  self > 0
end

#range(value) ⇒ Object Also known as: plus_or_minus

Create a range from the number plus-or-minus a given value.

Return [Range]



6
7
8
# File 'lib/core/facets/numeric/range.rb', line 6

def range(value)
  Range.new(self - value, self + value)
end

#round_to(*args) ⇒ Object

Conceptually, rounding is expected to apply to floating point numbers. However it can actually be applied to pretty much any Numeric object. For example, one could round an Integer to the nearest kilo.

See Float#round_to.



9
10
11
# File 'lib/core/facets/numeric/round_to.rb', line 9

def round_to(*args)
  to_f.round_to(*args)
end

#spacingObject

Returns the size of the string representation of a numerical value.

   1.spacing   #=> 1
  10.spacing   #=> 2
 100.spacing   #=> 3
-100.spacing   #=> 4
 1.2.spacing   #=> 3

CREDIT: Victor H. Goff III



14
15
16
# File 'lib/core/facets/numeric/spacing.rb', line 14

def spacing
  to_s.length
end

#to_bObject

Provides a boolean interpretation of self. If self == 0 then false else true.

0.to_b    #=> false
1.to_b    #=> true
2.3.to_b  #=> true


64
65
66
# File 'lib/core/facets/boolean.rb', line 64

def to_b
  self == 0 ? false : true
end