Digital type in Ruby

xiaoxiao2021-03-06  63

3.1.1 integer

Ruby supports two digital types of integer and floating-point.

Integer can be any length (maximum value is determined by the memory size of your machine). Internally by two classes to represent FixNum and Bignum, the number range of FixNum is smaller than Bignum, and the size of the size exceeds the FixNum range is represented by Bignum. If the result is out of the range after the two FixNum calculations; the opposite, if The two Bignum operation results are small, and will be converted to FixNum. Ruby is automatically converted between the two, transparent to the user.

Num = 10

5.Times Do

Print Num.class, "", NUM, "/ N"

Num * = NUM

end

#result

FIXNUM 10

FIXNUM 100

FIXNUM 10000

FIXNUM 100000000

Bignum 10000000000000000

FixNum has an immediate value (IMMEDIATE VALUE), that is, when the parameter is passed, it is the value passed, not the reference delivery. Assigning statements do not establish a reference relationship between variables and objects. Bignum is not this, not an immediate value, belongs to the reference delivery.

Some methods of these two classes:

A. Arithmetic operation (additional and subtraction and elimination must not say):

Irb (main): 053: 0> 5/2 # integer

=> 2

IRB (Main): 054: 0> 5% 2 # Type, ie

=> 1

IRB (main): 055: 0> 2 ** 4 # 2 4 power

=> 16

B. Bit operation:

|

Bits and

&

Bit or

^

Dissemination or

>>

Right movement

<<

Left shift

1 binary: 00000001

2 binary: 00000010

Irb (main): 056: 0> 1 & 2

=> 0

Irb (main): 057: 0> 1 | 2

=> 3

Irb (main): 058: 0> 1 ^ 2

=> 3

Irb (main): 059: 0> 1 << 1

=> 2

Irb (main): 060: 0> 2 >> 1

=> 1

Irb (main): 061: 0>

C. <=> Operator

Returns one of the three values ​​of -1, 0, 1 according to the result of the value of the value of the value between the two sides:

Irb (main): 004: 0> 5 <=> 9

=> -1

Irb (main): 005: 0> 5 <=> 5

=> 0

Irb (main): 006: 0> 5 <=> 2

=> 1

D. Operation

For example, F is an integer, then f [n] returns the value of the nth bit of the b binary format of this F, 0 or 1.

E. Size: Returns the number of bytes occupied by this integer

Irb (main): 007: 0> 1.size

=> 4

IRB (Main): 008: 0> 100000000.Size

=> 4

Irb (main): 009: 0> 1000000000000000000000000000.Size

=> 12

F. TO_F: Returns the floating point value of this integer, in fact, add a ".0". TO_S: Returns the string of this integer.

Irb (main): 010: 0> 102.to_f

=> 102.0

Irb (main): 011: 0> 102.to_S

=> "102"

Irb (main): 012: 0>

Similarly, you can also use a credit identifier in front of the numbers, such as 0 means an octal, 0x represents hexadecimal, 0b represents binary, etc. Moreover, if there is a downline in a integer number, this underscore will be ignored. (But the underscore cannot be at the beginning or end)

Irb (main): 061: 0> 1_2 * 2

=> 24

Irb (main): 062: 0>

Class ITEger

Class Integer is the parent class of FixNum and Bignum, there are some special methods:

CHR (Integer): Returns the ASCII value represented by this number.

Downto (Integer): Receive a Block to perform block from a large to a small loop.

Anint.next: Returns the next number of Anint (ie, Anint 1).

Anint.Step (EndNum, Step): loops from anint to EndNum, step size is STEP, not 1.

INT.TIMES {| I | Block} Cycle INT, each execution of statements in the block.

Upto: Similar to Downto.

Irb (main): 014: 0> 1.Next

=> 2

IRB (Main): 015: 0> 65.chr

=> "A"

IRB (Main): 016: 0> 5.Downto (2) {| i | PUTS I}

5

4

3

2

=> 5

Irb (main): 017: 0> 2.upto (5) {| i | PUTS I}

2

3

4

5

=> 2

IRB (Main): 018: 0> 3.Times {| i | PUTS I}

0

1

2

=> 3

IRB (Main): 019: 0>

3.1.2 floating point

The number of floating points in Ruby has a FLOAT class. In addition to arithmetic addition and subtraction, there are some special methods. The following is a message recipient by anfloat.

CEIL: Returns the smallest integer than ANFLOAT.

FLOOR: Returns the maximum integer than anfloat.

Finite?: If Anfloat is a floating point that complies with the IEEE format, return TRUE. (This method is ended in question mark).

INFINITE ?: Infinity, whether it is an infinite, limited floating point, is endless, return -1, nil, 1.

NaN?: Returns true if Anfloat is not a number of IEEE standards.

Round: Four of Anfloat into an integer.

TO_I: Returns an integer of the number of numbers behind the decimal point.

IRB (Main): 026: 0> (-2.9234) .ceil

=> -2

IRB (Main): 027: 0> (2.9234) .cers

=> 3

Irb (main): 028: 0> (2.9234) .floor

=> 2

IRB (Main): 029: 0> (-2.9234) .floor

=> -3irb (main): 030: 0> (-2.9234) .Round

=> -3

Irb (main): 031: 0> (2.9234) .Round

=> 3

IRB (Main): 032: 0> (2.9234) .to_i

=> 2

IRB (Main): 033: 0> (-2.9234) .to_i

=> -2

Irb (main): 034: 0> a = -1.0

=> -1.0

Irb (main): 035: 0> a.nan?

=> false

Irb (main): 040: 0> (0.0) .infinite?

=> nil

IRB (Main): 041: 0> (-1.0 / 0.0) .Infinite?

=> -1

IRB (Main): 042: 0> ( 1.0 / 0.0) .infinite?

=> 1

Irb (main): 043: 0>

Note, this article is not updated. Copyright (C) <2004>

This document is free document, released according to RDL (Ruby-CN Document License) V1.0 license, you can non-profit (reproduced) on the network, but please retain all information for original documents (including this copyright statement) Please read the RDL license document carefully. The latest version of the license can be obtained at http://www.ruby-cn.org/rdl.txt.

转载请注明原文地址:https://www.9cbs.com/read-86680.html

New Post(0)