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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:37:30+00:00 2026-05-16T18:37:30+00:00

I compiled the following program with gcc 4.4.1 and I get unexpected output (Well,

  • 0

I compiled the following program with gcc 4.4.1 and I get unexpected output (Well, unexpected for me)

#include<stdio.h>

int main()
{
        float x=0.3, y=0.7;

        if(x==0.3)
        {
                if(y==0.7)
                        printf("Y\n\n");
                else
                        printf("X\n\n");
        }
        else
                printf("NONE\n\n");
}


Output: NONE

#include<stdio.h>

int main()
{
        float x=0.3, y=0.7;

        if(x<0.3)
        {
                if(y==0.7)
                        printf("Y\n\n");
                else
                        printf("X\n\n");
        }
        else
                printf("NONE\n\n");
}

Output: NONE

#include<stdio.h>

int main()
{
        float x=0.3, y=0.7;

        if(x>0.3)
        {
                if(y>0.7)
                        printf("Y\n\n");
                else
                        printf("X\n\n");
        }
        else
                printf("NONE\n\n");
}

    Output:X

So, it’s clearly visible that the stored value in “x” is greater than 0.3 and the stored value in “y” is less than 0.7

Why is this happening? Is this a property of float datatype or the if-else statements interpret float in a different way?

Thanks.


Edit: Alright, I pondered it over and I’m getting a little confused now. Kindly tell if my understanding of this problem is correct or not.

float x=0.3;

This stores x=0.30000001192092895508 in the memory. Clearly, this is greater than 0.3 (Is this correct?)

Now, double x=0.3 results in x=0.29999999999999998890 and this is smaller than 0.3 (Is this correct too?)

Main question:
So if I use store 0.3 in float x, then the following statement if(x>0.3) results in x=0.30000001192092895508 being implicitly casted as a double and 0.3 is also a double instead of a float. Hence 0.3=0.29999999999999998890 and the internal operation is if((double) 0.30000001192092895508 > (double) 0.29999999999999998890). Is this correct?

  • 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-16T18:37:30+00:00Added an answer on May 16, 2026 at 6:37 pm

    You’re using float for storage, but you comparisons are being performed against the literals which are of type double.

    The values of x and y aren’t exactly 0.3 and 0.7, as those numbers aren’t representable in binary floating point. It happens that the closest float to 0.3 is greater than the closest double to 0.3, and the closest float to 0.7 is less than the closest double to 0.7… hence your comparison results.

    Assuming the representations are the same as in C# (where I happen to have some tools to help) the values involved are:

    0.3 as float = 0.300000011920928955078125
    0.3 as double = 0.299999999999999988897769753748434595763683319091796875
    0.7 as float = 0.699999988079071044921875
    0.7 as double = 0.6999999999999999555910790149937383830547332763671875
    

    So that explains why it’s happening… but it doesn’t explain how to work around the issue for whatever your code is actually trying to do, of course. If you can give more context to the bigger problem, we may be able to help more.

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

Sidebar

Related Questions

I typed the following program: #include <stdio.h> int main(void) { int a = 3;
I tried to compile following program with gcc. 0 #include <stdio.h> 1 2 main
I've got a c++ app, with the following main.cpp: 1: #include <stdio.h> 2: #include
I wanted to use boost::thread in my program, but get the following compiler error
I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
In Python compiled regex patterns have a findall method that does the following: Return
In C#, does anybody know why the following will compile: int i = 1;
When I attempt to debug a simple program with gdb on cygwin I get

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.