Giter Site home page Giter Site logo

decimal's Introduction

Custom Decimal Library (decimal.h)

This project involves the development of a custom decimal.h library, implementing various functions for decimal arithmetic. The library is written in C language, adheres to the C11 standard, and follows best practices in coding style and documentation.

Overview of Implemented decimal.h Functions

The decimal.h library provides functions for decimal arithmetic, supporting high-precision calculations. The following functions are implemented:

Arithmetic Operations

  1. s21_add - Addition of two decimal numbers.
  2. s21_sub - Subtraction of one decimal number from another.
  3. s21_mul - Multiplication of two decimal numbers.
  4. s21_div - Division of one decimal number by another.
  5. s21_mod - Modulus operation of two decimal numbers.

Comparison Functions

  1. s21_is_less - Check if one decimal number is less than another.
  2. s21_is_less_or_equal - Check if one decimal number is less than or equal to another.
  3. s21_is_greater - Check if one decimal number is greater than another.
  4. s21_is_greater_or_equal - Check if one decimal number is greater than or equal to another.
  5. s21_is_equal - Check if two decimal numbers are equal.
  6. s21_is_not_equal - Check if two decimal numbers are not equal.

Conversion Functions

  1. s21_from_int_to_decimal - Convert an integer to a decimal number.
  2. s21_from_decimal_to_int - Convert a decimal number to an integer (truncating the fractional part).
  3. s21_from_decimal_to_float - Convert a decimal number to a floating-point number (float).
  4. s21_from_float_to_decimal - Convert a floating-point number (float) to a decimal number.

Other Utility Functions

  1. s21_floor - Round a decimal number towards negative infinity.
  2. s21_round - Perform mathematical rounding of a decimal number.
  3. s21_truncate - Truncate the decimal part of a decimal number.
  4. s21_negate - Change the sign of a decimal number to its opposite.
  5. s21_bank_round - Perform banking rounding of a decimal number.

Implementation Details

  • The library adheres to the C11 standard and does not use outdated or legacy language constructions or library functions.
  • Coding style follows the Google style guidelines to ensure code consistency and readability.
  • Functions are prefixed with s21_ for clarity and to avoid naming conflicts.
  • Unit tests, using the Check library, ensure the correctness of each implemented function.
  • A comprehensive Makefile is provided for building the library and running tests, including targets like all, clean, test, and gcov_report.
  • Decimal numbers are represented internally using a binary format with the bits array, as specified in the provided example.
  • The library supports a wide range of decimal numbers, from -79,228,162,514,264,337,593,543,950,335 to +79,228,162,514,264,337,593,543,950,335.

Getting Started

To build the library and run tests, use the following commands:

make all  # Build the library and tests
make test  # Run unit tests
make gcov_report  # Generate a coverage report

This will compile the library, execute unit tests, and create a coverage report for analysis.

Usage

Include the s21_decimal.h header file in your C code to access the custom decimal functions:

#include "s21_decimal.h"

int main() {
    // Initialize decimal numbers
    s21_decimal num1, num2, result;
    s21_init_decimal(&num1);
    s21_init_decimal(&num2);

    // Set values for num1 and num2 (example)
    s21_from_int_to_decimal(10, &num1);
    s21_from_int_to_decimal(5, &num2);

    // Perform arithmetic operations
    s21_add(num1, num2, &result);
    s21_sub(num1, num2, &result);
    s21_mul(num1, num2, &result);
    s21_div(num1, num2, &result);

    // Convert to string and print (example)
    char result_str[50];
    s21_decimal_to_string(result, result_str, sizeof(result_str));
    printf("Result: %s\n", result_str);

    return 0;
}

Conclusion

The decimal.h library provides a reliable solution for high-precision decimal arithmetic in C projects. It ensures accurate calculations and follows best practices for code quality and maintainability.

For detailed information on each function and its usage, consult the project's source code.

decimal's People

Contributors

ranisminsafin avatar

Stargazers

Maksim avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.