LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2006, 03:26 PM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
Please explain me this - trouble with float


I want to see if a*b=1, that is a=1/b
a and b are float
if (a*b==1)
never seems to work (for, let's say a=1.0/2.0 and b=2.0)
Why is that?
 
Old 02-20-2006, 03:46 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
Don't know, it works for me using C programming...

My only thoughts are you forgot to include the math library or you forgot to define the variables as float or double...
 
Old 02-20-2006, 03:53 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by cdog
I want to see if a*b=1, that is a=1/b
a and b are float
if (a*b==1)
never seems to work (for, let's say a=1.0/2.0 and b=2.0)
Why is that?
What exactly do you mean with "never seems to work". What happens? Does it compile and link without errors and warnings? Does it ouput any errors? Or does it give you wrong answers? What is the code you tried?
 
Old 02-20-2006, 04:06 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by cdog
I want to see if a*b=1, that is a=1/b
a and b are float
if (a*b==1)
never seems to work (for, let's say a=1.0/2.0 and b=2.0)
Why is that?
this is a better way of checking for a float value.
Code:
const float epsilon = 0.00001f;

if(a*b < 1+epsilon && a*b > 1-epsilon)
<edit>a*b may not be cached( although it probably is) so an even better method maybe
Code:
float c = a*b;
if(c < 1+epsilon && c >1-epsilon)
If you want an explanation of why, then check out something like:
http://www.math.grin.edu/~stone/cour...EEE-reals.html
or one of other million web pages about the same issue.

Last edited by dmail; 02-20-2006 at 04:28 PM.
 
Old 02-20-2006, 04:08 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
With floating point numbers, your result may be 1.9999999999999 or something similar. A floating point fraction (base 10) may not be exactly convertable to a base 2 floating point number.

Instead of an equality test for floating point numbers you should check if the difference is below a small delta value.
For example, if you use a floating point variable to control a for loop and you test for equality to terminate the root, then the loop may not end.
 
Old 02-20-2006, 04:35 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
First off -
It's standard (ie., best practice) to use these comparison macros for floats and doubles (floating point numbers):
Code:
isgreater(x,y)       
isgreaterequal(x,y)  
isless(x,y)          
islessequal(x,y)     
islessgreater(x,y)
These are in math.h - Depending on your system there are others like isunordered and so on.

The suggestion of rolling your own epsilon, while okay, still has some problems - which is why these macros came about. A very long time ago.
 
Old 02-20-2006, 05:46 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
The use of these macros is advisable, are iso standard and maybe considered best practice but windows maths header does not include them; I normally code on windows (because theres just is not an ide which compares to visual studio in my opinion) and therefore use the above method rather than the macros. As I want my apps to work on both windows and nix.
 
Old 02-20-2006, 07:09 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Perhaps you can check if the macros are defined and if not roll your own macros.
 
Old 02-21-2006, 02:09 AM   #9
geek_comp
LQ Newbie
 
Registered: Feb 2006
Location: Hamirpur
Distribution: Fedora, Suse, Debian
Posts: 6

Rep: Reputation: 0
Post Regarding float comparison in if loop

dmail is almost to the point.

The storage data format for float in memory consists of integer data and its precision information. Thats why it doesnt works as it seems to be in " if(condition)" , because this loops works well only for integer values or binary values. In case of float this loop compares the precision values and gives unexpected results, sometimes it works.
better way for float comparision is to first separate the integer and data parts from the float byte.

Regards

geek
 
Old 02-21-2006, 02:32 PM   #10
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
thank you guys. I figured that out alone. the problem was that it never is exactly 1 when it comes to float.
thanks again. nice responces
 
Old 02-21-2006, 03:00 PM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The same question also came up in this thread:
http://www.linuxquestions.org/questi...d.php?t=416939
 
Old 02-22-2006, 02:09 PM   #12
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Just do this:
Code:
#define FLOAT_EQUAL(X, Y) (X < (Y+epsilon) && X > (Y-epsilon))
then you can:
Code:
float x, y;
if (FLOAT_EQUAL(x, y)) DoSomething();
 
Old 02-22-2006, 03:55 PM   #13
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Quote:
Originally Posted by dmail
The use of these macros is advisable, are iso standard and maybe considered best practice but windows maths header does not include them; I normally code on windows (because theres just is not an ide which compares to visual studio in my opinion) and therefore use the above method rather than the macros. As I want my apps to work on both windows and nix.

There's vi ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C++] char * to float Ossar Programming 2 08-03-2005 10:28 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
please explain me why there is so much of trouble in installing any software in linux amolgupta Programming 24 05-10-2005 04:56 PM
How to use a float in a script. philipina Programming 4 03-18-2004 08:06 AM
C newbie float ? bluesky Programming 7 09-03-2003 12:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration