Добавление ПР 2 по сис.прог.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
float Q_rsqrt(float number) {
|
||||
long i;
|
||||
float x2, y;
|
||||
const float threehalfs = 1.5F;
|
||||
|
||||
x2 = number * 0.5F;
|
||||
y = number;
|
||||
|
||||
i = * ( long * ) &y;
|
||||
i = 0x5f3759df - ( i >> 1 );
|
||||
y = * ( float * ) &i;
|
||||
|
||||
y = y * ( threehalfs - ( x2 * y * y ) );
|
||||
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
float Q_rsqrt_safe(float number) {
|
||||
uint32_t i;
|
||||
float y = number;
|
||||
const float threehalfs = 1.5F;
|
||||
float x2 = number * 0.5F;
|
||||
|
||||
union { float f; uint32_t i; } converter;
|
||||
converter.f = y;
|
||||
i = converter.i;
|
||||
|
||||
i = 0x5f3759df - (i >> 1);
|
||||
|
||||
converter.i = i;
|
||||
y = converter.f;
|
||||
|
||||
y = y * (threehalfs - (x2 * y * y));
|
||||
return y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float x = 4.0f;
|
||||
printf("1/sqrt(4.0) = %f\n", Q_rsqrt_safe(x)); // Ожидаем 0.5
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user