caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Issac Trotts <ijtrotts@ucdavis.edu>
To: Frederic van der Plancke <fvdp@decis.be>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Int overflow in literals
Date: Thu, 30 Oct 2003 15:27:24 -0800	[thread overview]
Message-ID: <20031030232724.GA20225@ucdavis.edu> (raw)
In-Reply-To: <3FA18458.6F8F30D5@decis.be>

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


  reply	other threads:[~2003-10-30 23:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-30 13:53 Marcin 'Qrczak' Kowalczyk
2003-10-30 17:37 ` Alex Baretta
2003-10-30 17:59   ` Frederic van der Plancke
2003-10-30 19:20     ` Oleg Trott
2003-10-30 19:40       ` Marcin 'Qrczak' Kowalczyk
2003-10-30 20:05 ` Issac Trotts
2003-10-30 21:14   ` Oleg Trott
2003-10-30 21:26     ` Kenneth Knowles
2003-10-31  0:18       ` Jacques Garrigue
2003-10-31  2:05         ` Kenneth Knowles
2003-11-02 15:05     ` skaller
2003-11-02 16:23       ` Brian Hurt
2003-11-02 16:39         ` Marcin 'Qrczak' Kowalczyk
2003-11-07  7:22         ` skaller
2003-10-30 21:36   ` Frederic van der Plancke
2003-10-30 23:27     ` Issac Trotts [this message]
2003-10-30 23:43     ` Oleg Trott
2003-10-31 16:42 ` Xavier Leroy
2003-10-31 17:39   ` Marcin 'Qrczak' Kowalczyk
2003-10-31 17:50   ` Oleg Trott
2003-11-02 15:23   ` skaller
2003-11-02 16:37     ` Marcin 'Qrczak' Kowalczyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031030232724.GA20225@ucdavis.edu \
    --to=ijtrotts@ucdavis.edu \
    --cc=caml-list@inria.fr \
    --cc=fvdp@decis.be \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).