caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Integer to floating point conversion errors
@ 2001-11-09 23:39 Post Office!~/sentmail
  2001-11-11 13:33 ` Berke Durak
  0 siblings, 1 reply; 4+ messages in thread
From: Post Office!~/sentmail @ 2001-11-09 23:39 UTC (permalink / raw)
  To: caml-list

I am getting negative results when I convert large integers to floating
point.  This happens on both i386 and my G4 laptop with OCaml 3.02.  The
code in question is this:

class prng = fun m i mo ->
        (fun (sd:int) ->
                object (self)
                        val mutable seed = sd
                        val mult = m
                        val incr = i
                        val modu = mo
                        method getseed = seed
                        method raw = (seed <- (seed*mult+incr)mod modu);
                                        float seed /. float modu
                        method gen low high = let range = high -. low in
                                        (self#raw *. range) +. low
                end);;
let myprng = new prng 274177 13 1073741824;;
let seeded = myprng 44;;

Then use seeded#raw a few times: the results are quite often negative,
which they should not be so far as I can see.

Can anyone advise me how to fix this?

Fred Ross
High Energy Physics Lab
University of Virginia
-------------------
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] 4+ messages in thread

* Re: [Caml-list] Integer to floating point conversion errors
  2001-11-09 23:39 [Caml-list] Integer to floating point conversion errors Post Office!~/sentmail
@ 2001-11-11 13:33 ` Berke Durak
  2001-11-11 15:35   ` Post Office!~/sentmail
  0 siblings, 1 reply; 4+ messages in thread
From: Berke Durak @ 2001-11-11 13:33 UTC (permalink / raw)
  To: Post Office!~/sentmail; +Cc: caml-list

On Fri, Nov 09, 2001 at 06:39:58PM -0500, Post Office!~/sentmail wrote:
> I am getting negative results when I convert large integers to floating
> point.  This happens on both i386 and my G4 laptop with OCaml 3.02.  The
> code in question is this:
> 
>                         method raw = (seed <- (seed*mult+incr)mod modu);
>                                         float seed /. float modu

The operator ``mod'' does not necessarily return positive values (depending
on your platform) :

        Objective Caml version 3.03 ALPHA
# (-1) mod 5;;
- : int = -1
--
Berke
-------------------
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] 4+ messages in thread

* Re: [Caml-list] Integer to floating point conversion errors
  2001-11-11 13:33 ` Berke Durak
@ 2001-11-11 15:35   ` Post Office!~/sentmail
  2001-11-11 16:34     ` Remi VANICAT
  0 siblings, 1 reply; 4+ messages in thread
From: Post Office!~/sentmail @ 2001-11-11 15:35 UTC (permalink / raw)
  To: caml-list

On Sun, 11 Nov 2001, Berke Durak wrote:

Pardon me, I wasn't clear.  Since incr and mult are assumed to be
positive, seed should always be positive.  Therefore a mod of C by a
positive number should always be positive.  The problems arise like the
following:

# seeded#raw;;
- : float = -0.011235290207
# seeded#getseed;;
- : int = 12063801
# float ( seeded#getseed );;
- : float = 12063801
# float ( seeded#getseed ) /. float( 1073741824 );;
- : float = -0.011235290207
# float (1073741824);;
- : float = -1073741824

> On Fri, Nov 09, 2001 at 06:39:58PM -0500, Post Office!~/sentmail wrote:
> > I am getting negative results when I convert large integers to floating
> > point.  This happens on both i386 and my G4 laptop with OCaml 3.02.  The
> > code in question is this:
> >
> >                         method raw = (seed <- (seed*mult+incr)mod modu);
> >                                         float seed /. float modu
>
> The operator ``mod'' does not necessarily return positive values (depending
> on your platform) :
>
>         Objective Caml version 3.03 ALPHA
> # (-1) mod 5;;
> - : int = -1
> --
> Berke
>
-------------------
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] 4+ messages in thread

* Re: [Caml-list] Integer to floating point conversion errors
  2001-11-11 15:35   ` Post Office!~/sentmail
@ 2001-11-11 16:34     ` Remi VANICAT
  0 siblings, 0 replies; 4+ messages in thread
From: Remi VANICAT @ 2001-11-11 16:34 UTC (permalink / raw)
  To: caml-list

Post Office!~/sentmail <fjr6b@cms.mail.virginia.edu> writes:

> On Sun, 11 Nov 2001, Berke Durak wrote:
> 
> Pardon me, I wasn't clear.  Since incr and mult are assumed to be
> positive, seed should always be positive.  Therefore a mod of C by a
> positive number should always be positive.  The problems arise like the
> following:
> 
> # seeded#raw;;
> - : float = -0.011235290207
> # seeded#getseed;;
> - : int = 12063801
> # float ( seeded#getseed );;
> - : float = 12063801
> # float ( seeded#getseed ) /. float( 1073741824 );;
> - : float = -0.011235290207
> # float (1073741824);;
> - : float = -1073741824

# max_int;;
- : int = 1073741823

so your int is bigger than max_int, and caml silently transform it in 
-1073741824 (there should be a warning)
float ( seeded#getseed ) /. 1073741824.0
should work.

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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] 4+ messages in thread

end of thread, other threads:[~2001-11-11 16:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-09 23:39 [Caml-list] Integer to floating point conversion errors Post Office!~/sentmail
2001-11-11 13:33 ` Berke Durak
2001-11-11 15:35   ` Post Office!~/sentmail
2001-11-11 16:34     ` Remi VANICAT

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