Пример #1
0
        /// <summary>
        /// Return E raised to specified power.
        /// 2x times faster than System.Math.Exp, but gives 1% error.
        /// </summary>
        /// <param name="power">Target power.</param>
        public static float Exp(float power)
        {
            var c = new DoubleInt64();

            c.Int64 = (long)(1512775 * power + 1072632447) << 32;
            return((float)c.Double);
        }
Пример #2
0
    public static int Compare(double x, double y)
    {
        // Very important that cmp(x, y) == cmp(y, x)
        if (Double.IsNaN(x) || Double.IsNaN(y))
        {
            return(1);
        }
        if (Double.IsInfinity(x) || Double.IsInfinity(y))
        {
            return(1);
        }
        var ix   = DoubleInt64.Reinterpret(x);
        var iy   = DoubleInt64.Reinterpret(y);
        var diff = Math.Abs(ix - iy);

        if (diff < MaxUnitsInTheLastPlace)
        {
            return(0);
        }
        if (ix < iy)
        {
            return(-1);
        }
        else
        {
            return(1);
        }
    }
Пример #3
0
        /// <summary>
        /// Return data raised to specified power.
        /// 4x times faster than System.Math.Pow, 6x times faster than System.Math.Pow, but gives 3% error.
        /// </summary>
        /// <param name="data">Data to raise.</param>
        /// <param name="power">Target power.</param>
        public static float PowInaccurate(float data, float power)
        {
            var c = new DoubleInt64();

            c.Double = data;
            c.Int64  = (long)(power * ((c.Int64 >> 32) - 1072632447) + 1072632447) << 32;
            return((float)c.Double);
        }