Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4615

Ruby Floating Point Math - Issue with Precision in Sum Calc

$
0
0

I'm having some issues with floating point math, and have gotten totally lost in ".to_f"'s, "*100"'s and ".0"'s!

I was hoping someone could help me with my specific problem, and also explain exactly why their solution works so that I understand this for next time.

My program needs to do two things:

  1. Sum a list of decimals, determine if they sum to exactly 1.0
  2. Determine a difference between 1.0 and a sum of numbers - set the value of a variable to the exact difference to make the sum equal 1.0.

For example:

  1. [0.28, 0.55, 0.17] -> should sum to 1.0, however I keep getting 1.xxxxxx. I am implementing the sum in the following fashion:

    sum = array.inject(0.0){|sum,x| sum+ (x*100)} / 100
  2. The reason I need this functionality is that I'm reading in a set of decimals that come from excel. They are not 100% precise (they are lacking some decimal points) so the sum usually comes out of 0.999999xxxxx or 1.000xxxxx. For example, I will get values like the following:

    0.568887955,0.070564759,0.360547286

To fix this, I am ok taking the sum of the first n-1 numbers, and then changing the final number slightly so that all of the numbers together sum to 1.0 (must meet validation using the equation above, or whatever I end up with). I'm currently implementing this as follows:

          sum = 0.0          array.each do |item|            sum += item * 100.0          end          array[i] = (100 - sum.round)/100.0

I know I could do this with inject, but was trying to play with it to see what works. I think this is generally working (from inspecting the output), but it doesn't always meet the validation sum above. So if need be I can adjust this one as well. Note that I only need two decimal precision in these numbers - i.e. 0.56 not 0.5623225. I can either round them down at time of presentation, or during this calculation... It doesn't matter to me.

Thank you VERY MUCH for your help!


Viewing all articles
Browse latest Browse all 4615

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>