Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
if (n:int) < 0 then (-n) > 0 is FALSE
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-12-08 (02:37)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] if (n:int) < 0 then (-n) > 0 is FALSE
On Thu, 2006-12-07 at 20:43 +0100, Mattias Engdegård wrote:
> >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.
> (Signed overflow is not legal in C (undefined behaviour), and a decent
> compiler will warn this can be detected statically.)

It can be detected statically? I'd sure like to know how
(without polluting the user with hundreds of spurious warnings).

> Of course it is a bug - a design bug, since the behaviour is intended
> and documented, but still a bug. But getting numbers, even integers,
> right in every respect is hard, and involves trade-offs between
> performance, correctness and convenience.

Hmmm. In the usual full word register operations most CPU
have an overflow flag which a single conditional branch can
detect. Given branch prediction the branch is unlikely to be
taken the cost of detection is probably extremely small,
provided you have control at the assembler level.

Ocaml has slighly messier representation with one less
bit, but i guess overflow detection wouldn't be so hard
or all that expensive -- but I could be wrong, perhaps
someone on Ocaml team has looked at this can can comment?

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: