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 AAA29813; Fri, 31 Oct 2003 00:22:07 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA29795 for ; Fri, 31 Oct 2003 00:21:55 +0100 (MET) Received: from pop9.ucdavis.edu (pop9.ucdavis.edu [169.237.105.19]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h9UNIq129955 for ; Fri, 31 Oct 2003 00:20:11 +0100 (MET) Received: from beech (beech.ucdavis.edu [128.120.141.134]) by pop9.ucdavis.edu (8.12.9/8.12.9/it-std-5.0.2) with ESMTP id h9UNIHEV004213; Thu, 30 Oct 2003 15:18:18 -0800 (PST) Received: from ijtrotts by beech with local (Exim 3.36 #1 (Debian)) id 1AFMCK-0005HI-00; Thu, 30 Oct 2003 15:27:24 -0800 Date: Thu, 30 Oct 2003 15:27:24 -0800 From: Issac Trotts To: Frederic van der Plancke Cc: OCaml Mailing List Subject: Re: [Caml-list] Int overflow in literals Message-ID: <20031030232724.GA20225@ucdavis.edu> Reply-To: ijtrotts@ucdavis.edu References: <1067522012.5880.6.camel@qrnik> <20031030200519.GA19324@ucdavis.edu> <3FA18458.6F8F30D5@decis.be> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3FA18458.6F8F30D5@decis.be> User-Agent: Mutt/1.5.4i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; issac:01 trotts:01 ijtrotts:01 caml-list:01 plancke:01 issac:01 trotts:01 marcin:01 'qrczak':01 kowalczyk:01 failwith:01 ric:99 bug:01 faq:01 faq:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, Oct 30, 2003 at 10:36:24PM +0100, Frederic van der Plancke wrote: > > > Issac Trotts wrote: > > > > On Thu, Oct 30, 2003 at 02:53:32PM +0100, Marcin 'Qrczak' Kowalczyk wrote: > > > I understand that int overflow is not checked on arithmetic for > > > efficiency reasons, but IMHO it would be better if it was checked > > > at least in literals. When someone writes 10000000000, he certainly > > > does not mean -737418240. > > > > If you want to be sure that the number is correctly stored, you can use > > Int64: > > > > Int64.of_string "10000000000" > > > > Issac > > That was not my problem. My problem was to be able to read a list of integers from a file and be warned in case of overflow. And to be able to rely on int_of_string for that purpose. I got hit... of course now I know, but other innocent programmers may get hit in the future as well. (Not to speak of the not-so-innocent people who wrote this nice OCaml compiler ;-) Here's a way to do what you want, at least for numbers betweeen -9223372036854775808 and 9223372036854775807 : let safe_int_of_string str = let i = int_of_string str in let i64 = Int64.of_string str in if i64 = Int64.of_int i then i else failwith "overflow" # safe_int_of_string "10000000000";; Exception: Failure "overflow". # safe_int_of_string (string_of_int 1073741823) ;; - : int = 1073741823 -ijt > > Fr?d?ric. > > ------------------- > 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 > -- ------------------- 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