PDA

View Full Version : La funzione segno e l'inferno dei numeri reali


verloc
16-05-2003, 11:59
Mi serviva una funzione che mi restituisse il segno di un numero e mi sono fatto questa:

template<class T>
inline const T SEGNO(const T &a )
{return a >= 0 ? 1 : -1;}

Poi mi è capitato di pensare all'aritmetica floating point e mi sono detto:
se è un numero picccolissimo minore di zero l' a.f.p lo arrotonda a zero e sballa.
Poi mi sono ricordato che esistono lo zero negativo e positivo.
Secondo voi la funzione è affidabile?

Per parlare poi delle safe comparisons e tutte le possibili dannazioni dello Real Inferno,
potremmo-potreste raggruppare e postare tutte le comparazioni affidabili in floating point?
(==, <= etc e tutte quelle che non mi vengono in mente...) :D

Grazie

cionci
16-05-2003, 12:10
L'uguaglianza non si verifica mai con i numeri FP...
In teoria dovrebbe comunque funzionare sempre...

a2000
16-05-2003, 13:33
per cominciare
google: "macheps"

P.S.
allora Piazza Plebiscito o Pert ?

verloc
16-05-2003, 15:53
Originally posted by "a2000"

per cominciare
google: "macheps"


Già fatto,ma il tema delle safe comparisons è controverso.Al prossimo giro posto le funzioni che ho trovato.


P.S.
allora Piazza Plebiscito o Pert

mandami un PVT per spiegarti meglio ;)

verloc
17-05-2003, 08:40
Il problema nella comparazione classica
if ( fabs(a-b) <= Machine_Epsilon * fabs(a))

è che non funziona se a è uguale zero:


From: [email protected] (Hans-Bernhard Broeker)
Subject: Re: Comparison of 2 floating point values
Date: 21 Dec 1999 00:00:00 GMT
Message-ID: <[email protected]>
References: <[email protected]> <[email protected]> <[email protected]> <[email protected]>
Followup-To: comp.graphics.algorithms,comp.graphics.api.opengl
X-Complaints-To: [email protected]
X-Trace: nets3.rz.RWTH-Aachen.DE 945780991 21761 137.226.32.75 (21 Dec 1999 12:56:31 GMT)
Organization: RWTH Aachen, III. physikalisches Institut B
NNTP-Posting-Date: 21 Dec 1999 12:56:31 GMT
Newsgroups: comp.graphics.algorithms,comp.graphics.api.opengl

Paul Hsieh ([email protected]) wrote:
> Usually to compare two numbers assuming a certain amount accuracy you do
> it like this:

> if( fabs(a/b-1) < MachEps )

Caution with this; you may get divide by zero problems. Avoid
division, like this:

if ( fabs(a-b) <= Machine_Epsilon * fabs(a))

Which is what the comp.lang.c FAQ recommends. And even _that_ doesn't
work if a is zero... you'ld need a special case, like:

#define EQ(a,b,precision) \
(fabs(a-b) <= precision * (a==0? (Machine_Epsilon*fabs(a)) \
: (Machine_Smallest)))

Where 'precision' is a factor that describes how much precision has
already been lost, relative to what the machine floating point format
can describe. This has to be a factor bigger than 1, to make sense.
It's this 'precision' factor you have to hand-tune to for every single
application of such a comparison, on every platform.

The latter is because comparing to a relative difference of
'Machine_Epsilon' is not a useful thing. By definition of Epsilon and
the structure of floating point numbers, it is roughly the smallest
*relative* difference between any two FP numbers. I.e. two numbers
with a relative difference of less than Epsilon would be exactly
equal, already, not just approximately, and the whole point of using a
test other than simple '==' would be moot.

As I hope you can see, dealing with floating point numbers can be
surprisingly complicated, as soon as you get down to the gory details.
--
Hans-Bernhard Broeker ([email protected])
Even if all the snow were burnt, ashes would remain.


poi ci sono le funzioni proposte da Matt Austern che è pur sempre un'autorità.
Ops scusatemi,non sono di Matt Austern(magari);la risposta è di Matt Austern

http://cpptips.hyperformix.com/cpptips/fp_eq

Avevo pensato di mettere tutto in un header in modo da avere a disposizione sempre
le capacità della macchina:

#include <limits>
...
numeric_limits<double> double_info;
const double EPS=double_info.epsilon();
...

ma dice che non è l'ideale se hai 50 moduli che utilizzano lo stesso header.

Che ne pensate?

cionci
17-05-2003, 10:30
Insomma...ma funziona o no la tua funzione ? In ogni caso se c'è un epsilon negativo o positivo puoi sempre vedere che sia maggiore o minore di 0...

verloc
17-05-2003, 15:34
Originally posted by "cionci"

Insomma...ma funziona o no la tua funzione ? In ogni caso se c'è un epsilon negativo o positivo puoi sempre vedere che sia maggiore o minore di 0...
Si... almeno credo :) (su stà materia mi sento molto insicuro)

Mi riferivo nel mio ultimo post a quelle funzioni che cercano di
appurare più o meno affidabilmente se 2 numeri sono uguali .
Scusatemi,avrei dovuto dirlo esplicitamente :)

ps a2000 quando ti serve non c'è mai.