943,534 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 699
  • C RSS
Oct 25th, 2007
0

double and float......

Expand Post »
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, 18 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vinaychalluru is offline Offline
6 posts
since Sep 2007
Oct 25th, 2007
0

Re: double and float......

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. }
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: converting string[][] to float[]
Next Thread in C Forum Timeline: subtract 2 signed bianry numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC