double and float......

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 4
Reputation: vinaychalluru is an unknown quantity at this point 
Solved Threads: 0
vinaychalluru's Avatar
vinaychalluru vinaychalluru is offline Offline
Newbie Poster

double and float......

 
0
  #1
Oct 25th, 2007
Hi,every one.I am new to C language and there I had tried a program as here and I got an output from what I had expected and hope you will help in this aspect and I have given the program as an attachment for better viewing of it.


thank you
vinaychalluru
Attached Files
File Type: txt my program.txt (517 Bytes, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: double and float......

 
0
  #2
Oct 25th, 2007
It's better to put the code directly in your post if it's short. The problem is that floating-point values aren't always exact, so you can't reliably test for equality. The usual fix for your problem is to do a "close enough" test using the difference of the two values and a suitably small epsilon value:
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. static double floating_epsilon ( int precision )
  5. {
  6. double result = 0.1;
  7.  
  8. while ( --precision >= 0 )
  9. result /= 10;
  10.  
  11. return result;
  12. }
  13.  
  14. int main ( void )
  15. {
  16. float a = 1.1;
  17. double b = 1.1;
  18.  
  19. if ( fabs ( a - b ) <= floating_epsilon ( 1 ) )
  20. printf("Both are equal\n");
  21. else
  22. printf("Both of them are not equal\n");
  23.  
  24. return 0;
  25. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC