Inconsistent Results When Evaluating a Typecast and Reshape Operation in MATLAB

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

What is the class of the variable named variable? What are that variable's contents?
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.
Its class is a single and its content is a constant float number
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.

Sign in to comment.

Answers (1)

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)
443.86093140
fprintf('%1.25f\n', s1); % NOT 818.1404 as you pretend, so you make many mistake along the way
443.8609313964843750000000000
s2=443.86093139 % This is NOT s1
s2 =
4.438609313900000e+02
typecast(reshape(uint32(4295010.2451024512 * (s1 - 0)), [], 1), 'uint8')
ans = 1×4
0 41 161 113
typecast(reshape(uint32(4295010.2451024512 * (s2 - 0)), [], 1), 'uint8')
ans = 1×4
48 41 161 113
s1-s2
ans = single
6.4843562e-09

Categories

Asked:

on 21 Sep 2023

Edited:

on 21 Sep 2023

Community Treasure Hunt

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

Start Hunting!