Determine the machine precision in Matlab

Click For Summary

Discussion Overview

The discussion centers around determining machine precision in MATLAB, specifically how to estimate machine epsilon and the implications for the number of bits used in the mantissa. Participants explore various methods to compute machine precision, including coding routines and using built-in functions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • Some participants propose a routine to determine machine epsilon by iteratively halving a number and checking when it no longer affects the sum with 1.0.
  • One participant suggests that a different routine, which compares to zero, yields a smaller machine epsilon, although they caution that this may not be a reliable lower limit.
  • Another participant points out a potential error in the first algorithm, suggesting it should compare to 2.0 instead of 1.0, as the sequence approaches 2.0.
  • One participant shares empirical results from running both algorithms in C, noting the number of iterations required for different data types.
  • Another participant mentions the built-in MATLAB command 'eps' to directly obtain the machine epsilon value.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of the two proposed algorithms for determining machine epsilon, with some agreeing on the utility of the built-in MATLAB function while others debate the accuracy of the iterative methods.

Contextual Notes

There are unresolved questions regarding the reliability of the different algorithms and their implications for machine precision, as well as the dependence on specific machine architectures and MATLAB versions.

madking153
Messages
37
Reaction score
0
hi, how to determine the machine precision in MATLAB and estimate from this how many bits are used in the mantissa ??
 
Physics news on Phys.org
madking153 said:
hi, how to determine the machine precision in MATLAB and estimate from this how many bits are used in the mantissa ??

OK, since no one tackled this, I will.

This is not restricted to just using Matlab, but I think you can write your own routine in matlab.

The common way of determining machine precision (some call it the machine epsilon) is the figure out when a mathematical operation hasn't changed when two different numbers are used that only differ by a very small amount (thus the term machine "epsilon"). For example, write a routine like this:

1. x = 1.0
2. x = (x/2) + 1.0
3. If x > 1.0, go do (2) again

It will continue to do the loop until x is small enough that when you add it to 1.0, the machine no longer sees it as any different than 1.0. Try this with a few times with x defined with several different precision. The smallest value of x upon exiting the loop is your machine epsilon.

Now, what is interesting also is that you MAY get a different value for the machine epsilon if you do this instead:

1. x = 1.0
2. x = x/2
3. If x > 0, go do (2) again

At first glance, there shouldn't be any different between these two routines, but it depends on how the machine or register compares a value with the value "0". You get a smaller machine epsilon using this routine. However, I don't think this is a safe lower limit to use, since most of the time, you are actually doing mathematical operations. So the earlier machine epsilon will be the one to trust.

Zz.
 
in MatLab you can simply type:
eps <enter>
 
ZapperZ said:
1. x = 1.0
2. x = (x/2) + 1.0
3. If x > 1.0, go do (2) again

Thanks ZapperZ for showing me this method!

However I think the first algorithm has a small error. The sequence approaches 2.0

On iteration
1 x = 1.5
2 x = 1.75
3 x = 1.875
...

So I think the algorithm should be recoded as
1. x = 1.0
2. x = (x/2) + 1.0
3. If x < 2.0, go do (2) again

Incidentally, I coded both algorithms in C and ran them on my P4.
Algorithm 1 ran for 24 iterations and algorithm 2 ran for 150 iterations with floats, and 53 and 1075 (!) for doubles.

So you were right, the compare to zero is much more accurate in floating point math.

Also, on my machine, MATLAB R14 produces

>> eps

ans =

2.2204e-016

which corresponds to the algorithm 1 double answer obtained from the C program.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K