Inconsistent Results When Evaluating a Typecast and Reshape Operation in MATLAB
Show older comments

When I put a break point on this line and evaluate the section of typecast(reshape(uint32(4295010.2451024512 * (variable - 0)), [], 1), 'uint8') I get a array of [48 41 161 113]. However once i step through this line and check the values of tmArray(145:148) I get a array of [0 41 161 113] or sometime something like [48 40 161 113]. what could this be?
4 Comments
Steven Lord
on 21 Sep 2023
What is the class of the variable named variable? What are that variable's contents?
Walter Roberson
on 21 Sep 2023
I definitely would not hard-code 4295010.2451024512 in that line. It is a "magic number" that provides no information to the reader. At the very least, store the value in a documented variable.
I would also assign the result of the typecast to a tempory variable and then assign the variable into the array, so that the result could be checked. If the result in the temporary variable does not match the result that shows up in the array after the assignment, then that would be something that could be independently investigated.
David
on 21 Sep 2023
Walter Roberson
on 21 Sep 2023
Please show us num2hex(variable)
I am not getting a result anywhere close to what you are reporting.
I would suggest to you that taking variable - 0 is not a transparent way of converting variable to double precision. Just use that number - double(variable) for clarity.
I do not understand what the reshape() is doing there. The hard-coded constant is a scalar and variable is a scalar, so the result of the multiplication will be a scalar, and taking uint32() of the scalar is going to give a scalar. It does not make sense to me to reshape() a scalar.
Answers (1)
Bruno Luong
on 21 Sep 2023
Edited: Bruno Luong
on 21 Sep 2023
Do NOT enter the value from the screen display of a number
format long,
u1=typecast(uint8([48 41 161 113]),'uint32');
s1=single(u1)/4295010.2451024512;
fprintf('%1.8f\n', s1)
fprintf('%1.25f\n', s1); % NOT 818.1404 as you pretend, so you make many mistake along the way
s2=443.86093139 % This is NOT s1
typecast(reshape(uint32(4295010.2451024512 * (s1 - 0)), [], 1), 'uint8')
typecast(reshape(uint32(4295010.2451024512 * (s2 - 0)), [], 1), 'uint8')
s1-s2
Categories
Find more on Numeric Types 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!