Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Floating Point Math - Issue with Precision in Sum Calc

Good morning all,

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!

like image 654
Brandon Avatar asked May 28 '12 13:05

Brandon


2 Answers

If accuracy is important to you, you should not be using floating point values, which, by definition, are not accurate. Ruby has some precision data types for doing arithmetic where accuracy is important. They are, off the top of my head, BigDecimal, Rational and Complex, depending on what you actually need to calculate.

It seems that in your case, what you're looking for is BigDecimal, which is basically a number with a fixed number of digits, of which there are a fixed number of digits after the decimal point (in contrast to a floating point, which has an arbitrary number of digits after the decimal point).

When you read from Excel and deliberately cast those strings like "0.9987" to floating points, you're immediately losing the accurate value that is contained in the string.

require "bigdecimal"
BigDecimal("0.9987")

That value is precise. It is 0.9987. Not 0.998732109, or anything close to it, but 0.9987. You may use all the usual arithmetic operations on it. Provided you don't mix floating points into the arithmetic operations, the return values will remain precise.

If your array contains the raw strings you got from Excel (i.e. you haven't #to_f'd them), then this will give you a BigDecimal that is the difference between the sum of them and 1.

1 - array.map{|v| BigDecimal(v)}.reduce(:+)
like image 145
d11wtq Avatar answered Sep 25 '22 22:09

d11wtq


Either:

  • continue using floats and round(2) your totals: 12.341.round(2) # => 12.34

  • use integers (i.e. cents instead of dollars)

  • use BigDecimal and you won't need to round after summing them, as long as you start with BigDecimal with only two decimals.

like image 28
Marc-André Lafortune Avatar answered Sep 22 '22 22:09

Marc-André Lafortune