how matlab save long integers values without approximate it?
7 views (last 30 days)
Show older comments
i am saving integer values in matlab bur when i open the m file i found that matlab approximate the values!!! for example the value 15440 i found it 0.1544 but when i clic on it i foun it 1.5449999999999e+05 which is not even an integer !!!
i used format long, format shortEng, format longEng but it didnt work
any suggestions?
0 Comments
Answers (1)
dpb
on 19 Mar 2017
Edited: dpb
on 20 Mar 2017
All variables in Matlab are double by default unless you make them something else explicitly. All the formats you've used are also floating point and only affect the displayed value, not internal storage.
That said, integer values up to the size that can be held in a double-precision float (roughly 15 decimal digits) are also exact, so certainly the value 15440 can and will be stored exactly in memory.
You don't show actual code and data normally aren't stored in .m files, so not sure just how you wrote it to get it converted to floating representation with rounding--show us the code that created the file and how you subsequently read/displayed the value and we can then show you how to avoid the problem.
ADDENDUM
Also, format only affects the command window display form, not internal nor external storage. So, if the value is 1.5449999999999e+05, the value was obtained from either computation that had some floating point rounding to be not quite an integer value or read from a file written with a value of the same situation and that's what the value actually is.
Observe the following:
>> x=1.5449999999999e+05; % store the value...
>> format rat % display as ratio of nearest integers...
>> disp(x) % looks like an integer
154500
>> format short % revert to normal
>> disp(x) % but isn't integer, really...
1.5450e+05
>> format long % what is full value???
>> disp(x) % what we stored and what is in memory is this...
1.544999999999900e+05
>> disp(round(x)) % make it look like an integer...but it really isn't
154500
>> x=round(x) % now make it so...
x =
154500
>>
2 Comments
dpb
on 28 Feb 2020
Per the above comment it IS resolved.
There's not enough information for the specific case to know more as OP never came back with the actual code that created the case of the original posting...
See Also
Categories
Find more on Logical 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!