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

This

P: n/a
Why can i compare 2 floats ?
if float a==float b print equal

Sep 19 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Partender <sa******@latinmail.comwrote:
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
Sep 19 '06 #2

P: n/a
Why can i compare 2 floats ?
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;
}

Sep 19 '06 #3

P: n/a
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

Sep 19 '06 #4

P: n/a
Partender wrote:
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!!!

Sep 19 '06 #5

P: n/a

Phlip เขียน:
Partender wrote:
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.

Sep 19 '06 #6

P: n/a

Partender wrote:
Phlip เขียน:
Partender wrote:
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

Sep 19 '06 #7

P: n/a
In article <11********************@i42g2000cwa.googlegroups.c om>,
sa******@latinmail.com says...
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.
Sep 19 '06 #8

P: n/a
Jerry Coffin <jc*****@taeus.comwrote:
In article <11********************@i42g2000cwa.googlegroups.c om>,
sa******@latinmail.com says...
>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
Sep 19 '06 #9

P: n/a
Please us a post subject that describes the topic of you post.

On 2006-09-19, Partender <sa******@latinmail.comwrote:
Why can i compare 2 floats ?
if float a==float b print equal
Sep 29 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.