[MLton-devel] Real.toLargeInt

Stephen Weeks MLton@mlton.org
Tue, 22 Oct 2002 19:32:40 -0700


If someonce could check my attempt at Real.toLargeInt as well that
would be great.  Thanks.

      val toLargeInt: IEEEReal.rounding_mode -> real -> IntInf.int =
	 let
	    val m = 52 (* The number of mantissa bits in 64 bit IEEE 854. *)
	    val half = Int.quot (m, 2)
	    val two = IntInf.fromInt 2
	    val twoPowHalf = IntInf.pow (two, half)
	    datatype z = datatype IEEEReal.rounding_mode
	    datatype z = datatype IEEEReal.float_class
	 in
	    fn mode => fn x =>
 	    (IntInf.fromInt (toInt mode x)
 	     handle Overflow =>
	     case class x of
		INF => raise Overflow
	      | _ => 
		   let
		      fun pos (x, mode) =
			 let 
			    val {frac, whole} = split x
			    val extra =
			       if mode = TO_NEAREST
				  andalso Real.== (frac, 0.5)
				  then
				     if Real.== (0.5, realMod (whole / 2.0))
					then 1
				     else 0
			       else IntInf.fromInt (toInt mode frac)
			    val {man, exp} = toManExp whole
			    (* 1 <= man < 2 *)
			    val man = fromManExp {man = man, exp = half}
			    (* 2^half <= man < 2^(half+1) *)
			    val {frac = lower, whole = upper} = split man
			    val upper = IntInf.* (IntInf.fromInt (floor upper),
						  twoPowHalf)
			    (* 2^m <= upper < 2^(m+1) *)
			    val {whole = lower, ...} =
			       split (fromManExp {man = lower, exp = half})
			    (* 0 <= lower < 2^half *)
			    val lower = IntInf.fromInt (floor lower)
			    val int = IntInf.+ (upper, lower)
			    (* 2^m <= int < 2^(m+1) *)
			    val shift = Int.- (exp, m)
			    val int =
			       if Int.>= (shift, 0)
				  then IntInf.* (int, IntInf.pow (2, shift))
			       else IntInf.quot (int, IntInf.pow (2, Int.~ shift))
			 in
			    IntInf.+ (int, extra)
			 end
		   in
		      if x > 0.0
			 then pos (x, mode)
		      else IntInf.~ (pos (~ x,
					  case mode of
					     TO_NEAREST => TO_NEAREST
					   | TO_NEGINF => TO_POSINF
					   | TO_POSINF => TO_NEGINF
					   | TO_ZERO => TO_ZERO))
		   end)
	 end


-------------------------------------------------------
This sf.net emial is sponsored by: Influence the future 
of Java(TM) technology. Join the Java Community 
Process(SM) (JCP(SM)) program now. 
http://ad.doubleclick.net/clk;4699841;7576301;v?http://www.sun.com/javavote
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel