Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:53:45+00:00 2026-06-04T22:53:45+00:00

Good morning all, I’m having some issues with floating point math, and have gotten

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:53:47+00:00Added an answer on June 4, 2026 at 10:53 pm

    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(:+)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Morning All! I have been developing a project for some time in which
Good morning all, I'm having a few problems with a method in my C#
Good morning all, I'm sure this is a gimme, but I have no idea
Good Morning, i have some Trouble with the Zend Framework and Zend_DB_Select, i want
Good Morning All, I have loaded over 1,300 blurbs into my client's database with
Good Morning All, I have code similar to the following try{ using(var tx =
Good morning all, I've got an association that I'm working on, and for some
Good morning all. I'm having a small crisis with table views in a 3.1
Good Morning All, I have a project which will be distributed as an Excel
Good morning all. I have an issue with a query. I want to select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.