Fractions represent a part of a whole. They are written in the form of a/b, where 'a' is the numerator (the number of parts we have) and 'b' is the denominator (the total number of equal parts the whole is divided into). For instance, 3/4 means we have 3 parts out of a total of 4 equal parts.
Fractions are fundamental in mathematics and appear in various real-world scenarios, from cooking recipes (e.g., 1/2 cup of flour) and measurements (e.g., 3/8 inch) to financial calculations and scientific data. This calculator helps you perform basic arithmetic operations on two fractions: addition, subtraction, multiplication, and division.
How the Calculator Works:
1. Addition and Subtraction of Fractions
To add or subtract fractions, they must have a common denominator. If they don't, we find the Least Common Multiple (LCM) of the denominators.
The general formula for adding fractions a/b and c/d is:
(a*d + c*b) / (b*d)
For subtraction, it's:
(a*d – c*b) / (b*d)
The calculator finds the LCM of the denominators to ensure the most simplified form directly or simplifies the result afterwards.
For example, to add 3/4 and 1/2:
LCM of 4 and 2 is 4.
Convert 1/2 to 2/4.
3/4 + 2/4 = (3+2)/4 = 5/4.
2. Multiplication of Fractions
Multiplying fractions is straightforward. You multiply the numerators together and the denominators together.
The formula for multiplying a/b by c/d is:
(a*c) / (b*d)
For example, to multiply 3/4 by 1/2:
(3*1) / (4*2) = 3/8.
3. Division of Fractions
Dividing fractions involves multiplying the first fraction by the reciprocal of the second fraction. The reciprocal of a fraction c/d is d/c.
The formula for dividing a/b by c/d is:
(a/b) / (c/d) = (a/b) * (d/c) = (a*d) / (b*c)
For example, to divide 3/4 by 1/2:
(3/4) / (1/2) = (3/4) * (2/1) = (3*2) / (4*1) = 6/4, which simplifies to 3/2.
Simplification of Fractions
The calculator also simplifies the resulting fraction to its lowest terms by dividing both the numerator and the denominator by their Greatest Common Divisor (GCD).
function gcd(a, b) {
var num1 = Math.abs(a);
var num2 = Math.abs(b);
while (num2) {
var temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return num1;
}
function simplifyFraction(numerator, denominator) {
if (denominator === 0) {
return "Undefined (division by zero)";
}
if (numerator === 0) {
return "0/1";
}
var commonDivisor = gcd(numerator, denominator);
var simplifiedNumerator = numerator / commonDivisor;
var simplifiedDenominator = denominator / commonDivisor;
// Ensure the denominator is positive
if (simplifiedDenominator < 0) {
simplifiedNumerator = -simplifiedNumerator;
simplifiedDenominator = -simplifiedDenominator;
}
return simplifiedNumerator + "/" + simplifiedDenominator;
}
function calculateFraction() {
var num1 = parseInt(document.getElementById("numerator1").value);
var den1 = parseInt(document.getElementById("denominator1").value);
var num2 = parseInt(document.getElementById("numerator2").value);
var den2 = parseInt(document.getElementById("denominator2").value);
var operation = document.getElementById("operation").value;
var resultElement = document.getElementById("result");
// Input validation
if (isNaN(num1) || isNaN(den1) || isNaN(num2) || isNaN(den2)) {
resultElement.textContent = "Please enter valid numbers.";
return;
}
if (den1 === 0 || den2 === 0) {
resultElement.textContent = "Denominator cannot be zero.";
return;
}
var resultNumerator = 0;
var resultDenominator = 0;
if (operation === "add") {
resultNumerator = (num1 * den2) + (num2 * den1);
resultDenominator = den1 * den2;
} else if (operation === "subtract") {
resultNumerator = (num1 * den2) – (num2 * den1);
resultDenominator = den1 * den2;
} else if (operation === "multiply") {
resultNumerator = num1 * num2;
resultDenominator = den1 * den2;
} else if (operation === "divide") {
if (num2 === 0) {
resultElement.textContent = "Cannot divide by zero.";
return;
}
resultNumerator = num1 * den2;
resultDenominator = den1 * num2;
}
var simplifiedResult = simplifyFraction(resultNumerator, resultDenominator);
resultElement.textContent = "Result: " + simplifiedResult;
}