caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Luc Maranget <luc.maranget@inria.fr>
To: jscott@planetinternet.be (Scott J, )
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Runtime overflow and what to do
Date: Thu, 10 Oct 2002 17:21:13 +0200 (MET DST)	[thread overview]
Message-ID: <200210101521.RAA0000032708@beaune.inria.fr> (raw)
In-Reply-To: <00bd01c2706d$e7cd68e0$0100a8c0@janxp> from "Scott J," at oct 10, 2002 05:01:26

> Hi,
> 
> 
> suppose I use the following code for fact
> 
> let rec fact n = if n <= 1 then 1 else n * fact (n - 1) ;;
> 
> Because an integer number is represented by a fixed number of bytes I will
> get a runtime overflow if n is chosen too large.
> 
> Is there in Ocamel a workaround to cope with this problem . Something like "
> Onoverflow goto .. " in imperative languages.
> 
> Thx
> 
> Scott
> 
> 

There is no such feature as a Onoverflow goto, because
Ocaml (and not OCamel...) default integers are not << real >> integers,
but machine integers on some size (typically 31 or 63) bits.

This means that maxint + 1 is minint and basically this is the
behavior of C.

I guess the reason why is avoiding putting non-obvious tests
everywhere.

Most of the time you do not need those tests.
  * If you want ``real'' integers, you can use some arbitrary
    precision library (Num, etc.), overflow detection would  not help
    here anyway.

  * If you want to be warned on overflow, you have to devise the tests
    by yourself, and then you can use an exception as your
    Onoverflow goto
     An easy one :

expection Overflow

let protected_incr x =
  let y = x+1 in
  if y > x then y
  else raise Overflow

try
  .... incr (..)
with
| Overflow -> ... do whatever you can ... 

Hope it helps.

--Luc
               

    
-------------------
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


  parent reply	other threads:[~2002-10-10 15:21 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-10 15:01 Scott J,
2002-10-10 15:07 ` Maxence Guesdon
2002-10-10 15:11 ` Maxence Guesdon
2002-10-10 15:21 ` Luc Maranget [this message]
2002-10-10 18:13   ` Sven LUTHER
2002-10-10 18:57     ` Xavier Leroy
2002-10-10 22:17       ` Scott J,
2002-10-11  8:00         ` Sven LUTHER
2002-10-11 10:16         ` Alessandro Baretta
2002-10-12 16:13           ` John Carr
2002-10-12 16:58             ` Sven LUTHER
2002-10-12 17:12               ` John Prevost
2002-10-13  9:31               ` Xavier Leroy
2002-10-13  9:52                 ` Sven LUTHER
2002-10-13  9:57                   ` Xavier Leroy
2002-10-13 10:15                     ` Sven LUTHER
2002-10-13 13:25                   ` John Carr
2002-10-13  9:28             ` Xavier Leroy
2002-10-14  0:56               ` Chris Hecker
2002-10-14  9:46                 ` Jacques Garrigue
2002-10-14 11:02                   ` Scott J,
2002-10-14 14:05                   ` Tim Freeman
2002-10-14 15:32                     ` Stefano Zacchiroli
2002-10-14 16:12                       ` Alain Frisch
2002-10-14 16:49                   ` [Caml-list] new ocaml switches (was Re: Runtime overflow and what to do) Chris Hecker
2002-10-13  9:25         ` [Caml-list] Runtime overflow and what to do Xavier Leroy
2002-10-13 10:04           ` Daniel de Rauglaudre
2002-10-13 10:29             ` Pierre Weis
2002-10-13 10:47               ` Daniel de Rauglaudre
2002-10-13 12:38                 ` Pierre Weis
2002-10-13 16:14               ` Xavier Leroy
2002-10-13 17:06                 ` Michel Quercia
2002-10-15 19:15                 ` Pierre Weis
2002-10-15 19:25                   ` Xavier Leroy
2002-10-16  8:24                     ` Pierre Weis
2002-10-13 10:19           ` Pierre Weis
2002-10-11  8:02       ` Sven LUTHER
2002-10-11  6:42 ` Florian Hars

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=200210101521.RAA0000032708@beaune.inria.fr \
    --to=luc.maranget@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=jscott@planetinternet.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).