A C library for high-precision decimal arithmetic, supporting numbers up to ±79228162514264337593543950335 (equivalent to three 0xFFFFFFFFs).
Library is developed as a practice project at School 21 (formerly 42.fr).
- Custom decimal data type (
s21_decimal) for precise arithmetic - Arithmetic operations: addition, subtraction, multiplication, division
- Comparison functions
- Type conversion utilities
- Rounding and truncation operations
To use the s21_decimal library in your project:
- Clone the repo and build the library:
git clone https://github.com/makesnosense/s21_decimal.git
cd s21_decimal/src
make- Include the header file in your C project:
#include "s21_decimal.h"- Link against the built library when compiling your project. Replace
/path/to/library/directorywith the actual path wheres21_decimal.ais located:
gcc your_program.c -L/path/to/library/directory -ls21_decimal -o your_programgit clone https://github.com/makesnosense/s21_decimal.git
cd s21_decimal/srcTo run test suites (Check library required):
make testTo generate a code coverage report (requires GCOVR library):
make gcov_reportThe project includes a test generator (s21_decimal/tests_generator/decimal.ipynb) implemented as a Jupyter Notebook. To use it:
- Open decimal.ipynb in Jupyter Notebook or JupyterLab
- Run the cells to generate test cases
- Copy the generated test cases from tests_body.txt and addition.txt into your test files
int s21_add(s21_decimal value_1, s21_decimal value_2, s21_decimal* result)
int s21_sub(s21_decimal value_1, s21_decimal value_2, s21_decimal* result)
int s21_mul(s21_decimal value_1, s21_decimal value_2, s21_decimal* result)
int s21_div(s21_decimal value_1, s21_decimal value_2, s21_decimal* result)int s21_is_less(s21_decimal decimal_1, s21_decimal decimal_2)
int s21_is_less_or_equal(s21_decimal decimal_1, s21_decimal decimal_2)
int s21_is_greater(s21_decimal decimal_1, s21_decimal decimal_2)
int s21_is_greater_or_equal(s21_decimal decimal_1, s21_decimal decimal_2)
int s21_is_equal(s21_decimal decimal_1, s21_decimal decimal_2)
int s21_is_not_equal(s21_decimal decimal_1, s21_decimal decimal_2)int s21_from_int_to_decimal(int src, s21_decimal* dst)
int s21_from_float_to_decimal(float src, s21_decimal* dst)
int s21_from_decimal_to_int(s21_decimal src, int* dst)
int s21_from_decimal_to_float(s21_decimal src, float* dst)int s21_floor(s21_decimal value, s21_decimal* result)
int s21_round(s21_decimal value, s21_decimal* result)
int s21_truncate(s21_decimal value, s21_decimal* result)
int s21_negate(s21_decimal value, s21_decimal* result)