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

1 ビュー (過去 30 日間)
Danijel Domazet 2021 年 9 月 15 日

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?

サインインしてコメントする。

### 採用された回答

Andy Bartlett 2021 年 9 月 15 日

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

サインインしてコメントする。

### その他の回答 (1 件)

Matt J 2021 年 9 月 15 日

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
##### 2 件のコメント表示非表示 1 件の古いコメント
Matt J 2021 年 9 月 15 日
@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.

サインインしてコメントする。

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by