caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Florian Hars <hars@bik-gmbh.de>
To: Sven Luther <sven.luther@wanadoo.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] ocaml, int32/64, bigarray and unsigned values ...
Date: Wed, 13 Apr 2005 08:54:56 +0200	[thread overview]
Message-ID: <425CC240.3050207@bik-gmbh.de> (raw)
In-Reply-To: <20050411153551.GA10362@pegasos>

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
     add (of_int32 n) 4294967296L

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

# #load "uInt64.cma";;
# 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.


  parent reply	other threads:[~2005-04-13  6:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-11  7:46 Sven Luther
2005-04-11 12:57 ` [Caml-list] " Eric Cooper
2005-04-11 15:35   ` Sven Luther
2005-04-11 16:13     ` Eric Cooper
2005-04-13  6:54     ` Florian Hars [this message]
2005-04-13 18:28     ` Ken Rose
2005-05-25  6:06     ` partition tables and ocaml Taras
2005-05-25  9:09       ` Sven Luther
2005-04-12 17:19   ` [Caml-list] ocaml, int32/64, bigarray and unsigned values Paul Snively

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=425CC240.3050207@bik-gmbh.de \
    --to=hars@bik-gmbh.de \
    --cc=caml-list@inria.fr \
    --cc=sven.luther@wanadoo.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).