Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: "Pal-Kristian Engstad" <pal_engstad@naughtydog.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] if (n:int) < 0 then (-n) > 0 is FALSE
Date: Fri, 8 Dec 2006 08:19:54 +1300	[thread overview]
Message-ID: <ad8cfe7e0612071119k6a9c51a4s79fae5838f5d6fa1@mail.gmail.com> (raw)
In-Reply-To: <45785E27.8050804@naughtydog.com>

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 <pal_engstad@naughtydog.com> wrote:
> Using OCaml 3.08.3 on the following function:
>
> let pow x n =
>  let rec aux x n acc =
>    if n == 0 then acc
>    else if n == 1 then acc *. x
>    else if n land 1 == 0 then aux (x*.x) (n/2) acc
>    else aux (x*.x) ((n-1)/2) (x*.acc)
>  in
>    if n >= 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=2^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 = -(-20^30) = -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ål-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
>


  reply	other threads:[~2006-12-07 19:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-07 18:32 Pal-Kristian Engstad
2006-12-07 19:19 ` Jonathan Roewen [this message]
2006-12-07 19:43   ` [Caml-list] " Mattias Engdegård
2006-12-07 20:02     ` Brian Hurt
2006-12-08  2:37     ` skaller
2006-12-08 11:09       ` Mattias Engdegård
2006-12-08 17:48     ` Jon Harrop
2006-12-08 18:20       ` Mattias Engdegård
2006-12-08 18:37         ` Brian Hurt
2006-12-09  2:03           ` skaller
2006-12-07 21:13 ` Martin Jambon

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=ad8cfe7e0612071119k6a9c51a4s79fae5838f5d6fa1@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=pal_engstad@naughtydog.com \
    /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