From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id F1E50BBAF for ; Tue, 19 Jan 2010 20:12:47 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuYBAAuTVUuACH+TmWdsb2JhbACbfwEBAQEBCAsKBxOwHYUehmuEMwQ X-IronPort-AV: E=Sophos;i="4.49,305,1262559600"; d="scan'208,217";a="42108995" Received: from server-nat-4.cs.umd.edu (HELO bacon.cs.umd.edu) ([128.8.127.147]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 19 Jan 2010 20:12:47 +0100 X-CSD-MailScanner-Watermark: 1264533161.26462@KvhaMZyLUcvLvkaZ0RPNmw Received: from 208-59-170-141.c3-0.slvr-ubr1.lnh-slvr.md.cable.rcn.com (208-59-170-141.c3-0.slvr-ubr1.lnh-slvr.md.cable.rcn.com [208.59.170.141]) (authenticated bits=0) by bacon.cs.umd.edu (8.13.1/8.14.1) with ESMTP id o0JJCe3q013208 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) for ; Tue, 19 Jan 2010 14:12:40 -0500 Message-Id: <348A6F6E-AE4B-42B3-9FDA-5E58FBD62426@cs.umd.edu> From: Elnatan Reisner To: OCaml List In-Reply-To: <00ff01ca9935$3539b050$9fad10f0$@romulus.metastack.com> Content-Type: multipart/alternative; boundary=Apple-Mail-75-5989953 Mime-Version: 1.0 (Apple Message framework v936) Subject: Re: [Caml-list] 2147483648l < 2147483647l Date: Tue, 19 Jan 2010 14:15:14 -0500 References: <4B4337A0.8000603@fiit.stuba.sk> <000101ca8e0a$aa006450$fe012cf0$@romulus.metastack.com> <87ska2gelk.fsf@frosties.localdomain> <00ff01ca9935$3539b050$9fad10f0$@romulus.metastack.com> X-Mailer: Apple Mail (2.936) X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0 (bacon.cs.umd.edu [172.24.3.34]); Tue, 19 Jan 2010 14:12:40 -0500 (EST) X-CSD-MailScanner-Information: Please email staff@cs.umd.edu for more information X-MailScanner-ID: o0JJCe3q013208 X-CSD-MailScanner: Found to be clean X-CSD-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-48.059, required 5, ALL_TRUSTED -50.00, HTML_MESSAGE 0.01, TVD_RCVD_IP 1.93) X-CSD-MailScanner-From: elnatan@cs.umd.edu X-Spam: no; 0.00; ocaml:01 bug:01 compiler:01 syntax:01 ocaml:01 bug:01 compiler:01 syntax:01 wrote:01 wrote:01 exception:01 exception:01 ints:01 ints:01 caml-list:01 --Apple-Mail-75-5989953 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit On Jan 19, 2010, at 1:28 PM, David Allsopp wrote: > Goswin von Brederlow wrote: > >>> # 2147483648l;; >>> - : int32 = -2147483648l >> >> Isn't that documented properly? I think in the docs I saw at least >> that >> ocaml will silently overflow ints. > > Arithmetic operations are allowed to overflow silently but at no > point do > you end up with an illegal constant which is the bug here. > > i.e. Int32.add 2147483647l 1l correctly evaluates to -2147483648l but > -2147483648l is a valid int32. The evaluation of a constant by the > compiler > in your ML source is supposed to follow the same rules as > Int32.of_string > (which would raise an exception if given "-2147483648l") hence > 2147483648l > which is not a valid int32 should be a "syntax" error. As a point of clarification, the top level and the to_string functions do have the same behavior; none of them raise an exception when given a literal representing max_int+1 or min_int. It doesn't matter whether this is done with ints, Int32s, or Int64s. (I am using OCaml 3.11.1 on a 32-bit machine.) # 1073741824,-1073741824;; - : int * int = (-1073741824, -1073741824) # int_of_string "1073741824",int_of_string "-1073741824";; - : int * int = (-1073741824, -1073741824) # 2147483648l,-2147483648l;; - : int32 * int32 = (-2147483648l, -2147483648l) # Int32.of_string "2147483648",Int32.of_string "-2147483648";; - : int32 * int32 = (-2147483648l, -2147483648l) # 9223372036854775808L,-9223372036854775808L;; - : int64 * int64 = (-9223372036854775808L, -9223372036854775808L) # Int64.of_string "9223372036854775808",Int64.of_string "-9223372036854775808";; - : int64 * int64 = (-9223372036854775808L, -9223372036854775808L) -Elnatan --Apple-Mail-75-5989953 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable
On Jan 19, 2010, at = 1:28 PM, David Allsopp wrote:

Goswin = von Brederlow wrote:

# = 2147483648l;;
- : int32 =3D = -2147483648l

Isn't = that documented properly? I think in the docs I saw at least = that
ocaml will silently = overflow ints.

Arithmetic operations are allowed to = overflow silently but at no point do
you end up with an illegal = constant which is the bug here.

i.e. Int32.add 2147483647l 1l = correctly evaluates to -2147483648l but
-2147483648l is a valid = int32. The evaluation of a constant by the compiler
in your ML source = is supposed to follow the same rules as Int32.of_string
(which would = raise an exception if given "-2147483648l") hence 2147483648l
which = is not a valid int32 should be a "syntax" = error.

As a point of = clarification, the top level and the to_string functions = do have the same behavior; none of them raise an = exception when given a literal representing max_int+1 or min_int. It = doesn't matter whether this is done with ints, Int32s, or Int64s. (I am = using OCaml 3.11.1 on a 32-bit = machine.)

# = 1073741824,-1073741824;;
- : int * int =3D (-1073741824, = -1073741824)
# int_of_string "1073741824",int_of_string = "-1073741824";;
- : int * int =3D (-1073741824, = -1073741824)

# = 2147483648l,-2147483648l;;
- : int32 * int32 =3D = (-2147483648l, -2147483648l)
# Int32.of_string = "2147483648",Int32.of_string "-2147483648";;
- : int32 * int32 = =3D (-2147483648l, -2147483648l)

# = 9223372036854775808L,-9223372036854775808L;;
- : int64 * int64 = =3D (-9223372036854775808L, -9223372036854775808L)
# = Int64.of_string "9223372036854775808",Int64.of_string = "-9223372036854775808";;
- : int64 * int64 =3D = (-9223372036854775808L, = -9223372036854775808L)

-Elnatan
= = --Apple-Mail-75-5989953--