473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

comparing floating point numbers

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
9 2710
weaknessforcats
9,208 Expert Mod 8TB
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
1,251 Expert 1GB
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
9,065 Expert Mod 8TB
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
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
1,251 Expert 1GB
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
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
14
by: nw | last post by:
Hi, I'd like to compare 2 floating point numbers within a given error. I'd rather not use a absolute error but one related to the number of values that can be represented between the two floats....
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
18
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
5
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.