This C library provides a set of functions for working with fractions. It allows you to perform various operations on fractions, including addition, subtraction, multiplication, division, exponentiation, square root, and more.
This library is designed to handle fractional calculations efficiently. It includes functions to create fractions, manipulate their numerator and denominator, perform arithmetic operations, and simplify fractions.
Create a new fraction with an initial value of 0.
Free the memory allocated for a fraction.
Set the numerator of the fraction with a double value.
Get the numerator of the fraction.
Set the denominator of the fraction with a double value.
Get the denominator of the fraction.
Set the fraction's value with a double.
Get the value of the fraction as a double.
Add two fractions and return the result as a simplified fraction.
Subtract two fractions and return the result as a simplified fraction.
Multiply two fractions and return the result as a simplified fraction.
Divide two fractions and return the result as a simplified fraction.
Raise a fraction to a power and return the result as a simplified fraction.
Calculate the square root of a fraction and return the result as a simplified fraction.
Simplify a fraction to its lowest terms.
Check if a fraction is an integer (i.e., its value is a whole number).
Calculate the greatest common divisor (GCD) of two integers.
To use this library in your C project, follow these steps:
-
Include the
fraction.h
header in your C source file.#include "fraction.h"
Make sure you have linked the library with your project during compilation.
gcc -o your_program your_program.c fraction.c -lm
Use the provided functions to work with fractions in your C code.
Here are some usage examples of the fraction manipulation functions:
// Create fractions
Fraction *frac1 = Fraction_Create();
Fraction *frac2 = Fraction_Create();
// Set numerator and denominator
Fraction_SetNum(frac1, 2.5);
Fraction_SetDen(frac2, 3);
// Perform arithmetic operations
Fraction result = Fraction_Sum(frac1, frac2);
Fraction result2 = Fraction_Mul(frac1, frac2);
// Print the results
printf("Sum: %d/%d\n", Fraction_GetNum(&result), Fraction_GetDen(&result));
printf("Product: %d/%d\n", Fraction_GetNum(&result2), Fraction_GetDen(&result2));
// Free memory
Fraction_Free(frac1);
Fraction_Free(frac2);
Feel free to adapt and use these functions in your C programs for efficient fraction handling.