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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:47:49+00:00 2026-06-12T01:47:49+00:00

While we all know that x == y can be problematic, where x and

  • 0

While “we all know” that x == y can be problematic, where x and y are floating point values, this question is a bit more specific:

int x = random.Next(SOME_UPPER_LIMIT);
float r = x;
// Is the following ALWAYS true?    
r == x

Now, since the range of float of is much larger than that of integers (but the precision is insufficient to uniquely present integers at the edges), it would be nice if responses to this question also addressed which values of x the above can be guaranteed for, if it can be guaranteed at all.


Currently my code is making this assumption (for relatively small values of x) – I would like to make sure that I won’t get bitten 🙂


This will fail with “not equal: 16777217” (cast float -> int):

for (int i = 0; i < int.MaxValue; i++) {
   float f = i;
   if ((int)f != i) throw new Exception("not equal " + i);
}

This similar code will not fail (only int -> float); however, due to loss in the conversion, there are several floats that can “equal” the same integer, and may represent a silent bug:

for (int i = 0; i < int.MaxValue; i++) {
   float f = i;
   if (f != i) throw new Exception("not equal " + i);
}
  • 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-12T01:47:50+00:00Added an answer on June 12, 2026 at 1:47 am

    Yes, the comparison will always be true, whatever value the int is.

    The int will be converted to a float to do the conversion, and the first conversion to float will always give the same result as the second conversion.

    Consider:

    int x = [any integer value];
    float y = x;
    float z = x;
    

    The values of y and z will always be the same. If the conversion loses precision, both conversions will lose the precision in exactly the same way.

    If you convert the float back to int to to the comparison, that’s another matter.


    Also, note that even if a specific int value converted to float always results in the same float value, that doesn’t mean that the float value has to be unique for that int value. There are int values where (float)x == (float)(x+1) would be true.

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

Sidebar

Related Questions

hi all i want to know that while specifying url in my iphone native
While reading this class BitmapFactory I noticed that almost all methods inside are static.
First of all, I know that it can be implemented with a mutex and
I have written this gem that has more than 10,000 generated classes. While installing
Is it possible to clear all resources in PHP? I know that you can
Folks, we all know that IP blacklisting doesn't work - spammers can come in
I know that there's a very similar topic for this question, however I'm trying
My idea was to copy a dictionary while resetting all the values of the
Seems I've outdone myself. All the while I was creating this pretty little 'latest
My Basic Goal A Rails app that can load new content via Ajax while

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.