Not Yet Answered # Double type problem in C

IsharaComix 97 Discussion Starter karthiken07 N1GHTS 102 Discussion Starter karthiken07 N1GHTS 102

0

Can any one give me an idea for compare these two values in double data type.

I hope this if check always gets FALSE, Unfortunately It is TRUE condition. IF you know any solution,Plz suggest to me.

```
# include <stdio.h>
int main()
{
double g = 51.200000000000007;
double h = 51.200000000000001;
if((g)>(h))
{
printf("%lf",h);
}
g=g;
h=h;
printf("%lf",g);
return 0;
}
```

Output:

51.200000

51.200000

*Edited 3 Years Ago by Dani*: Formatting fixed

0

Double variables just don't have that much precision. If you need numbers that are that fine-tuned, you need to look for an "arbitrary/multiple precision" library - something like GMP http://gmplib.org/

Hope this helps.

0

Thanks to your support.

I need Clarity of answer like only 51.2 ..

Input : 51.2 i was given , But compiler take it as a 51.200000006.

For the next time, 51.200000001

After comparing these values its not equal . But it should be equal ...

Hope everyone understand my problem,

0

Made this in a hurry, but it should work. Requires the math header. Basically you take the numbers you want to compare, multiply them by 10 to the power of your precision, trim them by casting to int, do your comparison, and your done.

```
inline int CompareDoubles(double A, double B, int Precision) {
int a = (int)(A * pow(10, Precision));
int b = (int)(B * pow(10, Precision));
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
```

So using your example, here is the usage.

```
int main()
{
double g = 51.200000000000007;
double h = 51.200000000000001;
if(CompareDoubles(g,h,1) > 0) // This will only compare "51.2", ignoring the rest
printf("%lf",h);
g=g;
h=h;
printf("%lf",g);
return 0;
}
```

0

Thanks NIGHTS, But my problem is like below two values should matches ....

Is there any header files wants to include. like float.h ..

i m working with gcc compiler , eclipse c editor,, Is it necessary to move other compiler...

```
double g = 9.5999999999999;
double h = 9.6000000000001;
```

0

Oops, your right. Sorry about that. Here you go...

```
inline int CompareDoubles(double A, double B, int Precision) {
int a = (int)round(A * pow(10, Precision));
int b = (int)round(B * pow(10, Precision));
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
```

So now, before truncating the end digits, it will round the numbers to the nearest given precision. That should work, right?

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`