[MLton-devel] Real.fromLargeInt

Stephen Weeks MLton@mlton.org
Tue, 22 Oct 2002 15:48:21 -0700


Can someone please check my work on Real.fromLargeInt, which has been
long missing from MLton's basis library.

structure Real =
   struct
      open Real
	 
      val fromLargeInt: IntInf.int -> real =
	 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
	    fun pos (i: IntInf.int): real = 
	       let
		  val exp: Int.int = IntInf.log2 i
	       in
		  if Int.< (exp, Int.- (valOf Int.precision, 1))
		     then Real.fromInt (IntInf.toInt i)
		  else if Int.>= (exp, 1024)
		     then Real.posInf
		  else
		     let
			val shift = Int.- (exp, m)
			val man: IntInf.int =
			   if Int.>= (shift, 0)
			      then IntInf.quot (i, IntInf.pow (two, shift))
			   else IntInf.* (i, IntInf.pow (two, Int.~ shift))
			(* 2^m <= man < 2^(m+1) *)
			val (q, r) = IntInf.quotRem (man, IntInf.pow (two, half))
			fun fromManExp (man, exp) =
			   Real.fromManExp
			   {man = Real.fromInt (IntInf.toInt man),
			    exp = exp}
		     in
			fromManExp (q, Int.+ (half, shift))
			+ fromManExp (r, shift)
		     end
	       end
	 in
	    fn i =>
	    case IntInf.compare (i, IntInf.fromInt 0) of
	       LESS => ~ (pos (IntInf.~ i))
	     | EQUAL => 0.0
	     | GREATER => pos i
	 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