Module: VV::NumericMethods

Included in:
Numeric
Defined in:
lib/vv/numeric_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/vv/numeric_methods.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#imaginary?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/vv/numeric_methods.rb', line 16

def imaginary?
  ! ( self.imaginary == 0 )
end

#readable_number(precision: 3, significant: false) ⇒ Object Also known as: readable



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vv/numeric_methods.rb', line 25

def readable_number precision: 3, significant: false
  message = "Cannot make imaginary number readable"
  fail message if imaginary?

  _int_digits,
  _remaining_digits = self.real_digits.split String.period

  _int_digits = \
  _int_digits.after String.dash, safe: false

  if _int_digits.size > 4
    # TODO: This type of pattern comes up all the time with
    #       strings. Figure out a way of abstracting it.
    offset   = ( _int_digits.size ) % 3
    required = ( _int_digits.size - 1 ) / 3

    index = required * 3 + ( offset % -3 )
    while index > 0
      _int_digits.insert(index, String.comma)
      index -= 3
    end
  end

  response = \
  real.negative? ? String.dash : String.empty_string

  response += _int_digits
  return response unless _remaining_digits
  response += String.period

  last_digit = significant ? -1 : ( precision )
  if significant || precision >= _remaining_digits.size
    dec_portion = _remaining_digits
  else
    rounded = self.real.to_f.round(precision)
    dec_portion = rounded.to_s.split(".")[-1][0..last_digit]
  end

  response + dec_portion
end

#real_digitsObject



20
21
22
23
# File 'lib/vv/numeric_methods.rb', line 20

def real_digits
  return real.to_digits if real.respond_to? :to_digits
  real.to_s
end