Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Same Float Values In C [duplicate]

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.

like image 301
gursahib.singh.sahni Avatar asked Mar 24 '12 09:03

gursahib.singh.sahni


People also ask

Can we compare float with double?

Since the precision of float is less than the double therefore after a certain point(23 in float and 52 in double) it would truncate the result. Hence, after promotion of float into double(at the time of comparison) compiler will pad the remaining bits with zeroes.

Can we use == to compare two float values?

You cant compare two floats with a == sign, if that is what you were hoping for. The trick is to have another float variable, epsilon of value say 10^-6 and then check whether the difference of the two floats lies between -epsilon to epsilon or not.

Can we compare float values in C?

Master C and Embedded C Programming- Learn as you goTo compare two floating point values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.

How do you compare two float values?

The compare() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Parameters: The function accepts two parameters: f1: The first float value to be compared.


1 Answers

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.
like image 86
paxdiablo Avatar answered Oct 03 '22 06:10

paxdiablo