caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Ints and overflow...
@ 2001-07-13 19:25 Krishnaswami, Neel
  2001-07-15 11:17 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Krishnaswami, Neel @ 2001-07-13 19:25 UTC (permalink / raw)
  To: 'caml-list@inria.fr'

Hello,

I need a really fast pseudo-rng (quality is second to speed) and 
decided to implement a linear congruential generator, since they
are really simple and there are literally reams of papers describing
good parameter choices.

However, this has led me directly into the zone of integer overflow.
Basically, I don't understand what I can expect to be portable, and
I really don't understand what OCaml does -- what is the tagging 
scheme, and how does that affect the overflow results I can expect?

For example: Is the integer tag 0 or 1? Does it vary by platform? Can 
the results vary by how Caml chooses to optimize adds? Does Caml 
promise anything at all about the results of an overflowing integer
add? Each of these seems quite simple but the sum total leaves me 
quite lost.

I would be very grateful if someone could relieve my confusion.

--
Neel Krishnaswami
neelk@cswcasa.com
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Ints and overflow...
  2001-07-13 19:25 [Caml-list] Ints and overflow Krishnaswami, Neel
@ 2001-07-15 11:17 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2001-07-15 11:17 UTC (permalink / raw)
  To: Krishnaswami, Neel; +Cc: 'caml-list@inria.fr'

> However, this has led me directly into the zone of integer overflow.
> Basically, I don't understand what I can expect to be portable, and
> I really don't understand what OCaml does -- what is the tagging 
> scheme, and how does that affect the overflow results I can expect?

As long as you're programming in well-typed Caml, the exact details of
the tagging scheme do not matter.  You get regular 2-complement modulo
arithmetic, just like in Java and most if not all C compilers, with 31
bits of precision, or 63 bits if you're lucky enought to run on a
64-bit processor.

> For example: Is the integer tag 0 or 1?

It's a 1 in the least significant bit, but again you can't observe
this from well-typed Caml.

> Does it vary by platform?

No, but same comment as above.

> Can the results vary by how Caml chooses to optimize adds?

Normally, no.  If it varied, that would be due to a bug in the tagging
optimization scheme of the ocamlopt compiler.

> Does Caml promise anything at all about the results of an
> overflowing integer add?

Yes: it wraps around, i.e. you get the result modulo 2^31 (or 2^63 on
a 64-bit platform).

Hope this answers your questions.

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-07-15 11:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-13 19:25 [Caml-list] Ints and overflow Krishnaswami, Neel
2001-07-15 11:17 ` 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).