logo

const class

sys::Float

sys::Obj
  sys::Num
    sys::Float
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //    2 Dec 05  Brian Frank  Creation
   7  //   11 Oct 06  Brian Frank  Rename Real to Float
   8  //
   9  
  10  **
  11  ** Float is used to represent a 64-bit floating point number.
  12  **
  13  const final class Float : Num
  14  {
  15  
  16  //////////////////////////////////////////////////////////////////////////
  17  // Constructor
  18  //////////////////////////////////////////////////////////////////////////
  19  
  20    **
  21    ** Make a Float for the specified 64-bit representation according
  22    ** IEEE 754 floating-point double format bit layout.  This method is
  23    ** paired with `Float.bits`.
  24    **
  25    static Float makeBits(Int bits)
  26  
  27    **
  28    ** Make a Float for the specified 32-bit representation according
  29    ** IEEE 754 floating-point single format bit layout.  This method is
  30    ** paired with `Float.bits32`.
  31    **
  32    static Float makeBits32(Int bits)
  33  
  34    **
  35    ** Parse a Str into a Float.  Representations for infinity and
  36    ** not-a-number are "-INF", "INF", "NaN".  This string format matches
  37    ** the lexical representation of Section 3.2.5 of XML Schema Part 2.
  38    ** If invalid format and checked is false return null, otherwise throw
  39    ** ParseErr.
  40    **
  41    ** TODO: need spec - follow XML Schema literal definition
  42    **
  43    static Float fromStr(Str s, Bool checked := true)
  44  
  45    **
  46    ** Private constructor.
  47    **
  48    private new make()
  49  
  50    **
  51    ** Float value for positive infinity.
  52    **
  53    const static Float posInf
  54  
  55    **
  56    ** Float value for negative infinity.
  57    **
  58    const static Float negInf
  59  
  60    **
  61    ** Float value for Not-A-Number.
  62    **
  63    const static Float nan
  64  
  65    **
  66    ** Float value for e which is the base of natural logarithms.
  67    **
  68    const static Float e
  69  
  70    **
  71    ** Float value for pi which is the ratio of the
  72    ** circumference of a circle to its diameter.
  73    **
  74    const static Float pi
  75  
  76  //////////////////////////////////////////////////////////////////////////
  77  // Obj Overrides
  78  //////////////////////////////////////////////////////////////////////////
  79  
  80    **
  81    ** Return true if same float value.  Unlike Java, NaN equals NaN.
  82    **
  83    override Bool equals(Obj obj)
  84  
  85    **
  86    ** Return if this Float is approximately equal to the given Float by the
  87    ** specified tolerance.  If tolerance is null, then it is computed
  88    ** using the magnitude of the two Floats.  It is useful for comparing
  89    ** Floats since often they loose a bit of precision during manipulation.
  90    ** This method is equivalent to:
  91    **   if (tolerance == null) tolerance = min(abs(this/1e6), abs(r/1e6))
  92    **   (this - r).abs < tolerance
  93    **
  94    Bool approx(Float r, Float tolerance := null)
  95  
  96    **
  97    ** Compare based on floating point value.
  98    **
  99    override Int compare(Obj obj)
 100  
 101    **
 102    ** Return bits().
 103    **
 104    override Int hash()
 105  
 106  //////////////////////////////////////////////////////////////////////////
 107  // Methods
 108  //////////////////////////////////////////////////////////////////////////
 109  
 110    **
 111    ** Negative of this.  Shortcut is -a.
 112    **
 113    Float negate()
 114  
 115    **
 116    ** Multiply this with b.  Shortcut is a*b.
 117    **
 118    Float star(Float b)
 119  
 120    **
 121    ** Divide this by b.  Shortcut is a/b.
 122    **
 123    Float slash(Float b)
 124  
 125    **
 126    ** Return remainder of this divided by b.  Shortcut is a%b.
 127    **
 128    Float percent(Float b)
 129  
 130    **
 131    ** Add this with b.  Shortcut is a+b.
 132    **
 133    Float plus(Float b)
 134  
 135    **
 136    ** Subtract b from this.  Shortcut is a-b.
 137    **
 138    Float minus(Float b)
 139  
 140    **
 141    ** Increment by one.  Shortcut is ++a or a++.
 142    **
 143    Float increment()
 144  
 145    **
 146    ** Decrement by one.  Shortcut is --a or a--.
 147    **
 148    Float decrement()
 149  
 150  /////////////////////////////////////////////////////////////////////////
 151  // Math
 152  //////////////////////////////////////////////////////////////////////////
 153  
 154    **
 155    ** Return the absolute value of this float.  If this value is
 156    ** positive then return this, otherwise return the negation.
 157    **
 158    Float abs()
 159  
 160    **
 161    ** Return the smaller of this and the specified Float values.
 162    **
 163    Float min(Float that)
 164  
 165    **
 166    ** Return the larger of this and the specified Float values.
 167    **
 168    Float max(Float that)
 169  
 170    **
 171    ** Returns the smallest whole number greater than or equal
 172    ** to this number.
 173    **
 174    Float ceil()
 175  
 176    **
 177    ** Returns the largest whole number less than or equal to
 178    ** this number.
 179    **
 180    Float floor()
 181  
 182    **
 183    ** Returns the nearest whole number to this number.
 184    **
 185    Float round()
 186  
 187    **
 188    ** Return e raised to this power.
 189    **
 190    Float exp()
 191  
 192    **
 193    ** Return natural logarithm of this number.
 194    **
 195    Float log()
 196  
 197    **
 198    ** Return base 10 logarithm of this number.
 199    **
 200    Float log10()
 201  
 202    **
 203    ** Return this value raised to the specified power.
 204    **
 205    Float pow(Float pow)
 206  
 207    **
 208    ** Return square root of this value.
 209    **
 210    Float sqrt()
 211  
 212  //////////////////////////////////////////////////////////////////////////
 213  // Trig
 214  //////////////////////////////////////////////////////////////////////////
 215  
 216    **
 217    ** Return the arc cosine.
 218    **
 219    Float acos()
 220  
 221    **
 222    ** Return the arc sine.
 223    **
 224    Float asin()
 225  
 226    **
 227    ** Return the arc tangent.
 228    **
 229    Float atan()
 230  
 231    **
 232    ** Converts rectangular coordinates (x, y) to polar (r, theta).
 233    **
 234    static Float atan2(Float y, Float x)
 235  
 236    **
 237    ** Return the cosine of this angle in radians.
 238    **
 239    Float cos()
 240  
 241    **
 242    ** Return the hyperbolic cosine.
 243    **
 244    Float cosh()
 245  
 246    **
 247    ** Return sine of this angle in radians.
 248    **
 249    Float sin()
 250  
 251    **
 252    ** Return hyperbolic sine.
 253    **
 254    Float sinh()
 255  
 256    **
 257    ** Return tangent of this angle in radians.
 258    **
 259    Float tan()
 260  
 261    **
 262    ** Return hyperbolic tangent.
 263    **
 264    Float tanh()
 265  
 266    **
 267    ** Convert this angle in radians to an angle in degrees.
 268    **
 269    Float toDegrees()
 270  
 271    **
 272    ** Convert this angle in degrees to an angle in radians.
 273    **
 274    Float toRadians()
 275  
 276  /////////////////////////////////////////////////////////////////////////
 277  // Num
 278  //////////////////////////////////////////////////////////////////////////
 279  
 280    **
 281    ** Convert this Float to an Int.
 282    **
 283    override Int toInt()
 284  
 285    **
 286    ** Return this.
 287    **
 288    override Float toFloat()
 289  
 290  //////////////////////////////////////////////////////////////////////////
 291  // Conversion
 292  //////////////////////////////////////////////////////////////////////////
 293  
 294    **
 295    ** Return 64-bit representation according IEEE 754 floating-point
 296    ** double format bit layout.  This method is paired with `Float.makeBits`.
 297    **
 298    Int bits()
 299  
 300    **
 301    ** Return 32-bit representation according IEEE 754 floating-point
 302    ** single format bit layout.  This method is paired with `Float.makeBits32`.
 303    **
 304    Int bits32()
 305  
 306    **
 307    ** Get string representation according to the lexical representation defined
 308    ** by Section 3.2.5 of XML Schema Part 2.  Representations for infinity and
 309    ** not-a-number are "-INF", "INF", "NaN".
 310    **
 311    override Str toStr()
 312  
 313  }