Unfortunately, this is still not enough in Ocaml 3.08 for the unsigned
integers needed by Sebastien:
Objective Caml version 3.08
# 4294967295l;;
Integer literal exceeds the range of representable integers of type int32
#
--
Boris
-------------------
To unsubscribe, mail caml-lis...@inria.fr Archives: http://caml.inr=
ia.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr=
/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Thank you very much for your help !
For sure, this new language feature is great.
However, I have another question:
With OCaml 3.08.0 :
# let max = 0xffffffffl;;
val max : int32 = -1l
# Printf.printf "%lu\n" max;;
4294967295
- : unit = ()
# 4294967295l;;
Integer literal exceeds the range of representable integers of type int32
Is this error the expected behaviour ?
Thanks in advance,
Sébastien.
> Yes: it's called int32. Think about it: being "unsigned" or "signed"
> is not a property of the representation (it will be 32 binary digits
> in both cases), it's just that some operations (division, modulus and
> comparisons) interpret those bits differently.
Sorry, I was caught by the fact they are signed.
However, I'm not sure it is so easy. Indeed, the representation and the r=
ange
of represented integers has to be exactly the same than with the uint32_t
type of C, to allow me to use some constants defined in C in Caml program=
s.
Consider for instance the following definition in a .h C header file :
#define UINT32_MAX (4294967295U)
Now I'd like to define the same thing in Caml:
# let uint32_max = Int32.of_string "4294967295";;
Exception: Failure "int_of_string".
How can I solve this problem ?
Thanks,
Yes: it's called int32. Think about it: being "unsigned" or "signed"
is not a property of the representation (it will be 32 binary digits
in both cases), it's just that some operations (division, modulus and
comparisons) interpret those bits differently.
> All I'd need is an abstract type defining these integers, plus some
> functions to go from strings to 32-bits unsigned integers and vice-versa.
Here you are:
module UInt32 = struct
type t = int32
let of_string = Int32.of_string
let to_string n = Printf.sprintf "%lu" n
end
You can throw in some arithmetic operations as well:
let add = Int32.add
let sub = Int32.sub
let mul = Int32.mul
etc. As I said above, the only operations that need to be treated
specially are comparisons and division/modulus.
Hope this helps,
- Xavier Leroy
-------------------
To unsubscribe, mail caml-lis...@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/