module Numbers

Numeric Extensions

Public Instance Methods

alpha_sum(u,o) click to toggle source
# File lib/numbers.rb, line 93
def alpha_sum(u,o)
  (self**u - self**(o+1)).fdiv(1-self)
end
days_to_months(options = {:s_days => "Tage", :s_months => "Monate", :s_sep => "und"}) click to toggle source
# File lib/numbers.rb, line 139
def days_to_months(options = {:s_days => "Tage", :s_months => "Monate", :s_sep => "und"})
  x = [self/30, self%30]
  "#{x[0]} #{options[:s_months]} #{options[:s_sep]} #{x[1]} #{options[:s_days]}"
end
defla(y=40,f=2) click to toggle source

Deflate number, y = duration of years, f = percentage tested

# File lib/numbers.rb, line 41
def defla(y=40,f=2)
  self.to_f*((1-(f.fdiv(100)))**y)
end
deg2rad() click to toggle source
# File lib/numbers.rb, line 109
def deg2rad
  self * Math::PI / 180
end
even?() click to toggle source
# File lib/numbers.rb, line 128
def even?
  !self.odd?
end
fdiv(d) click to toggle source

> tested

# File lib/numbers.rb, line 83
def fdiv(d)
  self.to_f/d
end
get_closest(nrs = [], lim = :ceil) click to toggle source

Prüfen, welche Zahl aus dem Array am nächsten an der aktuellen Dran ist tested

# File lib/numbers.rb, line 47
def get_closest(nrs = [], lim = :ceil)

  com = {}
  com_a = []
  nrs.each do |n|
    case lim
    when :ceil
      x = ((self+0.001)-n).abs
    when :floor
      x = ((self-0.001)-n).abs
    else
      raise ArgumentError, "lim must be :ceil or :floor"
    end
    com.merge!(x => n)
    com_a << x
  end
  return com[com_a.min]
end
infla(y=40,f=2) click to toggle source

Inflate number, y = duration of years, f = percentage tested

# File lib/numbers.rb, line 35
def infla(y=40,f=2)
  self.to_f*(f.to_q**y)
end
max(ref) click to toggle source

tested

# File lib/numbers.rb, line 72
def max(ref)
  [self.to_f, ref.to_f].max
end
min(ref) click to toggle source

tested

# File lib/numbers.rb, line 67
def min(ref)
  [self.to_f, ref.to_f].min
end
min_max(m1, m2) click to toggle source

Wert zwischen den Grenzen, ansonsten ober-oder unterkante tested

# File lib/numbers.rb, line 78
def min_max(m1, m2)
  self.min(m2).max(m1)
end
nan?() click to toggle source
# File lib/numbers.rb, line 113
def nan?
  self.to_f.nan?
end
odd?() click to toggle source
# File lib/numbers.rb, line 124
def odd?
  self%2 == 0
end
prime_factors(options = {}) click to toggle source

Split number in smallest prime factors

# File lib/numbers.rb, line 146
def prime_factors(options = {})
  options[:nrs] ||= Math.sqrt(self).floor.prime_numbers
  options[:nrs].each_with_index do |n,i|
    if self%n == 0
      return [n, (self/n).prime_factors(:nrs => options[:nrs])].flatten.except(1)
    elsif i == options[:nrs].size-1
      return [self]
    end
  end
end
prime_numbers() click to toggle source

Create list with prime numbers up to 'self'

# File lib/numbers.rb, line 158
def prime_numbers
  s = (0..self).to_a
  s[0] = s[1] = nil
  s.each do |p|
    next unless p
    break if p * p > self
    (p*p).step(self, p) { |m| s[m] = nil }
  end
  s.compact
end
to_de(label=nil) click to toggle source

Convert Number to numeric german style without precision

# File lib/numbers.rb, line 29
def to_de(label=nil)
  self.to_euro(label, :precision => 0)
end
to_euro(label = nil, options = {}) click to toggle source

Convert Number to numeric german style with precision

# File lib/numbers.rb, line 7
def to_euro(label = nil, options = {})
  options[:precision] ||= 2

  options[:precision] = 0 if options[:fix_int] == true && self.is_a?(Integer)

  result = ActionController::Base.helpers.number_with_precision(self, :precision => options[:precision], :separator => ",", :delimiter => ".")

  if options[:pre] == true && self > 0
    result = "+#{result}"
  elsif options[:pre] == true && self < 0
    result = "-#{result}"
  end


  if !label.blank?
    return [result, label].join("&nbsp;").html_safe
  else
    return result
  end
end
to_q() click to toggle source

Finanzmathematik, Zinsen und so

# File lib/numbers.rb, line 89
def to_q
  1+(self/100.0)
end
to_time(options = {}) click to toggle source
# File lib/numbers.rb, line 98
def to_time(options = {})
  values = [ self.to_i/3600, self.to_i / 60 % 60, self.to_i%60 ].map{ |t| t.to_s.rjust(2, '0') }
  if options[:split] == true
    return {:h => values[0].round, :m => values[1].round, :s => values[2].round}
  elsif options[:discard_hour] == true && values[0] == "00"
    return values[1,2].join(':')
  else
    return values.join(':')
  end
end
to_years(options = {:s_years => "Jahre", :s_months => "Monate", :s_sep => "und"}) click to toggle source

Cals

# File lib/numbers.rb, line 134
def to_years(options = {:s_years => "Jahre", :s_months => "Monate", :s_sep => "und"})
  x = [self/12, self%12]
  "#{x[0]} #{options[:s_years]} #{options[:s_sep]} #{x[1]} #{options[:s_months]}"
end