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

ocaml, int32/64, bigarray and unsigned values ...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2005-04-13 (06:55) From: Florian Hars Subject: Re: [Caml-list] ocaml, int32/64, bigarray and unsigned values ...
Sven Luther wrote:

> Ok, but it would be nice to tell this black on white in the manual.

It is. "All arithmetic operations over int64 are taken modulo 2^64" ;-)

The core of a solution would be something like

open Int64

let of_uint32 n =
if Int32.compare n Int32.zero >= 0 then
of_int32 n
else

let compare a b =
if logand min_int (logxor a b) = min_int then
if logand min_int a = min_int then 1 else (-1)
else
Pervasives.compare a b

let to_string n = format "%u" n

But you can't enter numbers larger than Int64.max_int that way, this would
require a C function and a fix to the compiler:

Objective Caml version 3.08.3

# Int64.compare Int64.max_int (Int64.succ Int64.max_int);;
- : int = 1
# UInt64.compare Int64.max_int (Int64.succ Int64.max_int);;
- : int = -1
# Int64.of_int32 (Int32.min_int);;
- : int64 = -2147483648L
# UInt64.of_uint32 (Int32.min_int);;
- : int64 = 2147483648L
# UInt64.of_uint32 (Int32.of_int (-1));;
- : int64 = 4294967295L
# UInt64.to_string (-1L);;
- : string = "18446744073709551615"
# Int64.of_string (UInt64.to_string (-1L));;
Exception: Failure "int_of_string".
# Int64.to_string 18446744073709551615L;;
Integer literal exceeds the range of representable integers of type int64

See http://pauillac.inria.fr/bin/caml-bugs/feature%20wish?id=2928
Strangely enough, ocaml accepts max_int + 1 although that literal exceeds the
range of representable integers, too:

# UInt64.to_string Int64.min_int;;
- : string = "9223372036854775808"
# Int64.to_string 9223372036854775808L;;
- : string = "-9223372036854775808"
# Int64.to_string 9223372036854775809L;;
Integer literal exceeds the range of representable integers of type int64
# min_int;;
- : int = -4611686018427387904
# 4611686018427387904;;
- : int = -4611686018427387904
# 4611686018427387905;;
Integer literal exceeds the range of representable integers of type int

Yours, Florian.