# Sig figs on the rand function

2 次查看（过去 30 天）
Spaceman 2024-3-20

Just another general question here, is there a reason the rand function has 6 sig figs? i.e. 12.3456
Is there a way to change this so there are more or less post decimal? Or is this just how MATLAB handles this function, generally.
Thank you in advance!

### 采纳的回答

Chunru 2024-3-20
==> Just another general question here, is there a reason the rand function has 6 sig figs? i.e. 12.3456
rand function (by defaut) has "double" type, which means it is represented internally by 64bits, precision of up to 15 digits.
MATLAB has a default display format, which is "short" and has 4 digits after decimal point (Note: this is only for display, internal computations are based on double).
==>Is there a way to change this so there are more or less post decimal? Or is this just how MATLAB handles this function, generally.
Use format long to display data with more digits after decimal point (doc format for more details)
a = rand(); % internally stored as double
a % default (4 digits after dp)
a = 0.0083
format long % (15 digits after dp)
a
a =
0.008251124809451
##### 3 个评论显示 1更早的评论隐藏 1更早的评论
Chunru 2024-3-20
% format long?
format long
grade=rand(1,49)*(100-20)+20; % could also do randi([20 100],1,49);
% format long?
61.084787400165922
% Use fprintf for more control of the format
fprintf('The average grade is %0.10f \n',grade_avg) % 10 digits after dp
The average grade is 61.0847874002
disp('passing average')
else
disp('failing average')
end
passing average
format default % revert to default
Spaceman 2024-3-20
Thank you. So default format is 'format short'. The power MATLAB holds is but only scratched on the surface by me.

### 类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by