Version française
Home     About     Download     Resources     Contact us    
Browse thread
2147483648l < 2147483647l
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Elnatan Reisner <elnatan@c...>
Subject: Re: [Caml-list] 2147483648l < 2147483647l
On Jan 19, 2010, at 1:28 PM, David Allsopp wrote:

> Goswin von Brederlow wrote:
>
>>> # 2147483648l;;
>>> - : int32 = -2147483648l
>>
>> Isn't that documented properly? I think in the docs I saw at least  
>> that
>> ocaml will silently overflow ints.
>
> Arithmetic operations are allowed to overflow silently but at no  
> point do
> you end up with an illegal constant which is the bug here.
>
> i.e. Int32.add 2147483647l 1l correctly evaluates to -2147483648l but
> -2147483648l is a valid int32. The evaluation of a constant by the  
> compiler
> in your ML source is supposed to follow the same rules as  
> Int32.of_string
> (which would raise an exception if given "-2147483648l") hence  
> 2147483648l
> which is not a valid int32 should be a "syntax" error.

As a point of clarification, the top level and the to_string functions  
do have the same behavior; none of them raise an exception when given  
a literal representing max_int+1 or min_int. It doesn't matter whether  
this is done with ints, Int32s, or Int64s. (I am using OCaml 3.11.1 on  
a 32-bit machine.)

# 1073741824,-1073741824;;
- : int * int = (-1073741824, -1073741824)
# int_of_string "1073741824",int_of_string "-1073741824";;
- : int * int = (-1073741824, -1073741824)

# 2147483648l,-2147483648l;;
- : int32 * int32 = (-2147483648l, -2147483648l)
# Int32.of_string "2147483648",Int32.of_string "-2147483648";;
- : int32 * int32 = (-2147483648l, -2147483648l)

# 9223372036854775808L,-9223372036854775808L;;
- : int64 * int64 = (-9223372036854775808L, -9223372036854775808L)
# Int64.of_string "9223372036854775808",Int64.of_string  
"-9223372036854775808";;
- : int64 * int64 = (-9223372036854775808L, -9223372036854775808L)

-Elnatan