Determine the machine precision in Matlab

In summary, to determine machine precision in Matlab, you can use the x=1.0;x=x/2;x=(x/2) + 1.0;x=if x>1.0; loop approach. If you want to be more accurate, you can use the x=1.0;x=x/2;x=if x>=2.0;x=2.0; loop approach.
  • #1
madking153
37
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
  • #2
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.
 
  • #3
in MatLab you can simply type:
eps <enter>
 
  • #4
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.
 

1. What is machine precision in Matlab?

Machine precision in Matlab refers to the smallest number that can be represented in the computer's memory. This is typically determined by the number of bits used to store numbers, and it can vary depending on the system and the data type used.

2. Why is it important to determine machine precision in Matlab?

Determining the machine precision in Matlab is important because it allows you to understand the limitations of the computer's memory and how it may affect your calculations. It also helps you avoid errors or inaccuracies in your code that may arise from using numbers that are too small to be accurately represented.

3. How can I determine machine precision in Matlab?

You can determine machine precision in Matlab by using the built-in function eps. This function returns the distance between 1 and the next largest number that can be represented in the computer's memory. Alternatively, you can use the realmin function to get the smallest positive normalized floating-point number.

4. Does machine precision vary in different programming languages?

Yes, machine precision can vary in different programming languages and systems. This is because different languages and systems may use different data types and represent numbers in different ways, leading to variations in the smallest representable number.

5. How can I handle machine precision in my Matlab code?

To handle machine precision in your Matlab code, you can use functions such as eps or realmin to determine the smallest representable number and take it into account when writing your calculations. You can also use the format function to change the display format of numbers to avoid displaying numbers that are too small to be accurately represented.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
955
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
800
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
Back
Top