# Quasi 0

Discussion in 'C Programming' started by David RF, Jun 4, 2012.

1. ### David RFGuest

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?

David RF, Jun 4, 2012

2. ### NoobGuest

Noob, Jun 4, 2012

3. ### David RFGuest

On 4 jun, 11:56, Noob <r...@127.0.0.1> wrote:
> 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

Can you link to someone else's website where formulas do not mix
letters and numbers?

David RF, Jun 4, 2012
4. ### Stefan RamGuest

David RF <> writes:
>Is this a good method for test the equality?

To test equality of two double values, use »==«.
(Another question is when or whether this is appropriate.)

Stefan Ram, Jun 4, 2012
5. ### Eric SosmanGuest

On 6/4/2012 5:32 AM, David RF wrote:
> 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?

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
d

Eric Sosman, Jun 4, 2012
6. ### David RFGuest

On 4 jun, 12:51, Eric Sosman <> 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

David RF, Jun 4, 2012
7. ### BartCGuest

"David RF" <> wrote in message
news:...
> On 4 jun, 12:51, Eric Sosman <> 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

What happened there?

--
Bartc

BartC, Jun 4, 2012
8. ### James KuyperGuest

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:
>>> 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

>
> 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.
--
James Kuyper

James Kuyper, Jun 4, 2012
9. ### David RFGuest

On 4 jun, 13:08, "BartC" <> wrote:
> "David RF" <> wrote in message
>
> news:...
>
>
>
> > On 4 jun, 12:51, Eric Sosman <> 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

>
> What happened there?

Oops, excuse me, I replied to Eric after using the translator

David RF, Jun 4, 2012
10. ### James Dow AllenGuest

On Jun 4, 5:56Â pm, David RF <> 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

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

James Dow Allen, Jun 4, 2012
11. ### David RFGuest

On 4 jun, 13:10, 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:
> >>> 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.

>
>
> > 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. learn algebra? career? you assume too much

David RF, Jun 4, 2012
12. ### BartCGuest

"Eric Sosman" <> wrote in message
news:jqi3ur\$u1d\$...
> 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

BartC, Jun 4, 2012
13. ### James KuyperGuest

On 06/04/2012 05:32 AM, David RF wrote:
> 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?

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

James Kuyper, Jun 4, 2012
14. ### David RFGuest

On 4 jun, 13:48, David RF <> wrote:
> On 4 jun, 13:10, 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:
> > >>> 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.

>
>
> > > 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. learn algebra? career? you assume too much

representation of floating point numbers, excuse my poor english

David RF, Jun 4, 2012
15. ### David RFGuest

On 4 jun, 13:55, James Kuyper <> 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

David RF, Jun 4, 2012
16. ### Keith ThompsonGuest

"BartC" <> writes:
> "David RF" <> wrote in message
> news:...
>> On 4 jun, 13:10, James Kuyper <> wrote:

>
>> 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

>
> 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.

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) <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"

Keith Thompson, Jun 4, 2012
17. ### Guest

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!

, Jun 5, 2012
18. ### James KuyperGuest

On 06/05/2012 06:54 AM, 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:

....
....
>>> 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

James Kuyper, Jun 5, 2012
19. ### Joe keaneGuest

In article <>,
<> 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

Joe keane, Jun 5, 2012
20. ### Stefan RamGuest

James Kuyper <> writes:
>People as young as six are learning to program nowadays; possibly
>younger.

Six year olds still can actually write a program to
draw a circle, while older children cannot!

(Alan Kay was teaching five-year old children how to
program a circle: They were asked to walk in a circle
and to report what they did. The children would answer
"walk a little and turn a little." After that cognition
they could write a program to draw a circle.

Ten-year old children already knew what a circle is:
"The set of all point, having the same distance to a
center." So they startet to program individual points
starting at a center, which was more complicated; and
the result was non a connected circle but only single
dots.

Fifteen-year old children already knew the formula »r² = x² + y²«.
They tried to draw a circle using that formula, but
failed. (This formula is not a good starting point for such a
program.) Just because of their additional knowledge, it was
actually more difficult or impossible for them to write such a
program. At least that is what Alan Kay said in a video.)

Stefan Ram, Jun 5, 2012