How to eliminate the error caused by converting double to uint16

How to eliminate the error caused by converting double to uint16

6 Comments

Could you specify which error?
Are you trying to convert a non-integer double value to uint16?
Or are you trying to assign numbers outside of the range of uint16 i.e. (0:1:2^16-1)
It would be natural to speculate that there might be some double precision values that cannot be converted, such as maybe inf or nan. However it turns out that every double precision number can be converted to some uint16 without error message.
So we have to guess that maybe you assigned a value to a variable named "uint16", or else that the number of input double precision values is not the same as the number of locations available to be assigned to.
"However it turns out that every double precision number can be converted to some uint16 without error message."
Would using "mapped" be more appropriate instead of "converted"?
@Dyuman Joshi "mapped" is not a bad word there, but see that the help documentation uses "convert"
help uint16
UINT16 Convert to unsigned 16-bit integer. I = UINT16(X) converts the elements of the array X into unsigned 16-bit integers. X can be any numeric object, such as a DOUBLE. The values of a UINT16 range from 0 to 65535, or INTMIN('uint16') to INTMAX('uint16'). Values outside this range saturate on overflow, namely they are mapped to 0 or 65535 if they are outside the range. If X is already an unsigned 16-bit integer array, then UINT16 has no effect. DOUBLE and SINGLE values are rounded to the nearest UINT16 value on conversion. Some arithmetic operations are defined for UINT16 on interaction with other UINT16 arrays. For example, +, -, .*, ./, .\ and .^. If at least one operand is scalar, *, /, \ and ^ are also defined. UINT16 arrays may also interact with scalar DOUBLE variables, including constants, and the result of the operation is UINT16. UINT16 arrays saturate on overflow in arithmetic. You can define or overload your own methods for the UINT16 class (as you can for any object) by placing the appropriately named method in an @uint16 directory within a directory on your path. Type HELP DATATYPES for the names of the methods you can overload. A particularly efficient way to initialize a large UINT16 arrays is: I = zeros(1000,1000,'uint16') which creates a 1000x1000 element UINT16 array, all of whose entries are zero. You can also use ONES and EYE in a similar manner. Example: X = 17 * ones(5,6,'uint16') See also DOUBLE, SINGLE, DATATYPES, ISINTEGER, UINT8, UINT32, UINT64, INT8, INT16, INT32, INT64, INTMIN, INTMAX, EYE, ONES, ZEROS. Documentation for uint16 doc uint16 Other uses of uint16 categorical/uint16 fixedpoint/uint16 opc.hda.Data/uint16 codistributed/uint16 gpuArray/uint16
I see, "convert" is used generally, where as "map" is used particularly when trying to convert numbers outside the range of an integer class.
Consider, though, that (for example) π is within the numeric range 0 to 65535 so if you were using "convert" for in-range but "map" for out of range, you would have to use "convert" for π . uint16(pi) is of course uint16(round(pi)) which is 3 and there are a lot of other nearby values like that also get changed to 3. So even within the range 0 to 65535 the mapping is not "one to one and onto". It would not be unreasonable to argue that "convert" is possibly the wrong verb when the conversion is not bijective, that "map" might be a more appropriate verb -- but that "map" should be used throughout, not just for values outside of the range 0 to 65535.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2019b

Asked:

on 14 Oct 2023

Commented:

on 14 Oct 2023

Community Treasure Hunt

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

Start Hunting!