-
Notifications
You must be signed in to change notification settings - Fork 0
Hw12 Double micro #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DolzhenkoAlexa
wants to merge
4
commits into
main
Choose a base branch
from
HW12_Dolzhenko_DoubleMicro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include "doubleExp.h" | ||
| #include <stdio.h> | ||
| #include "doubleExp.h" | ||
|
|
||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #include <stdio.h> | ||
|
|
||
| void doubleExp(double x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вот это лишнее