< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Common.Components.Math
Assembly: ReFlex.Core.Common
File(s): D:\a\reflex\reflex\library\src\Core\Common\Components\Math.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 77
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FastSqrt(...)100%22100%
InverseSqrt(...)100%22100%
Remap(...)100%11100%
ExponentialMapping(...)100%11100%
LogarithmicMapping(...)100%11100%

File(s)

D:\a\reflex\reflex\library\src\Core\Common\Components\Math.cs

#LineLine coverage
 1using System.Runtime.InteropServices;
 2
 3namespace ReFlex.Core.Common.Components
 4{
 5    /// <summary>
 6    /// Offers some useful and fast mathematical functions
 7    /// </summary>
 8    public class Math
 9    {
 10        #region methods
 11
 12        /// <summary>
 13        /// very roughly Approximates the square root of a number z. Variance: approx. 5%.
 14        /// </summary>
 15        /// <param name="z">The radicand</param>
 16        /// <returns>The square root of a number z, <see cref="float.NaN"/> if z is negative.</returns>
 17        public static float FastSqrt(float z)
 1318        {
 1419            if (z == 0) return 0;
 20            FloatIntUnion u;
 1221            u.tmp = 0;
 1222            u.f = z;
 1223            u.tmp -= 1 << 23;
 1224            u.tmp >>= 1;
 1225            u.tmp += 1 << 29;
 1226            return u.f;
 1327        }
 28
 29        /// <summary>
 30        /// Approximates the square root of a number z. Variance: approx. 2%
 31        /// It’s a tad slower than the first (though still nearly 2x as fast as Math.Sqrt()), but much more accurate.
 32        /// </summary>
 33        /// <param name="z">The radicand</param>
 34        /// <returns>The square root of a number z, <see cref="float.NegativeInfinity"/> if z is negative.</returns>
 35        public static float InverseSqrt(float z)
 1336        {
 1437            if (z == 0) return 0;
 38            FloatIntUnion u;
 1239            u.tmp = 0;
 1240            var half = 0.5f * z;
 1241            u.f = z;
 1242            u.tmp = 0x5f375a86 - (u.tmp >> 1);
 1243            u.f = u.f * (1.5f - half * u.f * u.f);
 1244            return u.f * z;
 1345        }
 46
 47        #endregion
 48
 49        [StructLayout(LayoutKind.Explicit)]
 50        private struct FloatIntUnion
 51        {
 52            [FieldOffset(0)]
 53            public float f;
 54
 55            [FieldOffset(0)]
 56            public int tmp;
 57        }
 58
 59        /// <summary>
 60        /// Remaps the specified value from one range to another range.
 61        /// </summary>
 62        /// <param name="value">The value.</param>
 63        /// <param name="from1">The from1.</param>
 64        /// <param name="to1">The to1.</param>
 65        /// <param name="from2">The from2.</param>
 66        /// <param name="to2">The to2.</param>
 67        /// <returns>The remapped value.</returns>
 68        public static double Remap(double value, double from1, double to1, double from2, double to2)
 769            => (value - from1) / (to1 - from1) * (to2 - from2) + from2;
 70
 71        public static double ExponentialMapping(double input) =>
 472            System.Math.Exp((input) - 1) / (System.Math.E - 1);
 73
 74        public static double LogarithmicMapping(double input) =>
 675            System.Math.Log(input + 1, System.Math.E) / System.Math.Log(2, System.Math.E);
 76    }
 77}