[Caml-list] 2147483648l < 2147483647l

2010-01-05 Thread Matej Kosik
Hello,

I am sorry, I have a stupid question.
I would like to ask if this:

# 2147483648l < 2147483647l;;
- : bool = true

should not regarded as a bug. In my project I need Int32 value and above
behavior surprised me. Value

2147483648l

should not be allowed at all because it cannot be encoded to 32-bit
signed integer encoded with 2's complement representation.

Thanks in advance for help.

___
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


Re: [Caml-list] 2147483648l < 2147483647l

2010-01-05 Thread Olivier Andrieu
On Tue, Jan 5, 2010 at 13:59, Matej Kosik  wrote:
> Hello,
>
> I am sorry, I have a stupid question.
> I would like to ask if this:
>
>        # 2147483648l < 2147483647l;;
>        - : bool = true
>
> should not regarded as a bug. In my project I need Int32 value and above
> behavior surprised me. Value
>
>        2147483648l
>
> should not be allowed at all because it cannot be encoded to 32-bit
> signed integer encoded with 2's complement representation.
>
> Thanks in advance for help.

yes that's a "misfeature" of the lexer/parser. The point is that it
accepts -2147483648l (representable as an int32) ; the downside is
that it accepts 2147483648l and treats it as -1l.

Cf. this bug report: http://caml.inria.fr/mantis/view.php?id=4210 .

-- 
  Olivier

___
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


RE: [Caml-list] 2147483648l < 2147483647l

2010-01-05 Thread David Allsopp
Matej Kosik wrote:
> I am sorry, I have a stupid question.
> I would like to ask if this:
> 
>   # 2147483648l < 2147483647l;;
>   - : bool = true

The bug is in fact:

# 2147483648l;;
- : int32 = -2147483648l

and given that behaviour, the above result makes sense! But...

> it cannot be encoded to 32-bit
> signed integer encoded with 2's complement representation.

and it's outside the range [Int32.min_int, Int32.max_int]

However, it's a known bug - http://caml.inria.fr/mantis/view.php?id=4210

According to Mantis, it's fixed in 3.12 - I'm sure there's a reason that
it's not been fixed in 3.11.2+rc1


David


___
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


Re: [Caml-list] 2147483648l < 2147483647l

2010-01-19 Thread Goswin von Brederlow
"David Allsopp"  writes:

> Matej Kosik wrote:
>> I am sorry, I have a stupid question.
>> I would like to ask if this:
>> 
>>  # 2147483648l < 2147483647l;;
>>  - : bool = true
>
> The bug is in fact:
>
> # 2147483648l;;
> - : int32 = -2147483648l
>
> and given that behaviour, the above result makes sense! But...

Isn't that documented properly? I think in the docs I saw at least
that ocaml will silently overflow ints.

But yeah, the parser/compiler should really warn when that happens.

MfG
Goswin

___
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


RE: [Caml-list] 2147483648l < 2147483647l

2010-01-19 Thread David Allsopp
Goswin von Brederlow wrote:
> "David Allsopp"  writes:
> 
> > Matej Kosik wrote:
> >> I am sorry, I have a stupid question.
> >> I would like to ask if this:
> >>
> >># 2147483648l < 2147483647l;;
> >>- : bool = true
> >
> > The bug is in fact:
> >
> > # 2147483648l;;
> > - : int32 = -2147483648l
> >
> > and given that behaviour, the above result makes sense! But...
> 
> 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.


David

___
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


Re: [Caml-list] 2147483648l < 2147483647l

2010-01-19 Thread Elnatan Reisner

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