Connecting Tech Pros Worldwide Forums | Help | Site Map

This

Partender
Guest
 
Posts: n/a
#1: Sep 19 '06
Why can i compare 2 floats ?
if float a==float b print equal


Marcus Kwok
Guest
 
Posts: n/a
#2: Sep 19 '06

re: This


Partender <savia755@latinmail.comwrote:
Quote:
Why can i compare 2 floats ?
if float a==float b print equal
Please see:
http://www.parashift.com/c++-faq-lit...html#faq-29.17

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Michael
Guest
 
Posts: n/a
#3: Sep 19 '06

re: This


Why can i compare 2 floats ?
Quote:
if float a==float b print equal
Typically this is due to the limits of numeric accuracy. For example,
in math class, 1.0 = .9999999(repeating), but in any finite
representation, these would be different. And since computers use
binary, even numbers like 1/5 have representations with repeating
decimals. So basically, a small round-off error or something like that
will get you.

The way you solve this is:
#include <math.h>
const double THRESHOLD = 1e-10; // or pick some other small value
if (fabs(a - b) < THRESHOLD) {
print equal;
}

Partender
Guest
 
Posts: n/a
#4: Sep 19 '06

re: This


Thanks Micheal and Marcus

I think I will try to again assemble my old hardware I used to lose and
throw away. No matter how old it is, it works good with my system. Very
nice I truly miss

Phlip
Guest
 
Posts: n/a
#5: Sep 19 '06

re: This


Partender wrote:
Quote:
I think I will try to again assemble my old hardware I used to lose and
throw away. No matter how old it is, it works good with my system. Very
nice I truly miss
Are you implying you will reassemble hardware where a program that compares
to floats for equality works correctly? Why not fix the floats?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!



Partender
Guest
 
Posts: n/a
#6: Sep 19 '06

re: This



Phlip เขียน:
Quote:
Partender wrote:
>
Quote:
I think I will try to again assemble my old hardware I used to lose and
throw away. No matter how old it is, it works good with my system. Very
nice I truly miss
>
Are you implying you will reassemble hardware where a program that compares
to floats for equality works correctly? Why not fix the floats?
>
Yes, I will but not now
Assembling a system has been my dream for years, old systems are easier
to work with than the modern ones.

shadowman615@comcast.net
Guest
 
Posts: n/a
#7: Sep 19 '06

re: This



Partender wrote:
Quote:
Phlip เขียน:
Quote:
Partender wrote:
Quote:
I think I will try to again assemble my old hardware I used to lose and
throw away. No matter how old it is, it works good with my system. Very
nice I truly miss
Are you implying you will reassemble hardware where a program that compares
to floats for equality works correctly? Why not fix the floats?
>
Yes, I will but not now
Assembling a system has been my dream for years, old systems are easier
to work with than the modern ones.
Heh. If google groups is accurate, this is the same guy who calls
himself JJ in this thread:

http://groups.google.com/group/comp....0e4ba0637a7416

Jerry Coffin
Guest
 
Posts: n/a
#8: Sep 19 '06

re: This


In article <1158686120.064738.6930@i42g2000cwa.googlegroups.c om>,
savia755@latinmail.com says...
Quote:
Why can i compare 2 floats ?
if float a==float b print equal
Unfortunately, most of the information in the FAQ on the sibject is
somewhere between misleading and downright wrong.

http://tinyurl.com/qcmyd

Shows one way to do floating point comparisons reasonably well, with the
proviso that it should really be renamed to reflect the fact that it
measures approximate equality.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Marcus Kwok
Guest
 
Posts: n/a
#9: Sep 19 '06

re: This


Jerry Coffin <jcoffin@taeus.comwrote:
Quote:
In article <1158686120.064738.6930@i42g2000cwa.googlegroups.c om>,
savia755@latinmail.com says...
Quote:
>Why can i compare 2 floats ?
>if float a==float b print equal
>
Unfortunately, most of the information in the FAQ on the sibject is
somewhere between misleading and downright wrong.
>
http://tinyurl.com/qcmyd
>
Shows one way to do floating point comparisons reasonably well, with the
proviso that it should really be renamed to reflect the fact that it
measures approximate equality.
I was able to dig up this other article that has a few different
methods:

http://www.adtmag.com/joop/carticle.aspx?id=396

Of course, there is also the classic paper "What Every Computer
Scientist Should Know About Floating-Point Arithmetic" by David
Goldberg:

http://docs.sun.com/source/806-3568/...dberg.html#674

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
scott urban
Guest
 
Posts: n/a
#10: Sep 29 '06

re: This


Please us a post subject that describes the topic of you post.

On 2006-09-19, Partender <savia755@latinmail.comwrote:
Quote:
Why can i compare 2 floats ?
if float a==float b print equal
>
Closed Thread