This program contains two primary functions:
getfrac: A function to approximate a floating-point number as a rational number (fraction) within a given maximum denominator.format: A utility function to format a number, representing it as either a decimal number, a fraction, a square root, or a multiple of π (pi) depending on the input and tolerance.
Purpose:
The getfrac function takes a floating-point number n and attempts to approximate it as a fraction numerator/denominator, with the denominator constrained by the maximum value maxden.
Parameters:
n(double): The number to approximate.maxden(long long): The maximum allowed denominator for the fraction.numerator(long long*): Pointer to store the computed numerator.denominator(long long*): Pointer to store the computed denominator.
Core Logic:
- Initializes a 2x2 matrix
mused for storing intermediary results in the approximation process. - The algorithm runs in a loop, finding terms for numerator and denominator until the denominator exceeds the
maxdenconstraint. - Two potential approximations are computed:
err1for the fraction from the initial matrix.err2for a second fraction obtained by adjusting the initial matrix.
- The function returns the error (difference between the original number and the approximation), and the better of the two approximations is used.
Usage:
This function is used to convert a floating-point number into a rational approximation when exact representation is not possible.
Purpose:
The format function attempts to represent a given number in a readable format, such as a fraction, square root, or multiple of π. It can handle cases where the input number is close to a whole number or fits a specific format within the tolerance provided.
Parameters:
N(double): The number to be formatted.TOL(double): The tolerance used to determine if two values are considered equal.res(char*): A string buffer to store the result of the formatted number.
Core Logic:
- Handles special cases like
NaN(not a number) and numbers that are extremely large or small. - If the number is an integer within the given tolerance, it is printed as an integer.
- The function attempts to:
- Represent the number as a fraction.
- Check if the square of the number can be expressed as a fraction.
- Check if the number can be represented as a multiple of π.
- The function selects the representation with the smallest error compared to the original number.
- If none of the special formats (fraction, square root, or multiple of π) fit within the tolerance, the function outputs the number in decimal format.
Usage:
The format function is useful in situations where mathematical expressions (such as fractions or multiples of π) need to be output in a readable way, especially in computational tools or symbolic computation environments.
//...Header files and others
int main()
{
double n;
printf("Enter: ");
scanf("%lf",&n);
char res[100];
printf("Formatted: %s",format(n,1e-7,NULL));//replace NULL with res if you want to store output
return 0;
}C:\Users\Downloads\C>gcc num-format.c -o num.exe
C:\Users\Downloads\C>num.exe
Enter: 8.306623863
Formatted: sqrt(69)