Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Integer to floating point conversion errors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Post Office!~/sentmail <fjr6b@c...>
Subject: [Caml-list] Integer to floating point conversion errors
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