module MovieMasher::FloatUtil

comparison of floats

Constants

HUNDRED
NEG_ONE
ONE
TWO
ZERO

Public Class Methods

cmp(float1, float2, digits = 3) click to toggle source

are 2 floats equal

# File lib/util/float_util.rb, line 14
def cmp(float1, float2, digits = 3)
  if digits.zero?
    i1 = float1.to_f.round.to_i
    i2 = float2.to_f.round.to_i
  else
    e = (10**digits).to_f
    i1 = (float1.to_f * e).round.to_i
    i2 = (float2.to_f * e).round.to_i
  end
  (i1 == i2)
end
gtr(big, small, digits = 3) click to toggle source

is one float bigger than another

# File lib/util/float_util.rb, line 27
def gtr(big, small, digits = 3)
  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

is big greater or equal to small

# File lib/util/float_util.rb, line 35
def gtre(big, small, digits = 3)
  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

is one float smaller than another

# File lib/util/float_util.rb, line 43
def less(small, big, digits = 3)
  !gtre(big, small, digits)
end
max(float1, float2, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 47
def max(float1, float2, digits = 3)
  (gtr(float1, float2, digits) ? float1 : float2)
end
min(float1, float2, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 51
def min(float1, float2, digits = 3)
  (gtr(float1, float2, digits) ? float2 : float1)
end
nonzero(float1) click to toggle source
# File lib/util/float_util.rb, line 55
def nonzero(float1)
  float1 && gtr(float1, ZERO)
end
precision(float, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 59
def precision(float, digits = 3)
  divisor = (10**digits.to_i).to_f
  (float.to_f * divisor).round / divisor
end
sort(float1, float2) click to toggle source
# File lib/util/float_util.rb, line 64
def sort(float1, float2)
  if gtr(float1[0], float2[0])
    1
  else
    (cmp(float1[0], float2[0]) ? 0 : -1)
  end
end
string(float, digits = 3) click to toggle source
# File lib/util/float_util.rb, line 72
def string(float, digits = 3)
  precision(float, digits)
end