From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=AWL,SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id B9905BC0B for ; Thu, 7 Dec 2006 20:19:57 +0100 (CET) Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.230]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id kB7JJu1x023491 for ; Thu, 7 Dec 2006 20:19:57 +0100 Received: by wx-out-0506.google.com with SMTP id i31so583792wxd for ; Thu, 07 Dec 2006 11:19:54 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=mCk5nN5n8uN0Az7fAgoe9CYP7B4OBiLOU3mOu0RDHi4h4raSErfAWkjMM8KY4JOMRFyR82NxrsCaIAuXf9I1thXL9XyamQqaacoE8WNn3uHBx2u8OLz/YgBx5GXbYwaRT+YG4+WbkHwghsLB7YobqDhoiAmHxsd7iuHBhaTpWNo= Received: by 10.70.109.12 with SMTP id h12mr4209992wxc.1165519194179; Thu, 07 Dec 2006 11:19:54 -0800 (PST) Received: by 10.70.122.8 with HTTP; Thu, 7 Dec 2006 11:19:54 -0800 (PST) Message-ID: Date: Fri, 8 Dec 2006 08:19:54 +1300 From: "Jonathan Roewen" To: "Pal-Kristian Engstad" Subject: Re: [Caml-list] if (n:int) < 0 then (-n) > 0 is FALSE Cc: caml-list@inria.fr In-Reply-To: <45785E27.8050804@naughtydog.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <45785E27.8050804@naughtydog.com> X-j-chkmail-Score: MSGID : 4578695C.000 on discorde : j-chkmail score : X : 0/20 1 0.000 -> 1 X-Miltered: at discorde with ID 4578695C.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; bug:01 ocaml:01 ocaml:01 3.08.3:01 bug:01 pke:01 beginner's:01 31.:98 1.0:98 1.0:98 dog:98 beginners:01 wrote:01 overflows:01 rec:01 It's not a bug -- you're relying on 32bit ints when OCaml has only 31bit ints (on 32-bit arch). In C (for example), if you use a plain int (I believe it'll default to signed), you get the exact same behaviour when the number overflows 2^31. The only difference here is that unlike C and many other languages, OCaml has no unsigned numeric type. However, even if it were unsigned, you still can't avoid a hard limit on maximum integer value. My recommendation would be to use the Nums library so you don't get overflow errors, or do an overflow check and throw an exception. Jonathan On 12/8/06, Pal-Kristian Engstad wrote: > Using OCaml 3.08.3 on the following function: > > let pow x n =3D > let rec aux x n acc =3D > if n =3D=3D 0 then acc > else if n =3D=3D 1 then acc *. x > else if n land 1 =3D=3D 0 then aux (x*.x) (n/2) acc > else aux (x*.x) ((n-1)/2) (x*.acc) > in > if n >=3D 0 then > aux x n 1.0 > else > 1.0 /. (aux x (-n) 1.0) > ;; > > I tested this function with > > # pow 1.0 (1024 * 1024 * 1024) > > To find that it loops forever. The reason is that 1024*1024*1024=3D2^30 > cannot be represented as a positive number on 32-bit platforms, hence it > silently converts it to -1073741824, or -2^30. The reason this loops > again is that -n =3D -(-20^30) =3D -20^30......, still negative! > > This is obviously a bug - has it since been fixed? But more alarmingly - > why is there no warning? > > Thanks, > > PKE. > > -- > P=E5l-Kristian Engstad (engstad@naughtydog.com), Lead Programmer, ICE > team, Naughty Dog, Inc., 1601 Cloverfield Blvd, 6000 North, > Santa Monica, CA 90404, USA. Ph.: (310) 633-9112. > > "Most of us would do well to remember that there is a reason Carmack > is Carmack, and we are not Carmack.", > Jonathan Blow, 2/1/2006, GD Algo Mailing List > > > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >