Subtraction Calculator for Fractions
How to Subtract Fractions
Subtracting fractions requires a systematic approach, especially when dealing with mixed numbers or different denominators. Follow these steps:
- Convert Mixed Numbers: If you have mixed numbers, convert them to improper fractions. Multiply the whole number by the denominator and add the numerator.
- Find a Common Denominator: If the denominators are different, find the Least Common Multiple (LCM) to make them the same.
- Adjust Numerators: Multiply the numerator of each fraction by the same factor used to adjust its denominator.
- Subtract: Subtract the second numerator from the first, keeping the common denominator.
- Simplify: Reduce the resulting fraction to its simplest form and convert it back to a mixed number if necessary.
Example Calculation
Let's subtract 2 1/4 from 5 1/2:
- Step 1: Convert to improper fractions: 5 1/2 = 11/2; 2 1/4 = 9/4.
- Step 2: Common denominator is 4.
- Step 3: Adjust: 11/2 becomes 22/4.
- Step 4: Subtract: 22/4 – 9/4 = 13/4.
- Step 5: Convert back: 13/4 = 3 1/4.
function subtractFractions() {
var w1 = parseInt(document.getElementById('whole1').value) || 0;
var n1 = parseInt(document.getElementById('num1').value) || 0;
var d1 = parseInt(document.getElementById('den1').value) || 1;
var w2 = parseInt(document.getElementById('whole2').value) || 0;
var n2 = parseInt(document.getElementById('num2').value) || 0;
var d2 = parseInt(document.getElementById('den2').value) || 1;
if (d1 === 0 || d2 === 0) {
alert("Denominator cannot be zero.");
return;
}
// Convert to improper fractions
// Handle negative whole numbers correctly
var improperNum1 = (Math.abs(w1) * d1 + n1) * (w1 < 0 ? -1 : 1);
var improperNum2 = (Math.abs(w2) * d2 + n2) * (w2 < 0 ? -1 : 1);
// If whole is 0 but user wants negative, they might put – on the numerator
if (w1 === 0 && n1 < 0) improperNum1 = n1;
if (w2 === 0 && n2 < 0) improperNum2 = n2;
// Find common denominator: d1 * d2
var commonD = d1 * d2;
var finalNum = (improperNum1 * d2) – (improperNum2 * d1);
if (finalNum === 0) {
displayResult("0", "");
return;
}
// Simplify fraction
var commonFactor = gcd(Math.abs(finalNum), Math.abs(commonD));
var simplifiedNum = finalNum / commonFactor;
var simplifiedDen = commonD / commonFactor;
// Prepare display
var resultString = "";
var isNegative = simplifiedNum = simplifiedDen) {
var wholePart = Math.floor(absNum / simplifiedDen);
var remNum = absNum % simplifiedDen;
resultString = (isNegative ? "-" : "") + wholePart;
if (remNum !== 0) {
resultString += " " + remNum + "/" + simplifiedDen;
}
} else {
resultString = (isNegative ? "-" : "") + absNum + "/" + simplifiedDen;
}
var decimalVal = (finalNum / commonD).toFixed(4);
displayResult(resultString, decimalVal);
}
function gcd(a, b) {
return b ? gcd(b, a % b) : a;
}
function displayResult(fraction, decimal) {
var container = document.getElementById('fractionResult');
var content = document.getElementById('resultContent');
container.style.display = "block";
var html = "
Final Result: " + fraction + "
";
if(decimal !== "") {
html += "
Decimal Equivalent: " + decimal + "
";
}
content.innerHTML = html;
}