* [Caml-list] min, max and nan
@ 2004-07-15 3:51 Yaron Minsky
2004-07-15 12:38 ` Markus Mottl
0 siblings, 1 reply; 2+ messages in thread
From: Yaron Minsky @ 2004-07-15 3:51 UTC (permalink / raw)
To: Caml Mailing List
One of the oddities associated with NaNs in ocaml is what happens when
you take the min of something and nan. Witness:
# min nan 3.;;
- : float = 3.
# min 3. nan;;
- : float = nan
Surprisingly, the result depends on the ordering. Why it does so is
clear once you look at the implementation and ponder for a moment the
IEEE requirements on comparisons involving nan. When using ocaml
3.07, I tried and failed to come up with a polymorphic min that
behaved reasonably in this case, that is, that returned nan when
either argument is nan. With 3.08, this is now doable. Here's how it
works:
# let contains_nan x = x <> x;;
val contains_nan : 'a -> bool = <fun>
# let nmin x y =
if contains_nan x then x
else if contains_nan y then y
else min x y;;
val nmin : 'a -> 'a -> 'a = <fun>
# nmin 3. nan;;
- : float = nan
# nmin nan 3.;;
- : float = nan
Still, we don't quite escape from the oddities of nan. We still get
order dependence in the case of larger data structures that include
nan's:
# nmin (1,nan) (2,nan);;
- : int * float = (1, nan)
# nmin (2,nan) (1,nan);;
- : int * float = (2, nan)
But in my mind, the current state of affairs is a big improvement.
Yaron
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] min, max and nan
2004-07-15 3:51 [Caml-list] min, max and nan Yaron Minsky
@ 2004-07-15 12:38 ` Markus Mottl
0 siblings, 0 replies; 2+ messages in thread
From: Markus Mottl @ 2004-07-15 12:38 UTC (permalink / raw)
To: yminsky; +Cc: Caml Mailing List
On Wed, 14 Jul 2004, Yaron Minsky wrote:
> When using ocaml 3.07, I tried and failed to come up with a polymorphic
> min that behaved reasonably in this case, that is, that returned
> nan when either argument is nan. With 3.08, this is now doable.
> Here's how it works:
Another possibility is to use "classify_float":
let nmin x y =
if classify_float x = FP_nan || classify_float y = FP_nan then nan
else min x y
This is actually even more efficient.
> Still, we don't quite escape from the oddities of nan. We still get
> order dependence in the case of larger data structures that include
> nan's:
I think this could be fixed by making use of the Obj-module, though this
would look a little bit messy.
Regards,
Markus
--
Markus Mottl http://www.oefai.at/~markus markus@oefai.at
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-07-15 12:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-15 3:51 [Caml-list] min, max and nan Yaron Minsky
2004-07-15 12:38 ` Markus Mottl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox