Ruby/Number/Number class

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

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

Содержание

floor Returns the largest integer less than or equal.

1.2.floor  
2.0.floor  
(2.2).floor  
(1.0).floor



infinite? Returns nil,1,or +1 depending on whether flt is finite.

(0.0).infinite?  
(?.0/0.0).infinite? 
(+1.0/0.0).infinite?



Math class and module hierarchy

              Object
                |
                +--------------+
                |              |
              Numermic       Matrix
                |
                |
             Integer
                |
                |
    +-----------+----------------------+---------+
    |           |                      |         |
 Fixnum         Bignum                 complex    Rational
(math Module)   (Precision module



modulo(numeric): Synonym for Float#%.

6543.21.modulo(137)  
6543.21.modulo(137.24)



Number Introduction

1000.class                                # => Fixnum
10000000000.class                         # => Bignum
(2**30 - 1).class                         # => Fixnum
(2**30).class                             # => Bignum
small = 1000
big = small ** 5                          # => 1000000000000000
big.class                                 # => Bignum
smaller = big / big                       # => 1
smaller.class                             # => Fixnum
0.01.class                                # => Float
1.0.class                                 # => Float
10000000000.00000000001.class             # => Float



Numbers are classes

2.class # => Fixnum
2.0.class # => Float
2_000_000_000.class # => Bignum



Ruby has a number of classes and modules related to numbers.

Numeric    The base class for numbers
Integer    The basic integer class, and the basis for the Fixnum class
Float      The class for real or floating-point numbers, based on the computer"s native capacity to represent double-precision
Fixnum     The main integer class, based on what the computer can hold in a native machine word, such as 32 bits or 64 bits, minus 1
Bignum     The class of integers outside the range of the basic, native machine word
Math       A module that holds math functions (as methods)
Precision  A module for approximating the precision of real numbers
Rational   A class that represents fractional numbers
Complex    A class that represents complex numbers, which extend real numbers with imaginary numbers (x + iy)
Matrix     A class for creating mathematical matrixes