What is the real value of “single(my_variable)”?
My script reads a string value “0.001044397222448” from a file, and after parisng the file this value ends up as double precission:
> format long
> value_double
value_double =
0.001044397222448
After I convert this number to singe using value_float = single(value_double), the value is:
> value_float
value_float =
0.0010444
What is the real value of this variable, that I later use in my Simulink simulation? Is it really truncated/rounded to 0.0010444?
My problem is that later on, after I compare this with analogous C code, I get differences.
In the C code the value is read as float gf = 0.001044397222448f; and it prints out as `0.001044397242367267608642578125000`. So the C code keeps good precission. But, does Matlab?
NOTE:-
Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.
Expert Answer
Prashant Kumar answered . 2021–10–22 06:33:19
Decimal text rounds to nearest representable value
When decimal text is read in and parsed by MATLAB, a C compiler, your own custom tool, etc., it will (or should) get mapped to the nearest representable value of the type it will be assigned to.
For the data type single, here are three neighboring representable values, shown exactly
Next Rep. Value Above 0.001044397358782589435577392578125
Quantized Value 0.001044397242367267608642578125
Next Rep. Value Below 0.001044397125951945781707763671875
Let’s also show the ideal mid-points between these three values. (Note, the midpoints are NOT representable in the type.)
Next Rep. Value Above 0.001044397358782589435577392578125
Mid-point Value Above 0.0010443973005749285221099853515625
Quantized Value 0.001044397242367267608642578125
Mid-point Value Below 0.0010443971841596066951751708984375
Next Rep. Value Below 0.001044397125951945781707763671875
Given these values, we can say if the decimal text’s value, as interpreted in the world of ideal math, is within this range
SEE COMPLETE ANSWER CLICK THE LINK