MATLAB switching between integer and floating numbers in a loop during computation
Show older comments
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 Comment
Stephen23
on 19 Jan 2023
"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.
Accepted Answer
More Answers (1)
Luca Ferro
on 18 Jan 2023
0 votes
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:
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!