generated from admin/template
Загрузить файлы в «works/SysProg/ПР 2»
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 58 KiB |
@@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int is_nan_custom(float f) {
|
||||||
|
uint32_t bits;
|
||||||
|
memcpy(&bits, &f, sizeof(float));
|
||||||
|
|
||||||
|
uint32_t exp = (bits >> 23) & 0xFF;
|
||||||
|
uint32_t mantissa = bits & 0x7FFFFF;
|
||||||
|
|
||||||
|
return (exp == 0xFF) && (mantissa != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
float a = 0.0f / 0.0f;
|
||||||
|
float b = 1.0f / 0.0f;
|
||||||
|
float c = 3.14f;
|
||||||
|
|
||||||
|
printf("0.0/0.0 is NaN: %d\n", is_nan_custom(a)); // 1 (true)
|
||||||
|
printf("1.0/0.0 is NaN: %d\n", is_nan_custom(b)); // 0 (false)
|
||||||
|
printf("3.14 is NaN: %d\n", is_nan_custom(c)); // 0 (false)
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
@@ -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