I really don’t think this is a precision problem, the answer should be about 0.226. Here’s the exact code:
val = I(i,j)
bucketSize
pos = val / bucketSize
I is just a matrix I’m taking values from. Here is the output from MATLAB:
val =
29
bucketSize =
128
pos =
0
What am I missing?
My guess would be that your matrix
Iis pixel data loaded from an image file, which will have values that are typically unsigned 8-bit integers. As already mentioned, converting both integer values to a double precision value will ensure that MATLAB performs floating point division instead of integer division (which will round off the result).Converting one value to double precision is insufficient:
If you’d like to find out more about the different numeric data types in MATLAB, you can check out this documentation.