What is the real value of "single(my_variable)"?

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Andy Bartlett
Andy Bartlett il 15 Set 2021
Modificato: Andy Bartlett il 15 Set 2021
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
0.0010443973005749285221099853515625 > decimalText > 0.0010443971841596066951751708984375
then MATLAB, a C compiler, etc., parsing that text would round it to this representable value of type single
0.001044397242367267608642578125
Sufficient digits and superflous digits
Let's look at our mid-point values again
Mid-point Value Above 0.0010443973005749285221099853515625
Mid-point Value Below 0.0010443971841596066951751708984375
and think about when fewer digits are sufficient to be certain we are between these two values.
Consider this example,
Mid-point Value Above 0.0010443973005749285221099853515625
0.0010443972xxx...
Mid-point Value Below 0.0010443971841596066951751708984375
where the x's could be any digit, and there can be any number of x's from 0 to inf.
We can be certain that a correct parser would map those infinite possible decimal text combinations to the single precision value 0.001044397242367267608642578125.
The decimal text 0.0010443972 is sufficiently long.
More digits could be appended to the end, but they would be superflous. The digital text with any set of superflous digits would still parse back to the one representable value 0.001044397242367267608642578125.
Lossless Roundtrip Sufficient Digits
Roundtrip means converting a value to text, then parsing that text to produce a value. If the final value is identical to the original value, then the rountrip conversion is lossless.
For doubles, 17 significant decimal digits are sufficient to always be lossless. For singles, 8 digits are always sufficient to be lossless.
The following code exercises this limit. Notice that format hex is utilized to make small value differences visible in the command window.
format hex
nDigitsOfPrecision = 8;
vOrig = single(0.001044397242367267608642578125);
vOrig = vOrig+eps(vOrig)*[1,0,-1]
vTextDig7 = mat2str(vOrig,'class',nDigitsOfPrecision-1)
vRoundTripDig7 = eval(vTextDig7)
format long
worstCaseErrorDig7 = max(abs(vRoundTripDig7 - vOrig))
format hex
vTextDig8 = mat2str(vOrig,'class',nDigitsOfPrecision)
vRoundTripDig8 = eval(vTextDig8)
format long
worstCaseErrorDig8 = max(abs(vRoundTripDig8 - vOrig))
The output shows 7 digits was not lossless, but 8 digits was.
vOrig =
1×3 single row vector
3a88e429 3a88e428 3a88e427
vTextDig7 =
'single([0.001044397 0.001044397 0.001044397])'
vRoundTripDig7 =
1×3 single row vector
3a88e426 3a88e426 3a88e426
worstCaseErrorDig7 =
single
3.4924597e-10
vTextDig8 =
'single([0.0010443974 0.0010443972 0.0010443971])'
vRoundTripDig8 =
1×3 single row vector
3a88e429 3a88e428 3a88e427
worstCaseErrorDig8 =
single
0
Note 8 digits for single is sufficient to be lossless, but for individual values 8 digits may not be necessary. For example, for the individual value single(0.01), one digit is sufficient for lossless roundtrip.
nDigitsOfPrecision = 1
vOrig = single(0.01)
format hex
vOrig
vTextDig1 = mat2str(vOrig,'class',nDigitsOfPrecision)
vRoundTripDig1 = eval(vTextDig1)
format long
vRoundTripDig1
worstCaseErrorDig1 = max(abs(vRoundTripDig1 - vOrig))
which outputs
nDigitsOfPrecision =
1
vOrig =
single
0.0100000
vOrig =
single
3c23d70a
vTextDig1 =
'single(0.01)'
vRoundTripDig1 =
single
3c23d70a
vRoundTripDig1 =
single
0.0100000
worstCaseErrorDig1 =
single
0

Più risposte (1)

Matt J
Matt J il 15 Set 2021
Modificato: Matt J il 15 Set 2021
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)
value_float = single
1.0443972e-03
  3 Commenti
Matt J
Matt J il 15 Set 2021
Modificato: Matt J il 15 Set 2021
@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.
Andy Bartlett
Andy Bartlett il 15 Dic 2022
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)
u1 = single 0.05
u2 = single 0.05
format hex is ugly but will show that values are distinct
format hex
u1, u2
u1 = single 3d4ccccd
u2 = single 3d4cccce
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
u1Sym = 
u2Sym = 
difference = 
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)
ans = 15.954589770191
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
u3 = 2.1
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
uOrig approximated to 15 digits of accuracy: double(2.1)
Error when that text is parsed back into MATLAB: 4.4408920985006262e-16
uOrig approximated to 16 digits of accuracy: double(2.1)
Error when that text is parsed back into MATLAB: 4.4408920985006262e-16
uOrig approximated to 17 digits of accuracy: double(2.0999999999999996)
Error when that text is parsed back into MATLAB: 0
------------------------------
uOrig approximated to 7 digits of accuracy: single(0.05)
Error when that text is parsed back into MATLAB: -3.7252902984619141e-09
uOrig approximated to 8 digits of accuracy: single(0.050000004)
Error when that text is parsed back into MATLAB: 0
------------------------------

Accedi per commentare.

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by