Class: Float

Inherits:
Object show all
Defined in:
lib/nano/float/round_at.rb,
lib/nano/float/round_to.rb,
lib/nano/float/approx%3F.rb,
lib/nano/float/round_off.rb

Instance Method Summary collapse

Instance Method Details

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

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

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/nano/float/approx%3F.rb', line 7

def approx?( x, n=0.01 )
  return(self == x) if n == 0
  self.round_to(n) == x.to_f.round_to(n)
end

#round_at(d) ⇒ Object

Rounds to the given decimal position.

4.555.round_at(0)  #=> 5.0
4.555.round_at(1)  #=> 4.6
4.555.round_at(2)  #=> 4.56
4.555.round_at(3)  #=> 4.555


11
12
13
# File 'lib/nano/float/round_at.rb', line 11

def round_at( d ) #d=0
  (self * (10.0 ** d)).round_off.to_f / (10.0 ** d)
end

#round_to(n) ⇒ Object

Rounds to the nearest _n_th degree.

4.555.round_to(1)     #=> 5.0
4.555.round_to(0.1)   #=> 4.6
4.555.round_to(0.01)  #=> 4.56
4.555.round_to(0)     #=> 4.555


11
12
13
14
# File 'lib/nano/float/round_to.rb', line 11

def round_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).round_off.to_f / (1.0 / n)
end