Recipe 2.1. Parsing a Number from a String
Problem
Given a string that contains some representation of a number, you want to get the corresponding integer or floating-point value.
Solution
Use
String#to_i to turn a string into an ...
Recipe 2.2. Comparing Floating-Point Numbers
Problem
Floating-point
numbers are not suitable for exact comparison. Often, two numbers that should be equal are actually slightly different. The Ruby interpreter can make seemingly nonse...
Recipe 2.3. Representing Numbers to Arbitrary Precision
Problem
You're doing high-
precision arithmetic, and floating-point numbers are not precise enough.
Solution
A BigDecimal number can represent a real number to an arbitrary nu...
Recipe 2.4. Representing Rational Numbers
Problem
You want to precisely represent a
rational number like 2/3, one that has no finite decimal expansion.
Solution
Use a Rational object; it represents a rational number as an integ...
Recipe 2.5. Generating Random Numbers
Problem
You want to generate pseudorandom numbers, select items from a data structure at random, or repeatedly generate the same "random" numbers for testing purposes.
Solution
Use the
Kernel#rand ...
Recipe 2.6. Converting Between Numeric Bases
Problem
You want to convert numbers from one base to another.
Solution
You can convert specific binary, octal, or hexadecimal numbers to decimal by representing them with the 0b, 0o, or 0x...
Recipe 2.7. Taking Logarithms
Problem
You want to take the logarithm of a number, possibly a huge one.
Solution
Math.log calculates the natural log of a number: that is, the log base e.
Math.log(1) # =...
Recipe 2.8. Finding Mean, Median, and Mode
Problem
You want to find the average of an array of numbers: its mean,
median, or
mode.
Solution
Usually when people speak of the "average" of a set of numbers they're referr...
Recipe 2.9. Converting Between Degrees and Radians
Problem
The trigonometry functions in Ruby's Math library take input in radians (2π radians in a circle). Most real-world applications measure angles in degrees (360 degrees in ...
Recipe 2.10. Multiplying Matrices
Problem
You want to turn arrays of arrays of numbers into mathematical
matrices, and multiply the matrices together.
Solution
You can create
Matrix objects from arrays of arrays, and multiply ...
Recipe 2.11. Solving a System of Linear Equations
Problem
You have a number of linear equations (that is, equations that look like "2x + 10y + 8z = 54"), and you want to figure out the solution: the values of x, y, and z. You have as many...
Recipe 2.12. Using Complex Numbers
Problem
You want to represent
complex ("imaginary") numbers and perform math on them.
Solution
Use the
Complex class, defined in the complex library. All mathematical and trigonometric operat...
Recipe 2.13. Simulating a Subclass of Fixnum
Problem
You want to create a class that acts like a subclass of Fixnum, Float, or one of Ruby's other built-in numeric classes. This wondrous class can be used in arithmetic along with real Int...
Recipe 2.14. Doing Math with Roman Numbers
Problem
You want to convert between Arabic and Roman numbers, or do arithmetic with Roman numbers and get Roman numbers as your result.
Solution
The simplest way to define a Roman class tha...
Recipe 2.15. Generating a Sequence of Numbers
Problem
You want to iterate over a (possibly infinite) sequence of numbers the way you can iterate over an array or a range.
Solution
Write a generator function that yields each number i...
Recipe 2.16. Generating Prime Numbers
Problem
You want to generate a
sequence of
prime numbers, or find all
prime numbers below a certain threshold.
Solution
Instantiate the Prime class to create a prime number generator. C...
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.