module MovieMasher::FloatUtil

comparison of floats

Constants

HUNDRED
NEG_ONE
ONE
TWO
ZERO

Public Class Methods

cmp(f1, f2, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 9
def self.cmp(f1, f2, digits = 3) # are 2 floats equal
  if digits.zero?
    i1 = f1.to_f.round.to_i
    i2 = f2.to_f.round.to_i
  else
    e = (10**digits).to_f
    i1 = (f1.to_f * e).round.to_i
    i2 = (f2.to_f * e).round.to_i
  end
  (i1 == i2)
end
gtr(big, small, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 23
def self.gtr(big, small, digits = 3) # is one float bigger than another
  e = (10**digits).to_f
  ibig = (big.to_f * e).round
  ismall = (small.to_f * e).round
  (ibig > ismall)
end
gtre(big, small, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 29
def self.gtre(big, small, digits = 3) # is big greater or equal to small
  e = (10**digits).to_f
  ibig = (big.to_f * e).round
  ismall = (small.to_f * e).round
  (ibig >= ismall)
end
less(small, big, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 20
def self.less(small, big, digits = 3) # is one float smaller than another
  !gtre(big, small, digits)
end
max(a, b, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 35
def self.max(a, b, digits = 3)
  (gtr(a, b, digits) ? a : b)
end
min(a, b, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 38
def self.min(a, b, digits = 3)
  (gtr(a, b, digits) ? b : a)
end
nonzero(a) click to toggle source
# File lib/util/float_util.rb, line 41
def self.nonzero(a)
  a && gtr(a, ZERO)
end
precision(f, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 54
def self.precision(f, digits = 3)
  divisor = (10**digits.to_i).to_f
  (f.to_f * divisor).round / divisor
end
sort(a, b) click to toggle source
# File lib/util/float_util.rb, line 44
def self.sort(a, b)
  if gtr(a[0], b[0])
    1
  else
    (cmp(a[0], b[0]) ? 0 : -1)
  end
end
string(f, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 51
def self.string(f, digits = 3)
  precision f, digits
end