PZ
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 52 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 64 KiB |
@@ -0,0 +1,37 @@
|
|||||||
|
Задание 1:
|
||||||
|
|
||||||
|
int divide(int a, int b) {
|
||||||
|
return a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
Задание 2:
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
uint16_t swap_bytes_16(uint16_t val) {
|
||||||
|
return ((val & 0x00FF) << 8) | ((val & 0xFF00) >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
Задание 3:
|
||||||
|
|
||||||
|
см. скриншот
|
||||||
|
|
||||||
|
Задание 4:
|
||||||
|
|
||||||
|
int multiply_by_3(int x) { return x * 3; }
|
||||||
|
int multiply_by_5(int x) { return x * 5; }
|
||||||
|
int multiply_by_9(int x) { return x * 9; }
|
||||||
|
|
||||||
|
Задание 5:
|
||||||
|
|
||||||
|
int factorial(int n) {
|
||||||
|
int result = 1;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
result = result * i;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
https://godbolt.org/z/Yn6exTrEE
|
||||||
|
https://godbolt.org/z/GePnx7oz3
|
||||||
|
https://godbolt.org/z/GMWfYe4h5
|
||||||
|
https://godbolt.org/z/x65WvvEcs
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
Задание 3:
|
||||||
|
|
||||||
|
float Q_rsqrt( float number )
|
||||||
|
{
|
||||||
|
long i;
|
||||||
|
float x2, y;
|
||||||
|
const float threehalfs = 1.5F;
|
||||||
|
|
||||||
|
x2 = number * 0.5F;
|
||||||
|
y = number;
|
||||||
|
i = * ( long * ) &y; // evil floating point bit hack
|
||||||
|
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
|
||||||
|
y = * ( float * ) &i;
|
||||||
|
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration (1-я итерация Ньютона)
|
||||||
|
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
|
||||||
|
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
Строка i = * ( long * ) &y; интерпретирует 32-битное число с плавающей запятой (float) как обычное целое число (long), не меняя его биты в памяти.
|
||||||
|
За счет особенностей кодирования чисел float (стандарт IEEE 754) сдвиг бит вправо i >> 1 (деление на 2) и вычитание из «магического числа» 0x5F3759DF дает на удивление точное приближение для 1/корень из х
|
||||||
|
Строка y = * ( float * ) &i; возвращает биты обратно в тип float.
|
||||||
|
Строка с threehalfs выполняет одну итерацию численного метода Ньютона-Рафсона для уточнения полученного значения. Это уменьшает погрешность до ничтожных 0.17%.
|
||||||
|
|
||||||
Reference in New Issue
Block a user