By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,671 Members | 1,624 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,671 IT Pros & Developers. It's quick & easy.

comparing floating point numbers

P: 3
i am posting codes and they give striking results can someone please explain why ???

Expand|Select|Wrap|Line Numbers
  1. main() 
  2. { float a=0.7 ; 
  3.   if (a<0.7) 
  4.      { printf("c")  ; 
  5.      } 
  6. else 
  7. printf("c++") ;
  8.  
OUTPUT is c
Expand|Select|Wrap|Line Numbers
  1. main() 
  2. { float a=0.7 ; 
  3.   if (a<0.7f) 
  4.      { printf("c")  ; 
  5.      } 
  6. else 
  7. printf("c++") ;
OUTPUT : c++

WHY I know in the first case 0.7 is a double and second it is taken as a float ....so wht ???
Jun 3 '07 #1
Share this Question
Share on Google+
9 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You can't compare floating point numbers using the common <,>, ==,etc, operators. Due to rounding floating point numbers that are close in value may trigger the wrong result from these operators.

Usually, you have to work withe th absolute value and do a comparsion within a sigma deviation that you specify.

Your can Google "floating point arithmetic" and get a ton of advice.

Also, a float is 4 bytes whereas a double is 8. Assigning a double to a float will produce truncation and loss of data. Do not mix these types.

I recommend that unless you have a real technical reason that you can write down on paper, you should stick with doubles everywhere. float is an obsolete type from the days of computers with small memories. float is in C++ only so relic C code willl still compile.
Jun 3 '07 #2

AdrianH
Expert 100+
P: 1,251
You can't compare floating point numbers using the common <,>, ==,etc, operators. Due to rounding floating point numbers that are close in value may trigger the wrong result from these operators.

Usually, you have to work withe th absolute value and do a comparsion within a sigma deviation that you specify.

Your can Google "floating point arithmetic" and get a ton of advice.

Also, a float is 4 bytes whereas a double is 8. Assigning a double to a float will produce truncation and loss of data. Do not mix these types.

I recommend that unless you have a real technical reason that you can write down on paper, you should stick with doubles everywhere. float is an obsolete type from the days of computers with small memories. float is in C++ only so relic C code willl still compile.
float may still be used in real-time embedded systems with little memory, but as wfc said, it is mostly not very useful to use.


Adrian
Jun 3 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
float may still be used in real-time embedded systems with little memory, but as wfc said, it is mostly not very useful to use.
I was going to make the same argument, however in most of the real-time embedded systems I have worked on we have avoided both float and double like the plague (not good for performance or memory usage as it tends to drag in a large maths library).

I have to say that has not been a particular problem there are several tricks you can use in order to avoid there use. Integer storing units of 1 hundredth, or to ints 1 numerator 1 demoninator are both schemes I have used.

I agree though, if you don't have a good reason use double.
Jun 3 '07 #4

dumparun
P: 26
I have to say that has not been a particular problem there are several tricks you can use in order to avoid there use. Integer storing units of 1 hundredth, or to ints 1 numerator 1 demoninator are both schemes I have used.
Off topic though, could you please explain this
Jun 4 '07 #5

AdrianH
Expert 100+
P: 1,251
Off topic though, could you please explain this
Basicly, he is using integer arithmetic in a way as to bypass the need to use a floating point number.

Expand|Select|Wrap|Line Numbers
  1. int x = 123; // really means x=12.3 if you say that x is actually 1/10th of what is stored.
  2.  
  3. // this requires supporting functions.
  4. struct fraction {
  5.   int n = 5;  // numerator
  6.   int d = 3; // denomonator
  7. };
  8.  

Adrian
Jun 4 '07 #6

dumparun
P: 26
Basicly, he is using integer arithmetic in a way as to bypass the need to use a floating point number.

Expand|Select|Wrap|Line Numbers
  1. int x = 123; // really means x=12.3 if you say that x is actually 1/10th of what is stored.
  2.  
  3. // this requires supporting functions.
  4. struct fraction {
  5.   int n = 5;  // numerator
  6.   int d = 3; // denomonator
  7. };
  8.  

Adrian
Ok... Got it right :)
Jun 5 '07 #7

Expert 10K+
P: 11,448

P: 3
well i found the ans wer nd its not wht u all r saying .....floats or doubles can be compared using >= etc the ans lies in the internal representation of floats and doubles
float is represented using IEEE 754 single precision
double by IEEE 754 double precision so .7>.7f and so on.....thnx newayz
Jun 6 '07 #9

Banfa
Expert Mod 5K+
P: 8,916
well i found the ans wer nd its not wht u all r saying .....floats or doubles can be compared using >= etc the ans lies in the internal representation of floats and doubles
float is represented using IEEE 754 single precision
double by IEEE 754 double precision so .7>.7f and so on.....thnx newayz
Can't was probably the wrong word to use you can use all the comparison operators on doubles and floats

==
!=
>
<
>=
<=

However using == and != is virtually pointless as after any calculations have been performed it is unlikely that any 2 given floats will that approximate the same value will actually have the same value so typically == will always return false and != will always return true making them fairly useless.

Using > < >= and <= is OK as long as you when you do so you are fully aware that if you test <0.7 if you variable has the value 0.700000000000001 than the test will return false.


I hope you realise that "IEEE 754 single precision double by IEEE 754 double precision so .7>.7f " does not mean that for every approximation of a given number in single and double precision then the double precision representation is bigger than the single precision representation, that is

x > xf

is not true for all x
Jun 6 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.