Skip to content

Commit 2ffcff1

Browse files
siddhant2002siriak
andauthored
Fix fast inverse sqrt (fixes TheAlgorithms#3199) (TheAlgorithms#3228)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent 12a4e27 commit 2ffcff1

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
6+
/** Program description - To find out the inverse square root of the given number*/
7+
8+
/** Wikipedia Link - https://en.wikipedia.org/wiki/Fast_inverse_square_root */
9+
10+
11+
package com.thealgorithms.maths;
12+
13+
public class FastInverseSqrt {
14+
public static boolean inverseSqrt(float number) {
15+
float x = number;
16+
float xhalf = 0.5f * x;
17+
int i = Float.floatToIntBits(x);
18+
i = 0x5f3759df - (i >> 1);
19+
x = Float.intBitsToFloat(i);
20+
x = x * (1.5f - xhalf * x * x);
21+
return x == (float)((float)1/(float)Math.sqrt(number));
22+
}
23+
24+
/**
25+
* Returns the inverse square root of the given number upto 6 - 8 decimal places.
26+
* calculates the inverse square root of the given number and returns true if calculated answer matches with given answer else returns false
27+
*/
28+
29+
30+
public static boolean inverseSqrt(double number) {
31+
double x = number;
32+
double xhalf = 0.5d * x;
33+
long i = Double.doubleToLongBits(x);
34+
i = 0x5fe6ec85e7de30daL - (i >> 1);
35+
x = Double.longBitsToDouble(i);
36+
for (int it = 0; it < 4; it++) {
37+
x = x * (1.5d - xhalf * x * x);
38+
}
39+
x *= number;
40+
return x == 1/Math.sqrt(number);
41+
}
42+
/**
43+
* Returns the inverse square root of the given number upto 14 - 16 decimal places.
44+
* calculates the inverse square root of the given number and returns true if calculated answer matches with given answer else returns false
45+
*/
46+
}
47+
48+
49+
/**
50+
* OUTPUT :
51+
* Input - number = 4522
52+
* Output: it calculates the inverse squareroot of a number and returns true with it matches the given answer else returns false.
53+
* 1st approach Time Complexity : O(1)
54+
* Auxiliary Space Complexity : O(1)
55+
* Input - number = 4522
56+
* Output: it calculates the inverse squareroot of a number and returns true with it matches the given answer else returns false.
57+
* 2nd approach Time Complexity : O(1)
58+
* Auxiliary Space Complexity : O(1)
59+
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.maths;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
6+
7+
public class FastInverseSqrtTests {
8+
@Test
9+
void testForOneElement()
10+
{
11+
assertFalse(FastInverseSqrt.inverseSqrt(1332));
12+
// calls for the 2nd inverse method
13+
}
14+
@Test
15+
void testForsecond()
16+
{
17+
assertFalse(FastInverseSqrt.inverseSqrt(1332f));
18+
// calls for the 1st inverse method
19+
}
20+
21+
@Test
22+
void testForThird()
23+
{
24+
assertFalse(FastInverseSqrt.inverseSqrt(1));
25+
}
26+
27+
@Test
28+
void testForFourth()
29+
{
30+
assertFalse(FastInverseSqrt.inverseSqrt(1f));
31+
}
32+
33+
@Test
34+
void testForFifth()
35+
{
36+
assertFalse(FastInverseSqrt.inverseSqrt(4522));
37+
}
38+
39+
@Test
40+
void testForSixth()
41+
{
42+
assertFalse(FastInverseSqrt.inverseSqrt(4522f));
43+
}
44+
45+
@Test
46+
void testForSeventh()
47+
{
48+
assertFalse(FastInverseSqrt.inverseSqrt(21));
49+
}
50+
51+
@Test
52+
void testForEighth()
53+
{
54+
assertFalse(FastInverseSqrt.inverseSqrt(21f));
55+
}
56+
}

0 commit comments

Comments
 (0)