From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id BAA11392; Fri, 25 Jun 2004 01:17:14 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA11617; Fri, 25 Jun 2004 01:17:13 +0200 (MET DST) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i5ONHCSH025923; Fri, 25 Jun 2004 01:17:12 +0200 Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.11.6/8.10.1) with ESMTP id i5ONH8i13521; Thu, 24 Jun 2004 18:17:09 -0500 (CDT) Date: Thu, 24 Jun 2004 18:23:04 -0500 (CDT) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Xavier Leroy cc: John Hughes , "'caml-list'" Subject: Re: [Caml-list] Why must types be always defined at the top level? In-Reply-To: <20040624194553.A27745@pauillac.inria.fr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at concorde with ID 40DB60F8.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 haskell:01 wether:01 floats:01 reinvent:01 kahan:01 floats:01 abide:01 ocaml:01 ocaml:01 equality:01 equality:01 writting:01 rec:01 arithmetic:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, 24 Jun 2004, Xavier Leroy wrote: > There is a general need to have polymorphic operations that are 1- not > defined on all instantiations of their types, and 2- can be defined > differently on different instantiations. Haskell type classes are an > example of a *general* mechanism that addresses this need; GCaml's > "extentional polymorphism" is another. Ocaml modules are another way to implement this. Wether it's a "good" way or not is open to debate. > As to whether equality should be defined on floats, there are pros and > cons. My standpoint is that it's eventually better to stick to > established standards (that is, IEEE float arithmetic) rather than try > to reinvent a wheel likely to be even squarer than these standards. > Prof. Kahan found it worthwhile to fully define equality over floats; > I'll abide by his wisdom. There are legitimate reasons to want floating point equality. It's generally not what you want, but I had an example of needing it in Ocaml just the other day. Basically, I was writting a root finding algorithm by range subdivision. Given a function f and a range [a,b] where sign(f(a)) != sign(f(b)), find the root in that range, the x such that a <= x <= b, and f(x) = 0 (or as close as can come). The function I ended up with was this: let root_find f a b = let rec loop a b fa_is_neg = let c = (a +. b) /. 2. in if (c = a) || (c = b) then (* note the floating point equality *) (* We've bottomed out- pick a or b depending upon if f(a) or f(b) is closer to 0. *) if (abs_float (f a)) > (abs_float (f b)) then a else b else let fc_is_neg = ((f c) < 0.) in if (fa_is_neg == fc_is_neg) then loop c b fc_is_neg else loop a c fa_is_neg in loop a b ((f a) < 0.) ;; Basically, I run the algorithm out until the distance between a and b is 1ulp, then stop. -- "Usenet is like a herd of performing elephants with diarrhea -- massive, difficult to redirect, awe-inspiring, entertaining, and a source of mind-boggling amounts of excrement when you least expect it." - Gene Spafford Brian ------------------- 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