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

  • SEARCH
  • Home
  • 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 8355841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:59:08+00:00 2026-06-09T09:59:08+00:00

(This question is asked in a Ruby context, but I wouldn’t be upset if

  • 0

(This question is asked in a Ruby context, but I wouldn’t be upset if it were answered in a more general way.)

So (just about) everyone knows that comparing floating point numbers involved in math computations for equality is a no-go, and you have to test for range (in Ruby, like so):

assert_in_delta 5.0, 3.5+1.5, 0.0001

However, is this still necessary when sanity checking a floating point number for equality when it is passed from one subsystem to another, say (in a Ruby context) like so?

json={:foo => 0.5}.to_json
# POST the JSON to a Rails controller, that puts it in a Mongo database
found_obj=pull_obj_out_of_Mongo # details not important here
assert_in_delta 0.5, found_obj[:foo], 0.0001

I personally argue is that assert_in_delta is still a good idea here, because I don’t know, and perhaps many people don’t know, whether the conceptual floating point value 0.5 is passed along in a way that would enable it to be compared with the floating point literal 0.5 with equals. Is that actually the case, or am I being too paranoid about how floating point numbers are stored and passed along? How much does the answer depend on what language is being used?

  • 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-09T09:59:09+00:00Added an answer on June 9, 2026 at 9:59 am

    This depends a lot on what the subsystem does with the Float. In Ruby (1.8 and 1.9 at least), Floats simply wrap an double precision number (literally a double in C, which is stored as an IEEE 754 double-precision number on most modern architectures). The bits don’t change when you pass the number around or copy it in that representation.

    However, when you pass the Float into a subsystem, it may change the underlying representation. In the example you give, you’re serializing the number with JSON, which involves converting the float to a string, and then converting it back. There’s no guarantee that you’ll recover more than 15-16 digits of accuracy (assuming you converted it with that degree of accuracy). That seems like a lot, but it’s not enough to ensure strict equality for all possible floating point values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question was asked earlier by someone else but never answered: MKAnnotation - Map
I'm working in Ruby, but I think this question is best asked agnostic of
I've asked a different question about directory watching, which was answered, but the other
I've seen this question asked here but I haven't found one that is answered
This is related to a question I asked here: Thread Locking in Ruby (use
I've seen this question asked often, but none of the proposed solutions seem to
I've seen this question asked already - but none of the answers really gelled
I've seen this question asked on here, but it seems to be different than
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more
I had asked a question about parsing a file in ruby. I accepted an

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.