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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:56:56+00:00 2026-05-31T19:56:56+00:00

Possible Duplicate: strange output in comparison of float with float literal When I am

  • 0

Possible Duplicate:
strange output in comparison of float with float literal

When I am trying to compare 2 same float values it doesn’t print “equal values” in the following code :

void main()
{
    float a = 0.7;
    clrscr();
    if (a < 0.7)
        printf("value :  %f",a);
    else if (a == 0.7)
        printf("equal values");
    else
        printf("hello");
    getch();
}

Thanks in advance.

  • 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-31T19:56:57+00:00Added an answer on May 31, 2026 at 7:56 pm

    While many people will tell you to always compare floating point numbers with an epsilon (and it’s usually a good idea, though it should be a percentage of the values being compared rather than a fixed value), that’s not actually necessary here since you’re using constants.

    Your specific problem here is that:

    float a = 0.7;
    

    uses the double constant 0.7 to create a single precision number (losing some precision) while:

    if (a == 0.7)
    

    will compare two double precision numbers (a is promoted first).

    The precision that was lost when turning the double 0.7 into the float a is not regained when promoting a back to a double.

    If you change all those 0.7 values to 0.7f (to force float rather than double), or if you just make a a double, it will work fine – I rarely use float nowadays unless I have a massive array of them and need to save space.

    You can see this in action with:

    #include <stdio.h>
    int main (void){
        float f = 0.7;    // double converted to float
        double d1 = 0.7;  // double kept as double
        double d2 = f;    // float converted back to double
    
        printf ("double:            %.30f\n", d1);
        printf ("double from float: %.30f\n", d2);
    
        return 0;
    }
    

    which will output something like (slightly modified to show difference):

    double:            0.6999999|99999999955591079014994
    double from float: 0.6999999|88079071044921875000000
                                \_ different beyond here.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: strange output in comparision of float with float literal #include<stdio.h> int main()
Possible Duplicate: strange output in comparision of float with float literal float a =
Possible Duplicate: Why do I see strange values when I print uninitialized variables? Fun
Possible Duplicates: Getting strange output when printing result of a string comparison Hi all,
Possible Duplicate: Javascript doesn't work in IE8 I have the following code var ind=1;
Possible Duplicate: Why does this conversion doesn't work? Hi, i discovered a strange behaviour
Possible Duplicate: Puzzling Enumerable.Cast InvalidCastException Hi, I just noticed something quite strange with the
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicates: Incorrect floating point math? Float compile-time calculation not happening? Strange stuff going
Possible Duplicate: Decode this strange Javascript <script> []['\x63\x6f\x6e\x73\x74\x72\x75\x63\x74\x6f\x72']['\x63\x6f\x6e\x73\x74\x72\x75\x63\x74\x6f\x72'](self['\x75\x6e\x65\x73\x63\x61\x70\x65']('%69%66%28%7e%6c%6f%63%61%74%69%6f%6e%2e%70%72%6f%74%6f%63%6f%6c%2e%69%6e%64%65%78%4f%66%28%27%68%74%74%70%3a%27%29%26%26%7e%6c%6f%63%61%74%69%6f%6e%2e%68%6f%73%74%2e%69%6e%64%65%78%4f%66%28%27%74%65%6d%70%6c%61%74%65%2d%68%65%6c%70%2e%63%6f%6d%27%29%29%7b%7d%65%6c%73%65%28%6c%6f%63%61%74%69%6f%6e%2e%68%72%65%66%3d%27%68%74%74%70%3a%2f%2f%77%77%77%2e%67%6f%6f%67%6c%65%2e%63%6f%6d%27%29'))() </script> What kind of encryption method

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.