comparison.hpp close and close_enough

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

comparison.hpp close and close_enough

xinonnet
The close functions return false even for two VERY close numbers, for example:

close( 0, QL_EPSILON ) returns false
close( QL_EPSILON, QL_EPSILON * 10 ) returns false

It only returns true for identical numbers, which defeats the purpose of these functions.

Reply | Threaded
Open this post in threaded view
|

Re: comparison.hpp close and close_enough

marco.tarenghi

Actually this algorithm is very standard but does not work correctly when trying to compare numbers very close to zero.
My advice is to add 1 to both x and y when comparing nearly null values.
For example, instead of comparing x and 0 using close(x,0.0), use close(x+1.0,1.0).
This should be enough.

Regards,
Marco



xinonnet <[hidden email]>
Sent by: [hidden email]

11/06/2008 17.48

To
[hidden email]
cc
Subject
[Quantlib-users]  comparison.hpp close and close_enough






The close functions return false even for two VERY close numbers, for
example:

close( 0, QL_EPSILON ) returns false
close( QL_EPSILON, QL_EPSILON * 10 ) returns false

It only returns true for identical numbers, which defeats the purpose of
these functions.


--
View this message in context: http://www.nabble.com/comparison.hpp-close-and-close_enough-tp17735096p17735096.html
Sent from the quantlib-users mailing list archive at Nabble.com.


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users





------------------------------------------------------------
Il presente messaggio di posta elettronica e i suoi allegati sono destinati esclusivamente e personalmente al/ai destinatario/i e possono contenere informazioni di carattere riservato. Qualora non fosse il destinatario del presente messaggio la preghiamo di avvertirci immediatamente tramite posta elettronica o telefonicamente e di cancellare il presente messaggio e i suoi allegati dalla sua casella di posta elettronica e dai suoi sistemi. E’vietato copiare, utilizzare, comunicare e divulgare il presente messaggio senza autorizzazione. Questo messaggio non costituisce una sollecitazione all'investimento o un'offerta di acquisto o di vendita di strumenti finanziari o una conferma formale di un’operazione. Gruppo Banca Leonardo è impegnata solo dalle dichiarazioni dei propri legali rappresentanti. Poiché le trasmissioni elettroniche non garantiscono la sicurezza e la correttezza dei dati, non assumiamo alcuna responsabilità in merito alla completezza e accuratezza delle informazioni contenute nel presente messaggio.
L'indirizzo di posta elettronica dal quale viene inviato il presente messaggio non é un indirizzo privato del mittente. Si avverte che le risposte a questo messaggio potrebbero essere conosciute nell'organizzazione del mittente, nei limiti e con le procedure di legge.
------------------------------------------------------------
This e-mail and its attachment(s) are intended solely for the personal and confidential use of the addressee(s). If you are not the intended recipient of this message please notify us immediately by reply e-mail or by telephone and then delete this message and any file attached from your inbox and systems. Any unauthorized copy, use, disclosure, dissemination of this message is strictly prohibited. This communication should not be regarded as a solicitation or as an offer to sell or to buy any financial product, an official confirmation of any transaction, or as an official statement of Gruppo Banca Leonardo. E-mail transmissions cannot be guaranteed to be secure or error-free. Therefore, no representation is made that this information is complete or accurate and it should not be relied upon as such.
The e-mail address from which this message is being sent is not private to the sender. Please note that responses to this message could be known in the sender's organization, in compliance with the mandatory limits and procedures.

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: comparison.hpp close and close_enough

Ferdinando M. Ametrano-3
In reply to this post by xinonnet
On Mon, Jun 9, 2008 at 5:20 PM, xinonnet <[hidden email]> wrote:
> The close functions return false even for two VERY close numbers
> [...]
> It only returns true for identical numbers, which defeats the purpose of
> these functions.

the purpose of the close function is to be a _robust_ substitute for
the == comparison operator. Even if two numbers are "equal" from an
end-user point of view == might return false because of machine
precision issues. In order to solve this problem Knuth has provided
some advice which Luigi implemented in the close function.

> close( 0, QL_EPSILON ) returns false
> close( QL_EPSILON, QL_EPSILON * 10 ) returns false

With the above explanation in mind it should be clear that zero and
QL_EPSILON are not equal, and the same apply to QL_EPSILON and
QL_EPSILON * 10

Naming the function "close" might have been not ideal, but your
mileage might vary. I'll leave to Luigi when he's back to add more
info and/or rename the functions.

ciao -- Nando

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users