# Display precision vs actual precision

11 visualizaciones (últimos 30 días)
Mario Trevino el 30 de Jul. de 2014
Comentada: Seth Wagenman el 3 de Sept. de 2014
Hi everyone We are running some simulations in MATLAB and storing the results afterwards. Yet, we are concerned that the actual precision of the number format used to solve the equations might be huge with respect to what we actually need. For instance, one of the output variables of the program is voltage, which is bound to -120<=V<=40 and we require a max resolution of about 0.01, no more than that....my guess is that the number precision provided by MATLAB is exceeding our needs and this is costing us computing time.....
thanks!
mario
##### 2 comentariosMostrar NingunoOcultar Ninguno
Sara el 30 de Jul. de 2014
It's preferable to do all your calculations with higher precision than needed and only round the results. You may use single variables instead of the default (double) but I'm not sure you'll have any speed up. You could build a toy problem and check it out.
Seth Wagenman el 3 de Sept. de 2014
Do the toy problems below convince you that speed will increase if you use single variables vs. doubles?

Iniciar sesión para comentar.

Image Analyst el 2 de Ag. de 2014
Editada: Image Analyst el 2 de Ag. de 2014
Your only choices are to cast your variables to single or double. The calculations will take the same amount of time no matter if you use "format long" or "format short" - the number of decimal places upon output do not have any effect on what's being used in the mathematical operations. Using single will cut your memory usage down by half.
Using single will make the calculations faster. See this demo:
m1 = rand(12345,12345);
m2 = rand(size(m1));
tic;
for expt = 1 : 10
m3 = m1 .* m2;
end
toc;
m1 = single(m1);
m2 = single(m2);
tic;
for expt = 1 : 10
m4 = m1 .* m2;
end
toc;
The results:
Elapsed time is 2.756050 seconds.
Elapsed time is 1.518655 seconds.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Salaheddin Hosseinzadeh el 4 de Ag. de 2014
Wow! Amazing!
Seth Wagenman el 3 de Sept. de 2014
Editada: Seth Wagenman el 3 de Sept. de 2014
I do not know what "expt" and the loop was for (and my computer froze when I tried the script above) so I tried this and got similar results:
m1 = rand(999,999);
m2 = rand(size(m1));
tic
m3 = m1 .* m2;
toc
m1 = single(m1);
m2 = single(m2);
tic
m4 = m1 .* m2;
toc
Elapsed time is 0.011342 seconds.
Elapsed time is 0.007512 seconds.

Iniciar sesión para comentar.

### Más respuestas (1)

Andrew Reibold el 30 de Jul. de 2014
Editada: Andrew Reibold el 30 de Jul. de 2014
To round to the hundredths place
X_rounded_to_hundredth = round(X*100)/100
Matlab still stores '0's after though. Not sure if you can increase computing time.
##### 6 comentariosMostrar 4 comentarios más antiguosOcultar 4 comentarios más antiguos
Image Analyst el 2 de Ag. de 2014
I've heard that (because modern CPUs are so highly optimized that it doesn't matter), so that's why I wrote my code - to test it. Surprisingly I found, at least for that one example, that the single calculations took about half as long, though perhaps it might be dues to a memory thrashing issue due to larger arrays rather than purely due to the multiplication times. I should test that.
Seth Wagenman el 3 de Sept. de 2014
One problem with single variables...if they are ever part of any future calculation, all products become single variables as well, even if a double were part of the calculation.

Iniciar sesión para comentar.

### Categorías

Más información sobre Logical en Help Center y File Exchange.

### Etiquetas

Aún no se han introducido etiquetas.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by