Tuesday, 22 May 2012

How to determine machine epsilon


Where accepted libraries do not accommodate precomputed ethics (as float.h does with FLT_EPSILON, DBL_EPSILON and LDBL_EPSILON for C and C++), the best way to actuate apparatus epsilon is to accredit to the table, above, and use the adapted pow formula. Computing apparatus epsilon is generally accustomed as a arbiter exercise. The afterward examples compute apparatus epsilon in the faculty of the agreement of the amphibian point numbers at 1 rather than in the faculty of the assemblage roundoff.

Note that after-effects depend on the accurate floating-point architecture used, such as float, double, continued double, or agnate as accurate by the programming language, the compiler, and the runtime library for the absolute platform.

Some formats accurate by the processor ability be not accurate by the called compiler and operating system. Other formats ability be emulated by the runtime library, including arbitrary-precision addition accessible in some languages and libraries.

In a austere faculty the appellation apparatus epsilon agency the 1+eps accurateness anon accurate by the processor (or coprocessor), not some 1+eps accurateness accurate by a specific compiler for a specific operating system, unless it's accepted to use the best format.

A atomic archetype is the apparatus epsilon for accumulation addition on processors after amphibian point formats; it is 1, because 1+1=2 is the aboriginal accumulation greater than 1.

IEEE 754 floating-point formats monotonically access over absolute ethics and monotonically abatement over abrogating values. They aswell accept the acreage that area f(x) is the reinterpretation of x from an bearding or twos-complement accumulation architecture to a floating-point architecture of the aforementioned width, and 0 < |f(x)| < ∞, |f(x+1) − f(x)| ≥ |f(x) − f(x−1)|. In languages that acquiesce blazon amusing and consistently use IEEE 754-1985, we can accomplishment this to compute a apparatus epsilon in connected time. For example, in C:

typedef abutment {

continued continued i64;

bifold d64;

} dbl_64;

double machine_eps (double value)

{

dbl_64 s;

s.d64 = value;

s.i64++;

acknowledgment s.d64 - value;

}

This will accord a aftereffect of the aforementioned assurance as value. If a absolute aftereffect is consistently desired, the acknowledgment account of machine_eps can be replaced with:

acknowledgment (s.i64 < 0 ? amount - s.d64 : s.d64 - value);

64-bit doubles accord 2.220446e-16, which is 2-52 as expected.

editApproximation application C

The afterward C affairs does not in fact actuate the apparatus epsilon; rather, it determines a amount aural a agency of two (one adjustment of magnitude) of the accurate apparatus epsilon, application a beeline search.

#include

int main( int argc, burn **argv )

{

float machEps = 1.0f;

printf( "current Epsilon, 1 + accepted Epsilon\n" );

do {

printf( "%G\t%.20f\n", machEps, (1.0f + machEps) );

machEps /= 2.0f;

// If next epsilon yields 1, afresh break, because current

// epsilon is the apparatus epsilon.

}

while ((float)(1.0 + (machEps/2.0)) != 1.0);

printf( "\nCalculated Apparatus epsilon: %G\n", machEps );

acknowledgment 0;

}

Abridged Output

$ cc machine_epsilon.c; ./a.out

accepted Epsilon, 1 + accepted Epsilon

1 2.00000000000000000000

0.5 1.50000000000000000000

...

0.000244141 1.00024414062500000000

0.00012207 1.00012207031250000000

6.10352E-05 1.00006103515625000000

3.05176E-05 1.00003051757812500000

1.52588E-05 1.00001525878906250000

7.62939E-06 1.00000762939453125000

3.8147E-06 1.00000381469726562500

1.90735E-06 1.00000190734863281250

9.53674E-07 1.00000095367431640625

4.76837E-07 1.00000047683715820312

2.38419E-07 1.00000023841857910156

Calculated Apparatus epsilon: 1.19209E-07

editApproximation application Java

A agnate Java method:

clandestine abandoned calculateMachineEpsilonFloat() {

float machEps = 1.0f;

do {

machEps /= 2.0f;

}

while ((float)(1.0 + (machEps/2.0)) != 1.0);

System.out.println( "Calculated apparatus epsilon: " + machEps );

}

Another Java accomplishing (together with a Smalltalk version) can be begin in the addendum to Besset's (2000) after methods in Java & Smalltalk book. This addendum presents a complete accomplishing of MACHAR (Cody, 1988) in Smalltalk and Java.

editApproximation application a Perl one-liner

The afterward Perl crack prints the amount of apparatus epsilon for the float abstracts blazon (as implemented by the accustomed Perl interpreter).

perl -le '$e=1; $e/=2 while $e/2+1>1; book $e'

On Windows, single-quotes have to be afflicted to bifold ones.

editApproximation application Haskell

main = book . endure . map (subtract 1) . takeWhile (/= 1) . map (+ 1) . iterate (/2) $ 1

editApproximation application Python

The afterward Python action uses an approximation adjustment to actuate the amount of apparatus epsilon for an approximate after abstracts type.

def machineEpsilon(func=float):

machine_epsilon = func(1)

while func(1)+func(machine_epsilon) != func(1):

machine_epsilon_last = machine_epsilon

machine_epsilon = func(machine_epsilon) / func(2)

acknowledgment machine_epsilon_last

Some examples of its achievement (using IPython):

In 1: machineEpsilon(int)

Out1: 1

In 2: machineEpsilon(float)

Out2: 2.2204460492503131e-16

In 3: machineEpsilon(complex)

Out3: (2.2204460492503131e-16+0j)

Machine epsilon from the Python accepted library:

>>> from sys acceptation float_info

>>> float_info.epsilon

2.2204460492503131e-16

Using NumPy, the amount of an inexact type's apparatus epsilon can be bent application the eps aspect of numpy.finfo as follows (again, in iPython):

In 1: acceptation numpy

In 2: numpy.finfo(numpy.float).eps

Out2: 2.2204460492503131e-16

In 3: numpy.finfo(numpy.complex).eps

Out3: 2.2204460492503131e-16

editApproximation application Ada

In Ada, epsilon maybe accepted from the 'Digits attribute.5

Epsilon : connected Real'Base := 1.0 / (10.0 ** Real'Digits);

editApproximation application Prolog

An approximation application addition and recursive predicates in Prolog is:

epsilon(X):-

Y is (1.0 + X),

Y = 1.0,

write(X).

epsilon(X):-

Y is X/2,

epsilon(Y).

An archetype beheading in the SWI-Prolog interpreter:

1 ?- epsilon(1.0).

1.1102230246251565e-16

true .

No comments:

Post a Comment