Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quasi 0

49 views
Skip to first unread message

David RF

unread,
Jun 4, 2012, 5:32:06 AM6/4/12
to
Hi friends:
Machine epsilon is the maximum relative error of the chosen rounding
procedure

#include <stdio.h>
#include <math.h>
#include <float.h>

int main(void)
{
double a = 0.1;
double b = 0.1;

a += 1.0;
a -= 1.0;

printf("a == b = %s\n", a == b ? "equals" : "unequals");
printf("a == b = %s\n", fabs(a - b) < DBL_EPSILON ? "equals" :
"unequals");
return 0;
}

Is this a good method for test the equality?


Noob

unread,
Jun 4, 2012, 5:56:13 AM6/4/12
to
David RF wrote:

> Is this a good method for test the equality?

If you're bored on a rainy afternoon, you can read David Goldberg's
looong article on FP arithmetic.

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

David RF

unread,
Jun 4, 2012, 6:05:35 AM6/4/12
to
Can you link to someone else's website where formulas do not mix
letters and numbers? :)
Message has been deleted

Eric Sosman

unread,
Jun 4, 2012, 6:51:00 AM6/4/12
to
No.

Rounding adds or subtracts an amount that is proportional to
the magnitude of the rounded number. DBL_EPSILON is scaled for
numbers near +-1, but is of completely the wrong size for numbers
around +-1E30 or +-1E-30. DBL_EPSILON is about 1E-16 on many
machines, so consider the values 1E-20 and 1E-30: They differ
by much less than the typical DBL_EPSILON, yet one is ten billion
times the other. Do you want to consider a ten billion-fold
discrepancy "equal?"

--
Eric Sosman
eso...@ieee-dot-org.invalid

David RF

unread,
Jun 4, 2012, 6:56:41 AM6/4/12
to
On 4 jun, 12:51, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> El 04/06/2012 05:32 AM, David RF escribió:
>
>
>
>
>
>
>
> > Hola amigos:
> Epsilon> de la máquina es el máximo error relativo de redondeo elegido
> > Procedimiento de
>
> > # Include
> > # Include <math.h>
> > # Include <float.h>
>
> > Int main (void)
> > {
> > Doble a = 0,1;
> > Doble b = 0,1;
>
> > A + = 1.0;
> > A - = 1,0;
>
> > Printf ("a == b =% s ​​\ n", un "igual" == b: "desiguales"?);
> > Printf ("a == b =% s ​​\ n", fábricas (a - b) <DBL_EPSILON "iguales"?
> > "Desiguales");
> > Return 0;
> >}
>
> > ¿Es este un buen método para la prueba de la igualdad?
>
> No.
>
> Completan suma o resta una cantidad que es proporcional a
> la magnitud del número redondeado. DBL_EPSILON se escala para la
> números de cerca de + -1, pero es por completo de la talla equivocada de los números
> alrededor de + o +-1e30-1E-30. DBL_EPSILON se trata de 1E-16 en muchosmáquinas, a fin de considerar los valores 1E-20 y 1E-30: Se diferencian
> por mucho menos que el DBL_EPSILON típica, sin embargo, uno es diez mil millones
> veces el otro. ¿Quieres tener en cuenta un niño de diez mil millones de veces
> discrepancia "iguales?"

Thanks, I get the point

BartC

unread,
Jun 4, 2012, 7:08:18 AM6/4/12
to


"David RF" <davr...@gmail.com> wrote in message
news:bfad6935-b012-44b5...@t8g2000yqd.googlegroups.com...
What happened there?

--
Bartc

James Kuyper

unread,
Jun 4, 2012, 7:10:24 AM6/4/12
to
Such formulas are the clearest and most natural way to describe the
concepts being discussed. If your mathematical background is not
sufficiently advanced for you to be able to understand such formulas,
it's also insufficient to understand the corresponding concepts. You
should wait until you've learned some algebra before attempting to read
things like this. If you've already tried to learn algebra, and came
away with an aversion to it, you should consider avoiding technically
oriented career paths such as computer programming.
--
James Kuyper

David RF

unread,
Jun 4, 2012, 7:14:02 AM6/4/12
to
On 4 jun, 13:08, "BartC" <b...@freeuk.com> wrote:
> "David RF" <davran...@gmail.com> wrote in message
Oops, excuse me, I replied to Eric after using the translator

James Dow Allen

unread,
Jun 4, 2012, 7:25:21 AM6/4/12
to
On Jun 4, 5:56 pm, David RF <davran...@gmail.com> wrote:
> On June 4, 12:51, Eric Sosman <those...@ ieee-dot-org.invalid> wrote:
>
>
>
>
>
>
>
> > On 04/06/2012 5:32 a.m., David RF wrote:
>
> >> Hello friends:
> > Epsilon> machine is the maximum relative error of rounding chosen
> Procedure >>
>
> >> # Include# Include <math.h> >>
> # Include <float.h> >>
>
> >> Int main (void)
> {>>
> Turn >> = 0.1;
> Double >> b = 0.1;
>
> >> A + = 1.0;
> >> A - = 1.0;
>
> >> Printf ("a == b =% s ​​\ n", an "equal" == b "unequal"?)
> >> Printf ("a == b =% s ​​\ n", factories (a - b) <DBL_EPSILON "equal"?
> >> "Unequal");
> >> Return 0;
> >>}
>
> >> Is this a good method to test for equality?
>
> > No.
>
> > Rounding adds or subtracts an amount which is proportional to
> > The sheer numbers rounded. DBL_EPSILON is scaled to the
> > Numbers of about + -1, but it is completely the wrong size of the numbers
> > About + or +-1e30-1E-30. DBL_EPSILON is muchosmáquinas 1E-16 in order to consider the values ​​1E-1E-20 and 30: They differ
> > For much less than the typical DBL_EPSILON, however, one is ten billion
> > Times the other. Want to take account of a ten billion times
> > Discrepancy "equal?"
>
> Thanks, I get the point

This is in reply to BartC, who asks "What happened?"

I respond to David's post, first clicking "Translate to English."
The post now has less Spanish than before (though still doesn't
look like Eric's post).

And this was with *Old* Google Groups.
I've not yet dared to learn what wondrous features
*New* Google Groups has!! ::slaps face with hand::

James

David RF

unread,
Jun 4, 2012, 7:48:12 AM6/4/12
to
I'm asking for the simplest way to detect round errors due to binary
representation
of floating numbers. learn algebra? career? you assume too much

BartC

unread,
Jun 4, 2012, 7:53:48 AM6/4/12
to
"Eric Sosman" <eso...@ieee-dot-org.invalid> wrote in message
news:jqi3ur$u1d$1...@dont-email.me...
> On 6/4/2012 5:32 AM, David RF wrote:

>> Is this a good method for test the equality?
>
> No.

>consider the values 1E-20 and 1E-30: They differ
> by much less than the typical DBL_EPSILON, yet one is ten billion
> times the other. Do you want to consider a ten billion-fold
> discrepancy "equal?"

I remember having a similar conversation with my bank: I was suggesting that
a 0.1% interest rate was, to all intents and purposes, a zero interest rate;
they insisted on still calling it an interest-bearing account.

Of course, from their point of view, it was still returning an infinitely
higher amount of interest than the zero-rated one, even after tax.

--
Bartc

James Kuyper

unread,
Jun 4, 2012, 7:55:17 AM6/4/12
to
Not particularly.

First of all, it's not a test for equality, that's simply done in C by
the == operator. It's a test for near-equality. It's seldom correct to
test floating point values for equality with each other; it's sometimes
appropriate to compare them for approximate equality, but you should
always be sure that it actually is appropriate.

The general form for conducting approximate equality tests is

fabs(a-b) < epsilon

where epsilon will, in general, have different values for different
comparisons.

You correctly described machine epsilon as describing a relative error.
That means that you should not use DBL_EPSILON directly. If the only
source of error in you calculations was a single floating point roundoff
error, you should scale it according to the numbers being compared, by
multiplying it by fabs(a) or fabs(b), whichever is larger.

If multiple floating point errors are involved (as, for instance, with
a+=1.0 and a-=1.0 in your example), then the calculation of the
appropriate value for epsilon gets more complicated. If any of the
quantities you compare are measurements, or numbers calculated from
measurements, measurement error is likely to be much larger than
floating point round-off error.

There's a subject called "propagation of errors" or "propagation of
uncertainty), which is essentially devoted to determining the correct
way of calculating the appropriate value to use for epsilon in such
contexts. The corresponding wikipedia page is at least as good a place
to start learning about that subject as any other.
--
James Kuyper

David RF

unread,
Jun 4, 2012, 7:56:26 AM6/4/12
to
representation of floating point numbers, excuse my poor english

David RF

unread,
Jun 4, 2012, 7:59:27 AM6/4/12
to
On 4 jun, 13:55, James Kuyper <jameskuy...@verizon.net> wrote:

[snip ...]
> If multiple floating point errors are involved (as, for instance, with
> a+=1.0 and a-=1.0 in your example), then the calculation of the
> appropriate value for epsilon gets more complicated. If any of the
> quantities you compare are measurements, or numbers calculated from
> measurements, measurement error is likely to be much larger than
> floating point round-off error.

Thanks

BartC

unread,
Jun 4, 2012, 8:05:15 AM6/4/12
to
"David RF" <davr...@gmail.com> wrote in message
news:2a148e85-d20a-4500...@c19g2000yqk.googlegroups.com...
The simplest way is to test whether the absolute difference between two
numbers, is less than some threshold such as 0.0001.

What the best value of that should be, and whether having a fixed limit at
all would work, depends on your application.

If you want to test whether two results are equal, allowing for minor
rounding errors, then that's a little different, especially if possible
results cover a wide range of magnitudes. Here you really want to ignore the
last few bits of the floating point representation, but it gets tricky to do
and you need a bit of background.

--
Bartc

James Kuyper

unread,
Jun 4, 2012, 8:05:25 AM6/4/12
to
On 06/04/2012 07:48 AM, David RF wrote:
> On 4 jun, 13:10, James Kuyper <jameskuy...@verizon.net> wrote:
>> On 06/04/2012 06:05 AM, David RF wrote:
...
>>> Can you link to someone else's website where formulas do not mix
>>> letters and numbers? :)
>>
>> Such formulas are the clearest and most natural way to describe the
>> concepts being discussed. If your mathematical background is not
>> sufficiently advanced for you to be able to understand such formulas,
>> it's also insufficient to understand the corresponding concepts. You
>> should wait until you've learned some algebra before attempting to read
>> things like this. If you've already tried to learn algebra, and came
>> away with an aversion to it, you should consider avoiding technically
>> oriented career paths such as computer programming.
>
> I'm asking for the simplest way to detect round errors due to binary
> representation
> of floating numbers. ...

While your original question was about dealing with floating point
round-off errors, I responded to that question in a different message.
In this message, I was responding only to your reaction to discovering
the use of algebra in David Goldberg's article.

> ...learn algebra? career? you assume too much

No, I was rather careful to write my message to avoid making those
assumptions; I didn't assume you were planning a technically-oriented
career - I merely advised against pursing one if you found algebra too
difficult. I didn't assume that you'd already tried and failed to learn
algebra, I deliberately worded it to be equally appropriate whether or
not you'd tried to learn algebra yet.

I did assume that you needed to learn algebra to correctly understand
propagation of error. Only the very simplest concepts in that subject
can be explained without it; not enough to be useful, just enough to be
dangerous.
--
James Kuyper

Keith Thompson

unread,
Jun 4, 2012, 5:55:59 PM6/4/12
to
Ideally, the nature of the test for near-equality needs to reflect the
nature of the computation(s) that created the error.

A simple test like

abs(x - y) <= 0.0001

is sensible only in limited circumstances. Sometimes it makes sense to
test for an absolute difference, sometimes it makes sense to test the
ratio, and sometimes you really need something more complicated.

The question you're trying to answer is whether the discrepancy between
two floating-point values reflects an actual mathematical difference or
a rounding error. Determining that, even imperfectly, requires an
understanding of what errors the computation(s) *could* have produced.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

nick_keigh...@hotmail.com

unread,
Jun 5, 2012, 6:54:20 AM6/5/12
to
On Monday, June 4, 2012 12:10:24 PM UTC+1, James Kuyper wrote:
> On 06/04/2012 06:05 AM, David RF wrote:
> > On 4 jun, 11:56, Noob <r...@127.0.0.1> wrote:
> >> David RF wrote:

> >> If you're bored on a rainy afternoon, you can read David Goldberg's
> >> looong article on FP arithmetic.
> >>
> >> http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

excellent stuff. I must re-read it.

> > Can you link to someone else's website where formulas do not mix
> > letters and numbers? :)
>
> Such formulas are the clearest and most natural way to describe the
> concepts being discussed. If your mathematical background is not
> sufficiently advanced for you to be able to understand such formulas,
> it's also insufficient to understand the corresponding concepts. You
> should wait until you've learned some algebra before attempting to read
> things like this.

doesn't everyone's education include basic algebra?

> If you've already tried to learn algebra, and came
> away with an aversion to it,

I never quite understood this, though I've encounterd it. It's like there's a conceptual hole in their brain so that they can't take the abstraction step to encompass algebra. Actual neural architecture or just bad teaching?

> you should consider avoiding technically
> oriented career paths such as computer programming.

I'm not sure i'd go that far. you can do quite a lot without algebra. Though floating point arithmatic sounds to be pushing it!

James Kuyper

unread,
Jun 5, 2012, 8:07:32 AM6/5/12
to
On 06/05/2012 06:54 AM, nick_keigh...@hotmail.com wrote:
> On Monday, June 4, 2012 12:10:24 PM UTC+1, James Kuyper wrote:
>> On 06/04/2012 06:05 AM, David RF wrote:
>>> On 4 jun, 11:56, Noob <r...@127.0.0.1> wrote:
...
>>>> http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
...
>>> Can you link to someone else's website where formulas do not mix
>>> letters and numbers? :)
>>
>> Such formulas are the clearest and most natural way to describe the
>> concepts being discussed. If your mathematical background is not
>> sufficiently advanced for you to be able to understand such formulas,
>> it's also insufficient to understand the corresponding concepts. You
>> should wait until you've learned some algebra before attempting to read
>> things like this.
>
> doesn't everyone's education include basic algebra?

People as young as six are learning to program nowadays; possibly
younger. The simplest explanation for his desire for "formulas [that] do
not mix letters and numbers" is that he hasn't taken algebra yet. I'm
not assuming he's that young, but I did consider the possibility. The
complexity of the language in his messages is near the upper limit of
what I'd expect such a young person to be capable of, but it's not over
that limit. The second simplest explanation is that he took algebra, but
failed to master it.

>> If you've already tried to learn algebra, and came
>> away with an aversion to it,
>
> I never quite understood this, though I've encounterd it. It's like there's a conceptual hole in their brain so that they can't take the abstraction step to encompass algebra. Actual neural architecture or just bad teaching?

I used to tutor math and science for undergrads at community college.
The ones who needed tutoring were often taking math or science classes
to meet a requirement, rather than because they found them interesting.
I ran into all kinds of math phobia. As a math lover, I never fully
understood it, but I did develop some helpful techniques for explaining
things to such people.

>> you should consider avoiding technically
>> oriented career paths such as computer programming.
>
> I'm not sure i'd go that far. you can do quite a lot without algebra. Though floating point arithmatic sounds to be pushing it!

That why I inserted the weasel words "consider" and "avoid".
--
James Kuyper

Joe keane

unread,
Jun 5, 2012, 9:32:21 AM6/5/12
to
In article <020694a0-5010-4af2...@googlegroups.com>,
<nick_keigh...@hotmail.com> wrote:
>Actual neural architecture or just bad teaching?

the second one!

i used to [very long time ago] tutor kids

the kids are smart, it's the adults who are missing some knifes

i'm like go from basic algebra to understanding calculus in a few weeks

no prob, let's go
Message has been deleted
0 new messages