Διαίρεση δεκαδικών

V13 v13 at priest.com
Sat May 11 22:49:01 EEST 2002


On Saturday 11 May 2002 20:20, drcypher wrote:
> Δε θυμάμαι αν το θέμα έχει ξανατεθεί...
>
> 	a = 1.0; b = 0.1;
> 	printf("(%g/%g == 10.0) == %s\n", a, b, (a/b == 10.0) ? "true" :
> "false");
Ayto an to alakseis se :
        printf("(%g/%g == 10.0) == %s %e\n", a, b, (a/b == 10.0) ? "true" :
"false", 10.0 - a/b);

Tha soy bgalei kati san:

(1/0.1 == 10.0) == false 5.551115e-16

> Τι μπορώ να κάνω ώστε ΟΛΑ να βγάζουν true? Και γιατί στα κομμάτια ο
> compiler (ή ο preprocessor, ξέρω γω;) μπορεί να κάνει τη διαίρεση
> επιτυχώς και το πρόγραμμα όχι; Υπάρχει τρόπος, ή πρέπει να κάνω κάτι
> ισοδύναμο; Ποιο θα είναι αυτό; Ουφ..

AFAIK ayto eksartatai isos apo compiler kai sigoyra apo epeksergasti 
(synepeksergasti gia tin akribeia). Ayto poy mas elegan kapote, einai oti 
orizeis mia mikri timi (se sxesi me ta parapano) kai sygkrineis tin apolyti 
timi tis diaforas me tin timi ayti (se fortran itan poio geniko/megalo to 
kako)
Edo px:
( fabs(10.0 - a/b)  < 0.000000001 ? "true" : "false" )

Oson afora ton compiler, thelo na pisteyo oti ayto den einai standard me oloys 
toys compilers kai oti yparxei periptosi kapoios allos compiler na soy bgazei 
kai aytos lathos.

se gcc3 an baleis -Wfloat-equal tha pareis:

a.cc:7: warning: comparing floating point with == or != is unsafe

Apo to man toy gcc:

       -Wfloat-equal
           Warn if floating point values are used in equality
           comparisons.

           The idea behind this is that sometimes it is conve-
           nient (for the programmer) to consider floating-point
           values as approximations to infinitely precise real
           numbers.  If you are doing this, then you need to com-
           pute (by analysing the code, or in some other way) the
           maximum or likely maximum error that the computation
           introduces, and allow for it when performing compar-
           isons (and when producing output, but that's a differ-
           ent problem).  In particular, instead of testing for
           equality, you would check to see whether the two val-
           ues have ranges that overlap; and this is done with
           the relational operators, so equality comparisons are
           probably mistaken.

<<V13>>



More information about the Linux-greek-users mailing list