# Section 5.2. Basic Operations on Numbers

### 5.1. Representing Numbers in Ruby

If you know any other language, the representation of numbers in Ruby is mostly intuitive. A Fixnum may be signed or unsigned:

`237     # unsigned (positive) number +237    # same as above -237    # negative number`

When numbers are long, we can insert underscores at will (between any two digits). This is purely cosmetic and does not affect the value of the constant. Typically, we would insert them at the same places where accountants might insert commas:

`1048576     # a simple number 1_048_576   # the same value`

It's also possible to represent integers in the most common alternative bases (bases 2, 8, and 16). These are "tagged" with the prefixes 0b, 0, and 0x, respectively.

`0b10010110      # binary 0b1211          # error! 01234           # octal (base 8) 01823           # error! 0xdeadbeef      # hexadecimal (base 16) 0xDEADBEEF      # same 0xdeadpork      # error!`

Floating point numbers have to have a decimal point and may optionally have a signed exponent.

`3.14            # pi to two digits -0.628          # -2*pi over 10, to two digits 6.02e23         # Avogadro's number 6.626068e-34    # Planck's constant`

Certain constants in the Float class help define limits for floating point numbers. These are machine-dependent. Some of the more important ones are as follows:

`Float::MIN          # 2.2250738585072e-308 (on this machine) Float::MAX          # 1.79769313486232e+308 Float::EPSILON      # 2.22044604925031e-16`

The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon