Coder Social home page Coder Social logo

fract_t's Introduction

fract_t

fract_t is a simple data structure for C99+, consisting of two variables of type long long, representing the numerator .num and the denominator .den of a fraction. It allows you to work with rational numbers of small sizes and has certain limitations. The library includes all the necessary arithmetic operations, as well as functions for converting to basic types float and double.

Goal

This library was not created for computational purposes and has many limitations. It is primarily designed to demonstrate how familiar fractions would look on a computer and approximately how they would work with them. The main.c file serves as an example and is not part of the library itself.

Variable Declaration

fract_t = { , };

If the number is negative, assign the numerator .num a negative value. Do not declare variables with a denominator = 0; otherwise, any operations with this number will result in 0/0.

If the numerator's value is = 0, some operations with it will also return 0/0. This is a special value that prevents further errors.

Limitations

The actual valid range of values for both the numerator and denominator is 9223372036854775807; however, for arithmetic operations, this range is significantly smaller.

For multiplication and division, a limiter is used: MAX_FRACTION_MUL

MAX_FRACTION_MUL = 3000000000L / -3000000000L

For addition and subtraction, a limiter is used: MAX_FRACTION_ADD

MAX_FRACTION_ADD = 4600000000000000000L / -4600000000000000000L

The result of calculations may be greater or less than these limits. Limiters exist to ensure that you do not exceed the maximum allowed value for long long.

If a number exceeds the limiter, performing arithmetic operations will return 0/0.

Arithmetic Operations

  • fract_mul(fst, scd) Product of two numbers
  • fract_div(fst, scd) Quotient of two numbers
  • fract_add(fst, scd) Sum of two numbers
  • fract_sub(fst, scd) Difference of two numbers

When performing arithmetic operations, the fraction is automatically reduced.

  • fract_red(*fct, val) Reduces the numerator and denominator by the value val.

Conversion to Floating-Point Numbers

The functions fract_to_float() and fract_to_double() allow you to convert fract_t to a decimal fraction.

Additional Information

You can also obtain the integer and fractional parts of a number using the fract_get_int() and fract_get_fract() functions. The integer part is returned with the type long long, and the fractional part with the type fract_t. You can read about the other functions in the header file fract_t.h.

fract_t's People

Contributors

creat0s 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.