Mathematics with Python and Ruby/Real numbers in Ruby

=Decimal representation=

Since the apparition of the digits, numbers are in some way more concrete than were the fractions: Seeing $$\frac{6}{5}$$ one has the impression that there are two numbers instead of one, whereas 1.2 is a single number at first sight.

Decimal numbers
A number is decimal when it has a finite number of digits. Then the real numbers that are not decimal have an infinite decimal expansion. Such a number can be constructed on purpose like the Liouville numbers.

In Ruby, certain decimal numbers have an infinite representation yet: This apparent paradox is due to the fact that the internal representation of numbers in Ruby is binary and even such a simple number as 1.2 has an infinite binary expansion.

Fractions
A fraction is characterized by the fact that its (finite or infinite) expansion is eventually periodic. Examples:

Irrational numbers
The first numbers known as not being fractions were the square roots and the famous Golden mean. Here are some of them:

Two famous transcendental numbers are e and &pi; ; they are properties of the Math object:

Champernowne's constant can be built from a string object:

At first, c is a string, and Ruby knows it because it is written inside a pair of quotes. It represents already the decimal expansion of the constant, which begins with a zero followed by the decimal period. Then the object (1..40) (a list of integers) is created on the fly, and its collect method is called upon, with a small Ruby block, which has only one variable called n and only one instruction, asking to add the string representation of n to c. As c is a string, + denotes the string addition, which is concatenation. At the end, c is converted, either in a float or in a fraction.

=Functions=

Operations
The four operations are denoted by the classical +, -, * and /. As soon as one of the operand includes a period, it is recognized as a real number, and the float operation is applied. Euclidian's division is available too, which allows Ruby to compute the principal value of an angle (in radians):

The minus sign can also be unary, in which case it represents the negation of the following number. To add a number h to an other number x, one can, instead of writing x=x+h, write x+=h which is shorter.

To get an integer approximation to a real number, floor, ceil and to_i can be used. To get the absolute value of the number x, use x.abs. And its square root can be obtained with any of the following methods:

Logarithms
To compute the natural logarithm, decimal logarithm, and the arguments of the hyperbolic circular functions, one writes

Hyperbolic functions
The following script computes and displays the exponential and hyperbolic circular functions of 2:

Circular functions
To get the cosine, sine and tangent of an angle which measures one radian:

To get the angle from one of the above numbers, one just has to add an extra a before the name of the function:

To know an angle from the sides of a right triangle, on can use Math.atan(y/x) or Math.atan2(x,y). And to know the third side, one can use Math.hypot(x,y). For example, if one knows that the sides of a right triangle measure 5' and 12', one can know the angles and hypotenuse with this script: