Danijel Domazet asked . 2021-10-22

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?

matlab , simulink , matlabprogramming

Expert Answer

Prashant Kumar answered . 2024-04-21 08:58:32

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

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.