This commit is contained in:
Miloslav Ciz 2025-02-08 19:09:45 +01:00
parent 16b8c6accd
commit 85321afd67
35 changed files with 1971 additions and 1919 deletions

18
sqrt.md
View file

@ -141,6 +141,24 @@ int32_t sqrtApprox(int32_t x)
}
```
If we need [floating point](float.md) square root, we can very well use [numerical methods](numerical.md): for example wanting to find square root of *a* we may apply Newton's method to arrive at the formula:
*x[n + 1] = 1/2 * (x[n] + a / x[n])*
into which we may substitute the initial estimate as *x[0]* and then just keep evaluating the series until the error becomes small enough. Here this is implemented in C (to keep it simple performing a fixed number of iterations that showed to give accurate results even for high values):
```
double sqrtF(double x)
{
double r = x / 2 + 1;
for (int i = 0; i < 20; ++i)
r = 0.5 * (r + x / r);
return r;
}
```
## See Also
- [logarithm](log.md)