// See https://en.wikipedia.org/wiki/Fast_inverse_square_root for more information. public static double InvSqrt(double x) { LongDouble i = new LongDouble(x); double x2 = x * 0.5, y; const double threehalfs = 1.5; i.long_value = inv_sqrt_magic_number_64_bit - (i.long_value >> 1); y = i.double_value; y *= threehalfs - (x2 * y * y); // 1st iteration y *= threehalfs - (x2 * y * y); // 2nd iteration // y *= threehalfs - (x2 * y * y); // 3nd iteration return y; }
/// <summary> /// Gets the binary representation of the specified double-precision floating point value. /// </summary> /// <param name="value">The value to convert to bytes.</param> /// <returns>The array that stores the binary representation.</returns> public static byte[] GetBytes( double value ) { var converter = new LongDouble(); converter.DoubleValue = value; return GetBytes(converter.LongValue); }
/// <summary> /// Gets the double-precision floating point value from the binary representation. /// </summary> /// <param name="array">The array that stores the binary representation.</param> /// <param name="startIndex">The starting position within <paramref name="array"/>.</param> /// <returns>The value found.</returns> public static double ToDouble( byte[] array, int startIndex ) { var converter = new LongDouble(); converter.LongValue = ToInt64(array, startIndex); return converter.DoubleValue; }
/// <summary> /// Gets the binary representation of the specified double-precision floating point value. /// </summary> /// <param name="value">The value to convert to bytes.</param> /// <param name="array">The array that stores the binary representation.</param> /// <param name="startIndex">The starting position within <paramref name="array"/>.</param> public static void GetBytes( double value, byte[] array, int startIndex ) { var converter = new LongDouble(); converter.DoubleValue = value; GetBytes(converter.LongValue, array, startIndex); }