A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d6/db8/inv__sqrt_8cpp.html below:

TheAlgorithms/C++: math/inv_sqrt.cpp File Reference

Loading...

Searching...

No Matches

Implementation of the inverse square root Root. More...

#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>

Go to the source code of this file.

template<typename T = double, char iterations = 2> T  Fast_InvSqrt (T x)   for std::sqrt
template<typename T = double> T  Standard_InvSqrt (T number)   This is the function that calculates the fast inverse square root. The following code is the fast inverse square root with standard lib (cmath) More information can be found at LinkedIn
static void  test ()   Self-test implementations.
int  main ()   Main function.

Implementation of the inverse square root Root.

Two implementation to calculate inverse inverse root, from Quake III Arena (C++ version) and with a standard library (cmath). This algorithm is used to calculate shadows in Quake III Arena.

Definition in file inv_sqrt.cpp.

◆ Fast_InvSqrt()

template<typename T = double, char iterations = 2>

for std::sqrt

for assert for IO operations for numeric_limits

This is the function that calculates the fast inverse square root. The following code is the fast inverse square root implementation from Quake III Arena (Adapted for C++). More information can be found at Wikipedia

Template Parameters
T floating type iterations inverse square root, the greater the number of iterations, the more exact the result will be (1 or 2).
Parameters
Returns
the inverse square root

Definition at line 28 of file inv_sqrt.cpp.

28 {

29 using Tint = typename std::conditional<sizeof(T) == 8, std::int64_t,

30 std::int32_t>::type;

31 T y = x;

32 T x2 = y * 0.5;

33

34 Tint i =

35 *reinterpret_cast<Tint *>(&y);

36

37 i = (sizeof(T) == 8 ? 0x5fe6eb50c7b537a9 : 0x5f3759df) -

38 (i >> 1);

39

40 y = *reinterpret_cast<T *>(&i);

41

42 y = y * (1.5 - (x2 * y * y));

43 if (iterations == 2) {

44 y = y * (1.5 - (x2 * y * y));

45 }

46 return y;

47}

◆ main()

Main function.

Returns
0 on exit

Definition at line 87 of file inv_sqrt.cpp.

87 {

89 std::cout << "The Fast inverse square root of 36 is: "

91 std::cout << "The Fast inverse square root of 36 is: "

93 << std::endl;

94 std::cout << "The Fast inverse square root of 100 is: "

96 << " (With default template type and iterations: double, 2)"

97 << std::endl;

98 std::cout << "The Standard inverse square root of 36 is: "

100 std::cout << "The Standard inverse square root of 100 is: "

102 << " (With default template type: double)" << std::endl;

103}

T Standard_InvSqrt(T number)

This is the function that calculates the fast inverse square root. The following code is the fast inv...

static void test()

Self-test implementations.

T Fast_InvSqrt(T x)

for std::sqrt

◆ Standard_InvSqrt()

template<typename T = double>

T Standard_InvSqrt ( T number )

This is the function that calculates the fast inverse square root. The following code is the fast inverse square root with standard lib (cmath) More information can be found at LinkedIn

Template Parameters
Parameters
number value to calculate
Returns
the inverse square root

Definition at line 59 of file inv_sqrt.cpp.

59 {

60 T squareRoot = sqrt(number);

61 return 1.0f / squareRoot;

62}

◆ test()

Self-test implementations.

Returns
void

Definition at line 68 of file inv_sqrt.cpp.

68 {

69 const float epsilon = 1e-3f;

70

71

76

79

assert(std::fabs(

Fast_InvSqrt

(12.0f) - 0.288423) < epsilon);

81}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4