?
Solved

Comparing 2 numbers of type "double" in C

Posted on 2005-03-31
7
Medium Priority
?
410 Views
Last Modified: 2010-04-15
Hi,
I have following code snippet. When i run it it gives strange result.

double Amount1=0,Amount2=720.05;
      Amount1=65.45;
      Amount1+=60.00;
      Amount1+=579.60;
      Amount1+=15.00;
      if(Amount1>Amount2)
            printf("Amount1 is greater than Amount2\n",f1,f2);
      else
            printf("The Amounts are equal.");

Expected Ans is: The Amounts are equal.

But it is giving result as "Amount1 is greater than Amount2"
Actually both amounts have same value - 720.05. Then how C treats Amount1 greater than Amount2?

Can some one explain why this is happening & what's the solution to get correct result?
0
Comment
Question by:payalpr2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13678513
binary decimal mathematics is not an exact science. So, some values that are exact in decimal form, are not exact when machine converts to a binary. In fact, fractional  portion of a number in binary is the sum of fractions of powers of 2

3.5  ---> 11.1  (3 + .5)
3.75 ---> 11.11  (3 + .5 + .25)
3.60 ----> 11.10011....  (3 + .5 + .0625 + 0.03125 + ....continues)

So, when converted back again to decimal, it is not the exact number. This error is increased when some operation are made to this number.
If you want no precision loss with few fractional digits, you can round your number to a fixed number of digits, before comparing.
0
 

Author Comment

by:payalpr2002
ID: 13679053
How to round a decimal number to a fixed number of digits before doing the comparison? i think it will then compare properly & i will get correct result.
0
 

Author Comment

by:payalpr2002
ID: 13679109
Can u suggest me a good link which will explain the binary decimal mathematics - addition,comparison & how the decimal numbers are stored in memory?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 13679894
> How to round a decimal number to a fixed number of digits before doing the comparison? i think it will then compare properly & i will get correct result.

You can multiply both numbers by 100 before comparing:

if ( ((int)(a*100.0)) == ((int)(b*100.0)) {
   .....
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13679913
0
 
LVL 1

Expert Comment

by:amar_r
ID: 13680010

 try by type casting.
 
   if((float)Amount1>Amount2)
0
 

Author Comment

by:payalpr2002
ID: 13680236
>You can multiply both numbers by 100 before comparing:

if ( ((int)(a*100.0)) == ((int)(b*100.0)) {
   .....
}


This is really a good solution - very simple but works perfectly fine.
Thanks a lot!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question