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 8187033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:25:35+00:00 2026-06-07T02:25:35+00:00

I was testing some of mine Ruby 1.9 code when I’ve stumbled across something

  • 0

I was testing some of mine Ruby 1.9 code when I’ve stumbled across something that is very interesting. I’m hoping that someone can explain why is this happening.

Here is the code:

inf = Float::INFINITY

x = inf - inf
y = 0.0/0.0

puts "X is #{x}"
puts "Y is #{y}"

puts "X and Y are both NaN." if x.nan? && y.nan?

puts "This is all as we expected, but here is the mystery..."

puts "X is not equal to Y." if x == y

puts "Surprisingly not even X is equal to X." if x == x

And this is the output:

X is NaN
Y is NaN
X and Y are both NaN.
This is all as we expected, but here is the mystery...

I have assigned a NaN value in two different ways to two variables and when I’ve tested if they were equal that was not the case. After that I’ve tested if one variable is equal to itself and even that wasn’t true. It’s logical to expect that some value is equal to itself but as we can se, it just isn’t.

I don’t know if this is the right way to test NaN values, but I would really like to know what is behind all of this and is there a reason for a Ruby to behave like this?

Thank you

  • 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-07T02:25:38+00:00Added an answer on June 7, 2026 at 2:25 am

    If we think about it, it seems logical. You get x by subtracting one infinitely large number from another infinitely large number? What’s the result of that? We don’t know.

    How can we compare things that we don’t know?

    To backup my logic, here’s the source of Float#==

    static VALUE flo_eq(VALUE x, VALUE y)
    {
        volatile double a, b;
    
        switch (TYPE(y)) {
          case T_FIXNUM:
            b = (double)FIX2LONG(y);
            break;
          case T_BIGNUM:
            b = rb_big2dbl(y);
            break;
          case T_FLOAT:
            b = RFLOAT_VALUE(y);
    #if defined(_MSC_VER) && _MSC_VER < 1300
            if (isnan(b)) return Qfalse;
    #endif
            break;
          default:
            return num_equal(x, y);
        }
        a = RFLOAT_VALUE(x);
    #if defined(_MSC_VER) && _MSC_VER < 1300
        if (isnan(a)) return Qfalse;
    #endif
        return (a == b)?Qtrue:Qfalse;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am testing some Ruby code and have a failing Test::Unit::TestCase. Unfortunately, the failure
Hi I have been testing some very basic things in ruby and discover the
I am testing some code that has a client exe and a server exe
I am testing some code that does asynchronous processing using Grand Central Dispatch. The
While testing some HTTP server code, I noticed something odd: if I return Content-Type:
I am testing some weird-looking CSS code that I wrote (I'm using a mix
I'm testing some methods that involve email, and am trying to use a mock
We are testing some code to send email messages using Gmail from a form,
I'm testing some cookies that I'm creating via JavaScript. Is there a way to
I was testing some CSS3 stuff and I discovered that some properties like border-radius

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.