Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/hw12_doubleExp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 4.0)
project(hw12_doubleExp C)

set(CMAKE_C_STANDARD 17)

add_executable(hw12_doubleExp main.c doubleExp.c doubleExp.h)
76 changes: 76 additions & 0 deletions src/hw12_doubleExp/doubleExp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "doubleExp.h"
#include <stdio.h>
#include "doubleExp.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот это лишнее


typedef union DoubleUnion {
double value;
unsigned char bytes[8];
} DoubleUnion;

// Получение бита i
static int getBitI(const DoubleUnion *d, int i)
{
int byte = i / 8;
int bit = i % 8;

// Пролучаем нужный байт и сдвигаем его на позицию 0
char neededByte = d->bytes[byte];
char changePosition = neededByte >> bit;

return changePosition & 1;
}

void doubleExp(double x)
{
DoubleUnion d;
d.value = x;

// Определяем знак
int signI = getBitI(&d, 63);
char sign;
if (signI)
sign = '-';
else
sign = '+';

int exp = 0;
for (int i = 62; i >= 52; i--) {
exp = (exp << 1) | getBitI(&d, i);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь удобнее было бы воспользоваться константными масками (или двумя сдвигами на нужную величину), а не собирать число по одному биту

}

// Достаем мантиссу
long long mantissaBits = 0;
for (int i = 51; i >= 0; i--) {
mantissaBits = (mantissaBits << 1) | getBitI(&d, i);
}

// Проверка для INF и NaN
if (exp == 2047) {
if (mantissaBits == 0) {
printf("%cINF\n", sign);
} else {
printf("NaN\n");
}
return;
}

// Проверка для +0 и -0
if (exp == 0 && mantissaBits == 0) {
printf("%c0\n", sign);
return;
}

double mantissa = 1.0;
double fractionPart = 0.5;
for (int i = 51; i >= 0; i--) {
if (getBitI(&d, i)) {
mantissa += fractionPart;
}
fractionPart /= 2.0;
}

// Так как в double порядок смещен на 1023, то получаем настоящий вычетанием
int power = exp - 1023;

printf("Результат: %c%.20f*2^%d\n", sign, mantissa, power);
}
3 changes: 3 additions & 0 deletions src/hw12_doubleExp/doubleExp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdio.h>

void doubleExp(double x);
12 changes: 12 additions & 0 deletions src/hw12_doubleExp/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
#include "doubleExp.h"

int main(void)
{
double x;
printf("Введите число: ");
scanf("%lf", &x);
doubleExp(x);

return 0;
}