MATLAB switching between integer and floating numbers in a loop during computation
显示 更早的评论
Hi,
I am wondering what is happening in the following for-loop where the variable is changing from being an integer to float to integer to float even though it is not supposed to (?). Don't know what I am missing here.
clc;clear;
for i = 4 : 0.1 : 5
sd=i*1e-09;
start_cell=sd*1e10+1
end
1 个评论
Stephen23
2023-1-19
"I am wondering what is happening in the following for-loop where the variable is changing from being an integer to float to integer to float even though it is not supposed to"
It isn't. The value is floating point the entire time.
"Don't know what I am missing here."
The behavior of floating point numbers and how MATLAB displays floating point numbers: the values with no trailing zeros are exactly that value, the others have some accumulated error.
采纳的回答
更多回答(1 个)
Luca Ferro
2023-1-18
0 个投票
start_cell is always of type double during the entire execution. You can test it by debugging the code manually or by using the whos command like this:
for i = 4 : 0.1 : 5
sd=i*1e-09;
start_cell=sd*1e10+1;
whos start_cell
end
The 'issue' you are facing is just visual, it depends on how your format is set.
Here you can find how to set and what type of settings you can change for the visualization:
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!