0

I'm creating a 3D matrix(lat,long,landuse)and trying to calculate the fraction of the landuse for each matrix cell:

One example cell could produce a vector specified by:

fractions=landuse/total_number_of_landuse;

I have to do this for multiple cells and when I do this and sum up the fractions, some are 1 and some are 1.0000. This is how it looks I know this is due to the floating point "problem".

I have to use these data for a C++-Modell which is checking the sum also and then throws me an error for some of the cells (those which summs up to 1.0000).

I also tried calculating these as double or single.

Any idea how I can make them sum up to 1?

Thanks, Philipp

Squeezie
  • 361
  • 2
  • 14
  • Just round them off if they are within some tolerance of 1? [Pertinent](https://stackoverflow.com/q/686439/52738)... – gnovice Oct 25 '17 at 21:40
  • @gnovice if I round up the sum in matlab, that wouldnt solve the problem when the c++ programm summs up the the fractions – Squeezie Oct 25 '17 at 21:52
  • I meant you could do that when you check it in the C++ program. – gnovice Oct 25 '17 at 21:55
  • Since the programm is some 10k lines I dont want to mess with that, since it has probably a purpose for the check. – Squeezie Oct 25 '17 at 21:59
  • 2
    You can't make then sum to exactly `1`, in general. You need to compare (in the C++ program) with a tolerance, maybe about `1e-15`. So instead of `x==1` you'd do something like `abs(x-1)<=1e-15` – Luis Mendo Oct 25 '17 at 22:02
  • [Not quite a duplicate, but similar and possibly helpful.](https://stackoverflow.com/questions/17641300/rounding-floats-so-that-they-sum-to-precisely-1/17641358#17641358) – Eric Postpischil Oct 25 '17 at 23:26
  • 3
    @LuisMendo: Well, you _could_ make them add to exactly 1, by rounding each fraction to an integer multiple of `2^-53` (assuming IEEE 754 binary64 format), and then tweaking the rounded fractions as necessary to make things work, perhaps using something resembling the [largest remainder method](https://en.wikipedia.org/wiki/Largest_remainder_method). But it seems unlikely that this is the right solution to the OPs problem. – Mark Dickinson Oct 26 '17 at 11:49
  • @MarkDickinson Good point. I agree on both accounts – Luis Mendo Oct 26 '17 at 12:04

0 Answers0