From b9e7d60be4a37cb9ea15874c19ebbc70591ae51b Mon Sep 17 00:00:00 2001 From: DougDevelopz Date: Sat, 23 Jul 2022 08:29:27 -0400 Subject: [PATCH 1/3] Implemented Fast Inverse Sqrt Method --- .../com/thealgorithms/maths/FastSqrt.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/FastSqrt.java diff --git a/src/main/java/com/thealgorithms/maths/FastSqrt.java b/src/main/java/com/thealgorithms/maths/FastSqrt.java new file mode 100644 index 000000000000..515b371aafa4 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/FastSqrt.java @@ -0,0 +1,43 @@ +package com.thealgorithms.maths; + +public class FastSqrt { + + /** + * To better understand this algorithm, please refer to the following link: https://en.wikipedia.org/wiki/Fast_inverse_square_root + */ + + /** + * Returns the inverse square root of the given number. + * + * @param number the number + * @return the inverse square root of the given number + */ + public static float inverseSqrt(float number) { + float x = number; + float xhalf = 0.5f * x; + int i = Float.floatToIntBits(x); + i = 0x5f3759df - (i >> 1); + x = Float.intBitsToFloat(i); + x = x * (1.5f - xhalf * x * x); + return x; + } + + /** + * Returns the inverse square root of the given number in a more accurate way. + * + * @param number the number + * @return the inverse square root of the given number + */ + public static double inverseSqrt(double number) { + double x = number; + double xhalf = 0.5d * x; + long i = Double.doubleToLongBits(x); + i = 0x5fe6ec85e7de30daL - (i >> 1); + x = Double.longBitsToDouble(i); + for (int it = 0; it < 4; it++) { + x = x * (1.5d - xhalf * x * x); + } + x *= number; + return x; + } +} From da3de8dfc5e687bc1a749f548334f296d3e0b63b Mon Sep 17 00:00:00 2001 From: DougDevelopz Date: Sun, 24 Jul 2022 11:47:09 -0400 Subject: [PATCH 2/3] Implemented Automated Tests --- .../com/thealgorithms/maths/FastSqrt.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/com/thealgorithms/maths/FastSqrt.java b/src/main/java/com/thealgorithms/maths/FastSqrt.java index 515b371aafa4..c55b2f0d6f2e 100644 --- a/src/main/java/com/thealgorithms/maths/FastSqrt.java +++ b/src/main/java/com/thealgorithms/maths/FastSqrt.java @@ -2,6 +2,35 @@ public class FastSqrt { + public static void main(String[] args){ + //Using Inverse Square Root Method #1 + float floatResult = 0f; + double doubleResult = 0d; + long start, mid, end; //Times to be benchmarked + start = System.currentTimeMillis(); + for (float x = 1F; x < 4_000_000F; x += 0.25F) { + floatResult = inverseSqrt(x); + } + mid = System.currentTimeMillis(); + + for(double x = 1D; x < 4_000_000D; x += 0.25D){ + doubleResult = inverseSqrt(x); + } + end = System.currentTimeMillis(); + + long controlled = mid - start; + + long experimental = end - mid; + + long percentageError = 100 * (controlled - experimental) / controlled; + + System.out.println("Controlled Difference: " + controlled + "ms With result: " + floatResult); + + System.out.println("Experimental Difference: " + experimental + "ms With result: " + doubleResult); + + System.out.println("Method 2 of Inverse Square Root Was " + percentageError + "% faster than Method 1 of Inverse Square Root"); + } + /** * To better understand this algorithm, please refer to the following link: https://en.wikipedia.org/wiki/Fast_inverse_square_root */ From ecc04f2a80dae51f453a28bd00bff358fa72d0fd Mon Sep 17 00:00:00 2001 From: DougDevelopz Date: Sun, 24 Jul 2022 11:59:59 -0400 Subject: [PATCH 3/3] Removed A Comment From The FastSqrt Class --- src/main/java/com/thealgorithms/maths/FastSqrt.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/FastSqrt.java b/src/main/java/com/thealgorithms/maths/FastSqrt.java index c55b2f0d6f2e..4df7b654b190 100644 --- a/src/main/java/com/thealgorithms/maths/FastSqrt.java +++ b/src/main/java/com/thealgorithms/maths/FastSqrt.java @@ -3,7 +3,6 @@ public class FastSqrt { public static void main(String[] args){ - //Using Inverse Square Root Method #1 float floatResult = 0f; double doubleResult = 0d; long start, mid, end; //Times to be benchmarked