caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* 2147483648l < 2147483647l
@ 2010-01-05 12:59 Matej Kosik
  2010-01-05 13:17 ` [Caml-list] " Olivier Andrieu
  2010-01-05 13:26 ` David Allsopp
  0 siblings, 2 replies; 6+ messages in thread
From: Matej Kosik @ 2010-01-05 12:59 UTC (permalink / raw)
  To: caml-list

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] 2147483648l < 2147483647l
  2010-01-05 12:59 2147483648l < 2147483647l Matej Kosik
@ 2010-01-05 13:17 ` Olivier Andrieu
  2010-01-05 13:26 ` David Allsopp
  1 sibling, 0 replies; 6+ messages in thread
From: Olivier Andrieu @ 2010-01-05 13:17 UTC (permalink / raw)
  To: Matej Kosik; +Cc: caml-list

On Tue, Jan 5, 2010 at 13:59, Matej Kosik <kosik@fiit.stuba.sk> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] 2147483648l < 2147483647l
  2010-01-05 12:59 2147483648l < 2147483647l Matej Kosik
  2010-01-05 13:17 ` [Caml-list] " Olivier Andrieu
@ 2010-01-05 13:26 ` David Allsopp
  2010-01-19 17:59   ` Goswin von Brederlow
  1 sibling, 1 reply; 6+ messages in thread
From: David Allsopp @ 2010-01-05 13:26 UTC (permalink / raw)
  To: OCaml List

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] 2147483648l < 2147483647l
  2010-01-05 13:26 ` David Allsopp
@ 2010-01-19 17:59   ` Goswin von Brederlow
  2010-01-19 18:28     ` David Allsopp
  0 siblings, 1 reply; 6+ messages in thread
From: Goswin von Brederlow @ 2010-01-19 17:59 UTC (permalink / raw)
  To: David Allsopp; +Cc: OCaml List

"David Allsopp" <dra-news@metastack.com> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [Caml-list] 2147483648l < 2147483647l
  2010-01-19 17:59   ` Goswin von Brederlow
@ 2010-01-19 18:28     ` David Allsopp
  2010-01-19 19:15       ` Elnatan Reisner
  0 siblings, 1 reply; 6+ messages in thread
From: David Allsopp @ 2010-01-19 18:28 UTC (permalink / raw)
  To: 'OCaml List'

Goswin von Brederlow wrote:
> "David Allsopp" <dra-news@metastack.com> 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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] 2147483648l < 2147483647l
  2010-01-19 18:28     ` David Allsopp
@ 2010-01-19 19:15       ` Elnatan Reisner
  0 siblings, 0 replies; 6+ messages in thread
From: Elnatan Reisner @ 2010-01-19 19:15 UTC (permalink / raw)
  To: OCaml List

[-- Attachment #1: Type: text/plain, Size: 1719 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 2564 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-01-19 19:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-01-05 12:59 2147483648l < 2147483647l Matej Kosik
2010-01-05 13:17 ` [Caml-list] " Olivier Andrieu
2010-01-05 13:26 ` David Allsopp
2010-01-19 17:59   ` Goswin von Brederlow
2010-01-19 18:28     ` David Allsopp
2010-01-19 19:15       ` Elnatan Reisner

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