From 43f679e50310635225538b87fb805c0bd2f64b64 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 1 Dec 2025 05:13:44 +0300 Subject: [PATCH 1/4] Add Makefile --- HW7/DoubleToExponential/Makefile | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 HW7/DoubleToExponential/Makefile diff --git a/HW7/DoubleToExponential/Makefile b/HW7/DoubleToExponential/Makefile new file mode 100644 index 0000000..8614e95 --- /dev/null +++ b/HW7/DoubleToExponential/Makefile @@ -0,0 +1,26 @@ +CC = gcc +CFLAGS = -Iinclude -Wall -Wextra + +SRC = main.c src/doubleToExponential.c +TEST_SRC = tests/tests.c + +OUT = program +TEST_OUT = testProgram + +all: $(OUT) + +$(OUT): $(SRC) + $(CC) $(CFLAGS) $(SRC) -o $(OUT) + +test: $(TEST_SRC) src/doubleToExponential.c + $(CC) $(CFLAGS) -DTEST_MAIN src/doubleToExponential.c $(TEST_SRC) -o $(TEST_OUT) + +clean: + rm -f $(OUT) $(TEST_OUT) + +format: + find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file -i + +format-check: + find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file --dry-run -Werror + From 22eca6261ade1871155ffdcce2262041e495f2a8 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 1 Dec 2025 05:13:54 +0300 Subject: [PATCH 2/4] Add main.c --- HW7/DoubleToExponential/main.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 HW7/DoubleToExponential/main.c diff --git a/HW7/DoubleToExponential/main.c b/HW7/DoubleToExponential/main.c new file mode 100644 index 0000000..f56abea --- /dev/null +++ b/HW7/DoubleToExponential/main.c @@ -0,0 +1,19 @@ +#include "include/doubleToExponential.h" +#include +#include + +int main() { + double number; + printf("Enter a number: "); + + if (scanf("%lf", &number) != 1) { + printf("Invalid input!\n"); + return 1; + } + + char* result = doubleToExponential(number); + printf("Result: %s\n", result); + free(result); + + return 0; +} From 5870a45d4a3c90208f067902665038e70aa50511 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 1 Dec 2025 05:14:08 +0300 Subject: [PATCH 3/4] Add double to exponential algorithm --- .../include/doubleToExponential.h | 3 ++ .../src/doubleToExponential.c | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 HW7/DoubleToExponential/include/doubleToExponential.h create mode 100644 HW7/DoubleToExponential/src/doubleToExponential.c diff --git a/HW7/DoubleToExponential/include/doubleToExponential.h b/HW7/DoubleToExponential/include/doubleToExponential.h new file mode 100644 index 0000000..2941678 --- /dev/null +++ b/HW7/DoubleToExponential/include/doubleToExponential.h @@ -0,0 +1,3 @@ +#pragma once + +char* doubleToExponential(double num); diff --git a/HW7/DoubleToExponential/src/doubleToExponential.c b/HW7/DoubleToExponential/src/doubleToExponential.c new file mode 100644 index 0000000..781dd9d --- /dev/null +++ b/HW7/DoubleToExponential/src/doubleToExponential.c @@ -0,0 +1,53 @@ +#include "../include/doubleToExponential.h" +#include +#include +#include +#include + +typedef union { + double value; + struct { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + uint64_t mantissa : 52; + uint64_t exponent : 11; + uint64_t sign : 1; +#else + uint64_t sign : 1; + uint64_t exponent : 11; + uint64_t mantissa : 52; +#endif + } parts; +} DoubleUnion; + +char* doubleToExponential(double num) { + DoubleUnion du; + du.value = num; + + int sign = du.parts.sign; + int codedExponent = du.parts.exponent; + uint64_t mantissa = du.parts.mantissa; + + if (codedExponent == 0 && mantissa == 0) { + char *result = malloc(9); + sprintf(result, "%c0.0*2^0", sign ? '-' : '+'); + return result; + } + + int exponent; + double normalizedMantissa; + + if (codedExponent == 0) { + // denormalized numbers + exponent = -1022; + normalizedMantissa = (double)mantissa / (1ULL << 52); + } + else { + // normalized numbers + exponent = codedExponent - 1023; + normalizedMantissa = 1.0 + (double)mantissa / (1ULL << 52); + } + + char* result = malloc(100); + sprintf(result, "%c%.20g*2^%d", sign ? '-' : '+', normalizedMantissa, exponent); + return result; +} From f68bc8f97842411421ca110a2fcdbafcd071740e Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 1 Dec 2025 05:14:25 +0300 Subject: [PATCH 4/4] Add tests --- HW7/DoubleToExponential/tests/tests.c | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 HW7/DoubleToExponential/tests/tests.c diff --git a/HW7/DoubleToExponential/tests/tests.c b/HW7/DoubleToExponential/tests/tests.c new file mode 100644 index 0000000..eff5f9f --- /dev/null +++ b/HW7/DoubleToExponential/tests/tests.c @@ -0,0 +1,66 @@ +#include "../include/doubleToExponential.h" +#include +#include +#include + +int assertOutput(const char* testName, double input, const char* expected) { + char* result = doubleToExponential(input); + + if (strcmp(result, expected) == 0) { + printf("Passed %s\n", testName); + free(result); + return 1; + } + else { + printf("Failed %s: got: %s, expected: %s\n", testName, result, expected); + free(result); + return 0; + } +} + +void testZero() { + printf("Zero values:\n"); + assertOutput("positive zero", 0.0, "+0.0*2^0"); + assertOutput("negative zero", -0.0, "-0.0*2^0"); +} + +void testPowersOfTwo() { + printf("Powers of two:\n"); + assertOutput("2^-1", 0.5, "+1*2^-1"); + assertOutput("2^0", 1.0, "+1*2^0"); + assertOutput("2^1", 2.0, "+1*2^1"); + assertOutput("2^2", 4.0, "+1*2^2"); + assertOutput("2^3", 8.0, "+1*2^3"); + assertOutput("2^4", 16.0, "+1*2^4"); + assertOutput("2^5", 32.0, "+1*2^5"); + assertOutput("negative power", -4.0, "-1*2^2"); +} + +void testKnownValues() { + printf("Known values:\n"); + assertOutput("negative decimal", -2.5, "-1.25*2^1"); + assertOutput("positive decimal", 10.0, "+1.25*2^3"); +} + +void runTests() { + printf("Running tests:\n"); + + testZero(); + printf("\n"); + + testPowersOfTwo(); + printf("\n"); + + testKnownValues(); + printf("\n"); + + printf("All tests finished.\n"); +} + +#ifdef TEST_MAIN +int main() +{ + runTests(); + return 0; +} +#endif