generated from admin/template
Локальная копия работ
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
float Q_rsqrt(float number) {
|
||||
uint32_t i;
|
||||
float x2, y;
|
||||
const float threehalfs = 1.5F;
|
||||
|
||||
x2 = number * 0.5F;
|
||||
y = number;
|
||||
|
||||
// 1 чтение float как целое число
|
||||
memcpy(&i, &y, sizeof(i));
|
||||
|
||||
// 2 битовая магия вычисление первого приближения
|
||||
i = 0x5F3759DF - (i >> 1);
|
||||
|
||||
// 3 обратное превращение битов в float
|
||||
memcpy(&y, &i, sizeof(y));
|
||||
|
||||
// 4 шаг метода Ньютона-Рафсона
|
||||
y = y * (threehalfs - (x2 * y * y));
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x = 16.0f;
|
||||
|
||||
// 1 / sqrt(16) = 1 / 4 = 0.25
|
||||
float result = Q_rsqrt(x);
|
||||
|
||||
printf("1 / sqrt(%f) = %f\n", x, result);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user