Julia – 7 – numeri interi e floating-point – 4

Continuo da qui, copio qui.

Epsilon della macchina
Most real numbers cannot be represented exactly with floating-point numbers, and so for many purposes it is important to know the distance between two adjacent representable floating-point numbers, which is often known as machine epsilon.

Julia provides `eps()`, which gives the distance between 1.0 and the next larger representable floating-point value:

These values are 2.0^-23 and 2.0^-52 as `Float32` and `Float64` values, respectively. The `eps()` function can also take a floating-point value as an argument, and gives the absolute difference between that value and the next representable floating point value. That is, `eps(x)` yields a value of the same type as x such that `x + eps(x)` is the next representable floating-point value larger than `x`:

The distance between two adjacent representable floating-point numbers is not constant, but is smaller for smaller values and larger for larger values. In other words, the representable floating-point numbers are densest in the real number line near zero, and grow sparser exponentially as one moves farther away from zero. By definition, `eps(1.0)` is the same as `eps(Float64)` since 1.0 is a 64-bit floating-point value.

Julia also provides the `nextfloat()` and `prevfloat()` functions which return the next largest or smallest representable floating-point number to the argument respectively:

This example highlights the general principle that the adjacent representable floating-point numbers also have adjacent binary integer representations.

Modi di arrotondamento
If a number doesn’t have an exact floating-point representation, it must be rounded to an appropriate representable value, however, if wanted, the manner in which this rounding is done can be changed according to the rounding modes presented in the IEEE 754 standard.

OOPS! ma ecco un warning: This feature is still experimental, and may give unexpected or incorrect values. E non ho installato l’ultima versione, devo ponderare se…

per intanto il risultato voluto lo ottengo con

The default mode used is always `RoundNearest`, which rounds to the nearest representable value, with ties rounded towards the nearest value with an even least significant bit.

Warning: Rounding is generally only correct for basic arithmetic functions (`+()`, `-()`, `*()`, `/()` and `sqrt()`) and type conversion operations. Many other functions assume the default `RoundNearest` mode is set, and can give erroneous results when operating under other rounding modes.

Background e riferimenti
Floating-point arithmetic entails many subtleties which can be surprising to users who are unfamiliar with the low-level implementation details. However, these subtleties are described in detail in most books on scientific computation, and also in the following references:

Posta un commento o usa questo indirizzo per il trackback.