caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] 32-bit unsigned integers
@ 2004-07-26 17:15 Sébastien Hinderer
  2004-07-26 17:49 ` Corey O'Connor
  0 siblings, 1 reply; 6+ messages in thread
From: Sébastien Hinderer @ 2004-07-26 17:15 UTC (permalink / raw)
  To: caml-list

Hi again,

> 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 range
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 programs.

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,
Sébastien.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.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


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

* Re: [Caml-list] 32-bit unsigned integers
  2004-07-26 17:15 [Caml-list] 32-bit unsigned integers Sébastien Hinderer
@ 2004-07-26 17:49 ` Corey O'Connor
  2004-07-26 18:09   ` Boris Yakobowski
  2004-07-26 18:32   ` Sébastien Hinderer
  0 siblings, 2 replies; 6+ messages in thread
From: Corey O'Connor @ 2004-07-26 17:49 UTC (permalink / raw)
  To: caml-list

Ah! I asked the same question just a bit ago!

A "language extension" to Ocaml (Make sure you have 3.07.11 or
greater) is to directly assign 32 bit integers without using
Int32.of_string. Just add an "l" to the end of the constant. (Lower
case "L" if your font is strange and it looks like a 1 | I) It's like
so:

# let blah = 0x8FFFFFFFl;;
val blah : int32 = -1879048193l
#

-Corey 

On Mon, 26 Jul 2004 19:15:18 +0200, Sébastien Hinderer
<sebastien.hinderer@ens-lyon.org> wrote:
> Hi again,
> 
> > 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 range
> 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 programs.
> 
> 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,
> Sébastien.
> 
> 
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.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
> 


-- 
-Corey O'Connor

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.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


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

* Re: [Caml-list] 32-bit unsigned integers
  2004-07-26 17:49 ` Corey O'Connor
@ 2004-07-26 18:09   ` Boris Yakobowski
  2004-07-26 18:32   ` Sébastien Hinderer
  1 sibling, 0 replies; 6+ messages in thread
From: Boris Yakobowski @ 2004-07-26 18:09 UTC (permalink / raw)
  To: Corey O'Connor; +Cc: caml-list

On Mon, Jul 26, 2004 at 10:49:25AM -0700, Corey O'Connor wrote:
> On Mon, 26 Jul 2004 19:15:18 +0200, Sébastien Hinderer
> <sebastien.hinderer@ens-lyon.org> wrote:
> > Now I'd like to define the same thing in Caml:
> > 
> > # let uint32_max = Int32.of_string "4294967295";;
> > Exception: Failure "int_of_string".
>
> Ah! I asked the same question just a bit ago!
> 
> A "language extension" to Ocaml (Make sure you have 3.07.11 or
> greater) is to directly assign 32 bit integers without using
> Int32.of_string. Just add an "l" to the end of the constant. (Lower
> case "L" if your font is strange and it looks like a 1 | I) It's like
> so:
> 
> # let blah = 0x8FFFFFFFl;;
> val blah : int32 = -1879048193l
> #

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-list-request@inria.fr Archives: http://caml.inria.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


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

* Re: [Caml-list] 32-bit unsigned integers
  2004-07-26 17:49 ` Corey O'Connor
  2004-07-26 18:09   ` Boris Yakobowski
@ 2004-07-26 18:32   ` Sébastien Hinderer
  1 sibling, 0 replies; 6+ messages in thread
From: Sébastien Hinderer @ 2004-07-26 18:32 UTC (permalink / raw)
  To: caml-list

Hi again,

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.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.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


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

* Re: [Caml-list] 32-bit unsigned integers
  2004-07-26 15:38 Sébastien Hinderer
@ 2004-07-26 16:46 ` Xavier Leroy
  0 siblings, 0 replies; 6+ messages in thread
From: Xavier Leroy @ 2004-07-26 16:46 UTC (permalink / raw)
  To: caml-list

> Does a type representing unsigned 32-bits integer exist in Caml ?

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-list-request@inria.fr Archives: http://caml.inria.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


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

* [Caml-list] 32-bit unsigned integers
@ 2004-07-26 15:38 Sébastien Hinderer
  2004-07-26 16:46 ` Xavier Leroy
  0 siblings, 1 reply; 6+ messages in thread
From: Sébastien Hinderer @ 2004-07-26 15:38 UTC (permalink / raw)
  To: caml-list

Dear all,

Does a type representing unsigned 32-bits integer exist in Caml ?
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.
Thanks,
Sébastien.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.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


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

end of thread, other threads:[~2004-07-26 19:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-26 17:15 [Caml-list] 32-bit unsigned integers Sébastien Hinderer
2004-07-26 17:49 ` Corey O'Connor
2004-07-26 18:09   ` Boris Yakobowski
2004-07-26 18:32   ` Sébastien Hinderer
  -- strict thread matches above, loose matches on Subject: below --
2004-07-26 15:38 Sébastien Hinderer
2004-07-26 16:46 ` Xavier Leroy

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