caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] OCaml max_int and min_int in C
@ 2016-07-18  8:43 Richard W.M. Jones
  2016-07-18  8:59 ` Jeremy Yallop
  0 siblings, 1 reply; 3+ messages in thread
From: Richard W.M. Jones @ 2016-07-18  8:43 UTC (permalink / raw)
  To: caml-list


This almost feels like an FAQ but I can't find the answer anywhere.

We need to check that an integer being returned from some C bindings
isn't larger or smaller than Pervasives.max_int / min_int.  Are there
C macros or constants containing these bounds?  Obviously we can
define our own but it seems better to use ones from OCaml if they
exist.

Rich.

-- 
Richard Jones

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

* Re: [Caml-list] OCaml max_int and min_int in C
  2016-07-18  8:43 [Caml-list] OCaml max_int and min_int in C Richard W.M. Jones
@ 2016-07-18  8:59 ` Jeremy Yallop
  2016-07-18  9:04   ` Richard W.M. Jones
  0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Yallop @ 2016-07-18  8:59 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: Caml List

On 18 July 2016 at 09:43, Richard W.M. Jones <rich@annexia.org> wrote:
> We need to check that an integer being returned from some C bindings
> isn't larger or smaller than Pervasives.max_int / min_int.  Are there
> C macros or constants containing these bounds?  Obviously we can
> define our own but it seems better to use ones from OCaml if they
> exist.

There are Max_long and Min_long:

   https://github.com/ocaml/ocaml/blob/ebbe205d/byterun/caml/mlvalues.h#L75-L76

Here's an example of Max_long being used to check that the return
value of lseek fits in an OCaml int:

   https://github.com/ocaml/ocaml/blob/ebbe205d/otherlibs/unix/lseek.c#L46-L50

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

* Re: [Caml-list] OCaml max_int and min_int in C
  2016-07-18  8:59 ` Jeremy Yallop
@ 2016-07-18  9:04   ` Richard W.M. Jones
  0 siblings, 0 replies; 3+ messages in thread
From: Richard W.M. Jones @ 2016-07-18  9:04 UTC (permalink / raw)
  To: Jeremy Yallop; +Cc: Caml List

On Mon, Jul 18, 2016 at 09:59:40AM +0100, Jeremy Yallop wrote:
> On 18 July 2016 at 09:43, Richard W.M. Jones <rich@annexia.org> wrote:
> > We need to check that an integer being returned from some C bindings
> > isn't larger or smaller than Pervasives.max_int / min_int.  Are there
> > C macros or constants containing these bounds?  Obviously we can
> > define our own but it seems better to use ones from OCaml if they
> > exist.
> 
> There are Max_long and Min_long:
> 
>    https://github.com/ocaml/ocaml/blob/ebbe205d/byterun/caml/mlvalues.h#L75-L76
> 
> Here's an example of Max_long being used to check that the return
> value of lseek fits in an OCaml int:
> 
>    https://github.com/ocaml/ocaml/blob/ebbe205d/otherlibs/unix/lseek.c#L46-L50

So there is!  I was grepping for MAX & INT, not Max and long :-/

Thanks,

Rich.

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

end of thread, other threads:[~2016-07-18  9:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-18  8:43 [Caml-list] OCaml max_int and min_int in C Richard W.M. Jones
2016-07-18  8:59 ` Jeremy Yallop
2016-07-18  9:04   ` Richard W.M. Jones

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