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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:32:01+00:00 2026-06-08T01:32:01+00:00

Consider the following code: #include<stdio.h> #define k 0.7 #define p 0.5 int main() {

  • 0

Consider the following code:

#include<stdio.h>
#define k 0.7
#define p 0.5
int main()
{
    float a=k,b=p;
    double aa=k;
    if(a<k)
    {
         if(b<p) printf("2 is right");    
         else      printf("1 is right");
    }
    else printf("0 is right");
    printf("\n");
    if(a>k)
    {
         if(b<p) printf("2 is right");    
         else      printf("1 is right");
    }
    else printf("0 is right");
    return 0;
}

Consider this as part II of this question, here the understanding was that the double precision values of floating point constants (doubles when represented as numeric constants) was lost when it was converted to their corresponding floating point values. The exceptional values were X.5 and X.0. But I observed the following results:

Input           Output
K=0.1 to 0.4        0 is right
                    1 is right

K=0.5               0 is right
                    1 is right

K=0.6               0 is right
                    1 is right

K=0.7               1 is right
                    0 is right

K=0.8               0 is right
                    1 is right

K=0.9               1 is right
                    0 is right

K=8.4               1 is right
                    0 is right

Why is this queer behavior? How come only few floating point values are displaying this property? Can’t we assume that float precision values are always less than double precision values?? How do we explain this behavior?

  • 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-08T01:32:03+00:00Added an answer on June 8, 2026 at 1:32 am

    Can’t we assume that float precision values are always less than double precision values??

    Not really, they may both have the same precision. You can assume that the range and precision of double is not smaller than that of float.

    But, for all practical purposes, it’s a profitable bet that double has 53 bits of precision and float has 24. And that double has 11-bit exponents, float 8-bit.

    So, disregarding exotic architectures, float has less precision and a smaller range than double, every float value is representable as a double, but not vice versa. So casting from float to double is value-preserving, but casting from double to float will change all values needing more than 24 bits of precision.

    The cast is generally performed (for values in the float range) by rounding the significand of the double value to 24 bits of precision in the following way:

    • if the 25th most significant bit of the significand is 0, the significand is truncated (the value is rounded towards zero)
    • if the 25th most significant bit is 1, and not all bits of lower significance are 0, the value is rounded away from zero (the significand is truncated and then the value of the lest significant bit is added)
    • otherwise, the significand is rounded so tat the 24th most significant bit is zero, that rounds away from zero in half the cases and towards zero in half.

    You cannot predict if casting a double to float increases or decreases the value by looking at the decimal expansion, except in the few cases where you can see that the value will be unchanged. It’s the binary expansion that matters.

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

Sidebar

Related Questions

Consider the following code: #include <stdio.h> int main(void) { int a[10]; printf(%d,(int)sizeof(a)); //prints 10*sizeof(int)
Consider the following code: #include <stdio.h> int main() { printf(%d, 300 * 300 /
Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)
Consider the following code which shows compile time error : #include <stdio.h> int main(int
Consider the following code: #include <stdio.h> #include <ctype.h> char* Mstrupr(char* szCad); int main() {
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following code: #include <stdio.h> int aaa(char *f, ...) { putchar(*f); return 0;
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include <iostream> #include <vector> #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> int main()
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int

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.