Ruby/Number/float

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Absolute value and sign

puts -2.0.abs     # => 2.0: absolute value
puts -2.0<=>0.0   # => -1: use <=> operator to compute sign of a number



ArgumentError: invalid value for Float(): "ure"

Float("99.44% pure")



Check whether a floating-point value is a number at all with Float"s nan?

val = 1.0
val.nan? # => false
val = 0.0/0.0
val.inspect # => "NaN"
val.nan? # => true



Check whether a number is finite or infinite with the finite? and infinite?

0.0.finite? # => true
(-1.0/0.0).finite? # => false
(+1.0/0.0).finite? # => false
0.0.infinite? # => nil
(-1.0/0.0).infinite? # => -1
(+1.0/0.0).infinite? # => 1



Class constants

DIG            Precision of Float (in decimal digits) 
EPSILON        The smallest Float such that 1.0+ EPSILON =1.0 
MANT_DIG       The number of mantissa digits (base RADIX) 
MAX            The largest Float
MAX_10_EXP     The maximum integerx such that 10x is afinite Float
MAX_EXP        The maximum integerx such that FLT_RADIX(x1) is a finite Float
MIN            The smallest Float
MIN_10_EXP     The minimum integerx such that 10x is afinite Float
MIN_EXP        The minimum integerx such that FLT_RADIX(x1) is a finite Float
RADIX          The radix offloating point representations 
ROUNDS         The rounding mode forfloating point operations. Possible values include
                    1 if the mode is indeterminate
                    0 if rounding is toward zero
                    1 if rounding is to nearest representable value
                    2 if rounding is toward+
                    3 if rounding is toward



Constants in Float

puts Float::MAX     # => 1.79769313486232e+308: may be platform dependent
puts Float::MIN     # => 2.2250738585072e-308
puts Float::EPSILON # => 2.22044604925031e-16: difference between adjacent floats



Floating Point Numbers

puts 10 / 3
puts 10.0 / 3.0



Float predicates

ZERO, INF, NAN = 0.0, 1.0/0.0, 0.0/0.0  # Constants for testing
puts ZERO.finite?   # => true: is this number finite?
puts INF.finite?    # => false
puts NAN.finite?    # => false
puts ZERO.infinite? # => nil: is this number infinite? Positive or negative?
puts INF.infinite?  # => 1
puts -INF.infinite? # => -1
puts NAN.infinite?  # => nil
puts ZERO.nan?      # => false: is this number not-a-number?
puts INF.nan?       # => false
puts NAN.nan?       # => true



Float value step

1.5.step(2.0, 0.25) { |x| puts x }



Round a float

class Float
  def roundf(places)
    temp = self.to_s.length
    sprintf("%#{temp}.#{places}f",self).to_f
  end
  def round2
    whole = self.floor
    fraction = self - whole
    if fraction == 0.5
      if (whole %2) == 0
        whole
      else
        whole+1
      end
    else
      self.round
    end
   end
end



Rounding methods

puts 1.1.ceil     # =>  2: ceiling: smallest integer >= its argument
puts -1.1.ceil    # => -1: ceiling: smallest integer >= its argument
puts 1.9.floor    # =>  1: floor: largest integer <= its argument
puts -1.9.floor   # => -2: floor: largest integer <= its argument
puts 1.1.round    # =>  1: round to nearest integer
puts 0.5.round    # =>  1: round toward infinity when halfway between integers
puts -0.5.round   # => -1: or round toward negative infinity
puts 1.1.truncate # =>  1: chop off fractional part: round toward zero
puts -1.1.to_i    # => -1: synonym for truncate



round Rounds to the nearest integer.

def round
    case
        when self > 0.0 then (self+0.5).floor
        when self < 0.0 then return (self?.5).ceil
        else 0
    end
end
1.5.round  
(3.5).round