How to eliminate the error caused by converting double to uint16
Show older comments
How to eliminate the error caused by converting double to uint16
6 Comments
Dyuman Joshi
on 14 Oct 2023
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)
Walter Roberson
on 14 Oct 2023
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.
Dyuman Joshi
on 14 Oct 2023
"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
Dyuman Joshi
on 14 Oct 2023
I see, "convert" is used generally, where as "map" is used particularly when trying to convert numbers outside the range of an integer class.
Walter Roberson
on 14 Oct 2023
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.
Answers (0)
Categories
Find more on Data Type Conversion 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!