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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:57:10+00:00 2026-05-25T12:57:10+00:00

I came across a strange corner of Java.(It seems strange to me) double dd

  • 0

I came across a strange corner of Java.(It seems strange to me)

double dd = 3.5;          
float ff = 3.5f;
System.out.println(dd==ff);   

o/p: true

double dd = 3.2;
float ff = 3.2f;
System.out.println(dd==ff);

o/p: false

I observed that if we compare any two values (a float and a double as I mentioned in the example) with .5 OR .0 like 3.5, 234.5, 645.0
then output is true i.e. two values are equal otherwise output is false though they are equals.

Even I tried to make method strictfp but no luck.
Am I missing out on something.

  • 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-05-25T12:57:11+00:00Added an answer on May 25, 2026 at 12:57 pm

    Take a look at What every computer scientist should know about floating point numbers.

    Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation….

    — Edit to show what the above quote means —

    You shouldn’t ever compare floats or doubles for equality; because, you can’t really guarantee that the number you assign to the float or double is exact.

    So

     float x = 3.2f;
    

    doesn’t result in a float with a value of 3.2. It results in a float with a value of 3.2 plus or minus some very small error. Say 3.19999999997f. Now it should be obvious why the comparison won’t work.

    To compare floats for equality sanely, you need to check if the value is “close enough” to the same value, like so

    float error = 0.000001 * second;
    if ((first >= second - error) || (first <= second + error)) {
       // close enough that we'll consider the two equal
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across a strange behaviour with Chrome earlier that I can't seem to
While working on a WebSocket server in Java I came across this strange bug.
I came across some Java code that has a method containing the following: static
I came across something that I think rather strange. The test program int main(int
Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
I came across a strange problem today that i could understand the reason. Take
I just came across something that was quite strange. >>> t = ([],) >>>
I came across a strange behavior while theming Drupal. I turned a few modules
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I came across some strange behavior of asp:PlaceHolder , it removes whitespaces between the

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.