What is the real value of "single(my_variable)"?
Show older comments
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?
Accepted Answer
More Answers (1)
So the C code keeps good precission. But, does Matlab?
Yes, if by "good precision" you mean up to 7 digits. You can see that even in your C code output, anything beyond the 7th significant digit is fake news.
format longE
value_float = single(0.001044397222448)
3 Comments
Awais Saeed
on 15 Sep 2021
@Matt J just asking for the sake of curiosity and learning, why is it fake beyond 7th significant digit?
@Awais Saeed Because single floating point numbers only have 7 digits of precision, (unlike double floats which have 15). You can see in @Danijel Domazet's post that the C code output disagrees with the input number after the 7th significant digit.
MATLAB Display Format is an Approximation
MATLAB's display of values is very often just an approximation of the value actual stored in a variable. This is true even if format long, format long e, or format long g is used.
For example, notice how this two different values are displayed as if they are the same value.
format compact
format long g
u1 = single(0.05), u2 = u1 + eps(u1)
format hex is ugly but will show that values are distinct
format hex
u1, u2
Symbolic Toolbox shows the represented value with the 'f' trick
To see what value is actually represented in a double or single, Symbolic Toolbox is very helpful. But, be sure to provide the 'f' option to get an exact conversion of the represented value to symbolic.
format long g
u1Sym = sym(u1,'f'), u2Sym = sym(u2,'f'), difference = u2Sym - u1Sym
Minimum digits needed for lossless roundtrip parsing depends on value and type.
The number digits needed to represent a value with sufficient accuracy that it parses back to the original value depends on the type being used and the particular value.
Please keep in mind that sometimes you will hear a description of the equivalent digits of accuracy of a type. This equivalent digits of accuracy is not the same as the digits needed in a textual representation for lossless round trip parsing.
For example, you may hear that doubles has a little less than 16 decimal digits of accuracy, based on a calculation like this.
log10(flintmax-1)
But many double values, such as 3*0.07, need 17 digits of accuracy in a textual approximation to support lossless round trip parsing. For example.
u3 = 3*0.7
for j = 0:1
if j
uCur = u2;
else
uCur = u3;
end
nLo = floor(log10( flintmax(class(uCur)) - 1 ) );
nHi = nLo + 3;
for numDigitsPrecision = nLo:nHi
strRep = mat2str(uCur,numDigitsPrecision,'class');
uRoundTrip = eval(strRep);
err = uRoundTrip - uCur;
fprintf('uOrig approximated to %d digits of accuracy: %s\n',numDigitsPrecision,strRep)
fprintf('Error when that text is parsed back into MATLAB: %s\n',num2str(err,17))
if err == 0
fprintf('------------------------------\n')
break
end
end
end
Categories
Find more on Data Type Identification 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!