Decimal To Fraction

To convert a decimal to fraction we express the decimal as a ratio of two integers (a numerator and a denominator). The process can be different depending on the decimal's characteristics.

Decimal to Fraction and Normalization

Decimal:



Algorithm

Step 1: Identify the Decimal

Let's say you have a decimal number, such as 0.75.

Step 2: Determine the Place Value

Count the number of decimal places in the given decimal. In our example (0.75), there are two decimal places.

Step 3: Convert the Decimal to a Fraction

  1. Write down the decimal as a fraction with the decimal part as the numerator and the denominator as a power of 10 corresponding to the number of decimal places.

For 0.75:

  • Numerator = 75 (the decimal part)
  • Denominator = 10^2 (since there are 2 decimal places)

So, 0.75 as a fraction is 75/100.

Step 4: Simplify the Fraction

If possible, simplify the fraction by finding the greatest common divisor (GCD) of the numerator and the denominator, and then dividing both by the GCD.

In our example, the GCD of 75 and 100 is 25. Dividing both the numerator and denominator by 25 results in:

75 ÷ 25 = 3
100 ÷ 25 = 4

So, the simplified fraction is 3/4.

Keep in mind that not all decimals can be easily converted to fractions, especially repeating decimals (those that have a repeating pattern of digits after the decimal point). In such cases, the conversion might involve more complex techniques, such as using algebra or long division.

Example in C: Convert from decimal to fraction

Let's create an ANSI C function decimalToFraction. It will convert a decimal number to a fraction and then simplify it. When we run it, the output should look like this:

Decimal-to-Fraction-output
void decimalToFraction(double decimal) {
    // Scale the decimal to handle more precision
    int numerator = (int)(decimal * 1000000);
    int denominator = 1000000;

    // Find the greatest common divisor
    int gcd = getGCD(numerator, denominator);

    // Simplify the fraction
    numerator /= gcd;
    denominator /= gcd;

    printf("Decimal: %.6lf\n", decimal);
    printf("Fraction: %d / %d\n", numerator, denominator);

    // Simplify the fraction further
	int wholePart = numerator / denominator;
	int remainderNumerator = numerator % denominator;
    printf("Simplified Fraction: %d and %d/%d\n", wholePart, remainderNumerator, denominator);
}

Here's a step-by-step explanation of what the function does:

  1. Scaling the Decimal: The decimal input is scaled by multiplying it by 1000000. This is done to work with more precision, as integer arithmetic is used for the fraction calculations.
  2. Initializing Variables: Two integers, numerator and denominator, are initialized. The numerator is set to the scaled integer value of the decimal, and the denominator is set to 1000000. This represents the fraction as a ratio of integers.
  3. Finding Greatest Common Divisor (GCD): The getGCD function is called to find the greatest common divisor (GCD) between the numerator and the denominator. This step is crucial for simplifying the fraction.
  4. Simplifying the Fraction: The numerator and denominator are divided by their GCD to simplify the fraction.
  5. Printing the Decimal and Fraction: The original decimal value is printed with 6 decimal places of precision. Then, the simplified fraction is printed in the form of numerator / denominator.
  6. Simplifying the Fraction Further: The function calculates the whole part and remainder of the fraction after simplification. The whole part is found by dividing the numerator by the denominator using integer division, and the remainder is found using the modulus operator. This gives a mixed number representation of the simplified fraction (e.g., 3 and 1/4).
  7. Printing the Simplified Fraction: The simplified fraction in mixed number form is printed, showing the whole part, remainder, and denominator.

Overall, this function performs the conversion from a decimal to a fraction, simplifies the fraction, and provides both the raw fraction form and a mixed number representation of the simplified fraction.

Finding Greatest Common Divisor

int getGCD(int a, int b) {
    if (b == 0) {
        return a;
    }
    return getGCD(b, a % b);
}

The C function getGCD calculates the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm, a well-known algorithm for finding the greatest common divisor efficiently. Here's how the function works:

  1. Base Case: The function checks if the second integer b is equal to zero. If it is, the function returns the value of the first integer a. This is the base case of the recursion, where the GCD is found.
  2. Recursive Case: If b is not equal to zero, the function recursively calls itself with arguments (b, a % b). This step replaces a with b and b with the remainder of the division a / b. The recursive call continues until b becomes zero (base case is reached).
  3. Returning the GCD: As the recursion unwinds (due to the base case), the GCD value is propagated back through the recursive calls. Eventually, the function returns the GCD of the original a and b provided.

The algorithm utilizes the fact that the GCD of two numbers remains the same if the larger number is replaced by the remainder of its division by the smaller number. This property allows the algorithm to iteratively reduce the problem until it reaches the base case, where the GCD is found.

Overall, the function efficiently calculates the GCD of two integers using recursion and the properties of the Euclidean algorithm.

GitHub

A whole working program, using the functions above is located at my github page.

To get all examples use:

    git clone https://github.com/ProgrammingSimpleSteps/c-examples.git

   Search this site: