Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/src/raw-data/double.sml
ViewVC logotype

View of /trunk/sml3d/src/raw-data/double.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 897 - (download) (annotate)
Fri May 14 16:47:48 2010 UTC (9 years, 5 months ago) by jhr
File size: 2754 byte(s)
  Moved floating-point modules into raw-data library, so they can be shared
  by the OpenAL module.
(* double.sml
 *
 * COPYRIGHT (c) 2008 John Reppy (http://www.cs.uchicago.edu/~jhr)
 * All rights reserved.
 *
 * Double-precision floating-point numbers.
 *)

structure Double : sig

    type double = Real64.real

    include FLT where type flt = double

  end = struct

    type double = Real64.real
    type flt = double

    open Real64.Math

    val epsilon : flt = 1.0e~7

    val M_E : double		= 2.71828182845904523536028747135266250   (* e *)
    val M_LOG2E : double	= 1.44269504088896340735992468100189214   (* log 2e *)
    val M_LOG10E : double 	= 0.434294481903251827651128918916605082  (* log 10e *)
    val M_LN2 : double		= 0.693147180559945309417232121458176568  (* log e2 *)
    val M_LN10 : double		= 2.30258509299404568401799145468436421   (* log e10 *)
    val M_PI : double		= 3.14159265358979323846264338327950288   (* pi *)
    val M_PI_2 : double		= 1.57079632679489661923132169163975144   (* pi/2 *)
    val M_PI_4 : double		= 0.785398163397448309615660845819875721  (* pi/4 *)
    val M_1_PI : double		= 0.318309886183790671537767526745028724  (* 1/pi *)
    val M_2_PI : double		= 0.636619772367581343075535053490057448  (* 2/pi *)
    val M_2_SQRTPI : double	= 1.12837916709551257389615890312154517   (* 2/sqrt(pi) *)
    val M_SQRT2 : double	= 1.41421356237309504880168872420969808   (* sqrt(2) *)
    val M_SQRT1_2 : double	= 0.707106781186547524400844362104849039  (* 1/sqrt(2) *)

    val M_POS_INF : double	= Real64.posInf				  (* +infinity *)
    val M_NEG_INF : double	= Real64.negInf				  (* -infinity *)

    val abs = Real64.abs
    val min = Real64.min
    val max = Real64.max
  
    fun clip {min : flt, max : flt} x =
	  if (x < min) then min
	  else if (x > max) then max
	  else x
    val clamp = clip {min=0.0, max=1.0}
    fun lerp (v1, t : flt, v2) = (1.0-t)*v1 + t*v2
    fun step (edge : flt, x) : flt = if (x < edge) then 0.0 else 1.0
    fun smoothStep (min, max, x : flt) =
	  if (x < min) then 0.0
	  else if (max < x) then 1.0
	  else let
	    val t = (x - min) / (max - min)
	    in
	      t*t*(3.0 - 2.0*t)	(* 2t^3 + 3t^2 *)
	    end

    local
      val m_pi_180 = M_PI / 180.0
      val m_180_Pi = 180.0 * M_1_PI
    in
    fun toRadians deg = m_pi_180 * deg
    fun toDegrees rad = m_180_Pi * rad
    end (* local *)

  (* integer conversions *)
    fun fromInt i = Real64.fromLargeInt(Int32.toLarge i)
    fun fromLong i = Real64.fromLargeInt(Int64.toLarge i)
    val roundToInt = Real64.round
    val frac = Real64.realMod
    val trunc = Real64.realTrunc
    val round = Real64.realRound
    val floor = Real64.realFloor
    val ceil = Real64.realCeil

    val split = Real64.split

  (* string conversions *)
    val fromString = Real64.fromString
    val toString = Real64.toString

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0