Perform arithmetic operations (addition, subtraction, multiplication, division) on whole numbers and fractions.
Inputs
/
/
+
–
*
/
Result:
Understanding Whole Numbers and Fractions
This calculator is designed to handle arithmetic operations involving whole numbers and fractions. Both whole numbers and fractions are fundamental components of the number system, allowing us to represent quantities precisely.
What are Whole Numbers?
Whole numbers are the set of non-negative integers: 0, 1, 2, 3, … They represent complete units without any fractional parts.
What are Fractions?
A fraction represents a part of a whole. It is typically written as two integers, a numerator and a denominator, separated by a line.
The numerator (top number) indicates how many parts of the whole are being considered.
The denominator (bottom number) indicates the total number of equal parts the whole is divided into.
For example, 1/2 means one part out of two equal parts, and 3/4 means three parts out of four equal parts.
How to Represent Numbers
This calculator allows you to enter numbers in two ways:
Whole Numbers: Enter the number in the "Whole" field and leave the "Numerator" and "Denominator" fields blank or as 0. For instance, the whole number 5 can be represented as 5 (whole), 0 (numerator), 1 (denominator).
Proper/Improper Fractions: Enter the numerator and denominator, and leave the "Whole" field blank or as 0. For instance, 1/3 is 0 (whole), 1 (numerator), 3 (denominator).
Mixed Numbers: Enter the whole number part, the numerator, and the denominator. For instance, 2 1/4 is 2 (whole), 1 (numerator), 4 (denominator).
Arithmetic Operations with Fractions
The calculator performs the following operations:
Addition (a/b + c/d): Find a common denominator (usually bd), then add the numerators: (ad + cb) / bd. If whole numbers are involved, convert them to fractions first.
Subtraction (a/b - c/d): Similar to addition, find a common denominator and subtract the numerators: (ad - cb) / bd.
Multiplication (a/b * c/d): Multiply the numerators and the denominators: ac / bd.
Division (a/b รท c/d): Invert the second fraction and multiply: a/b * d/c = ad / bc.
Simplification
After performing an operation, the result is simplified to its lowest terms by dividing both the numerator and the denominator by their greatest common divisor (GCD).
Use Cases
This calculator is useful for:
Students learning arithmetic with fractions.
Anyone needing to perform quick calculations involving mixed numbers or fractions.
Comparing fractional quantities.
Tasks in cooking, crafting, or DIY projects that require precise measurements.
// Helper function to find the Greatest Common Divisor (GCD)
var gcd = function(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b) {
var t = b;
b = a % b;
a = t;
}
return a;
};
// Function to convert mixed number to improper fraction
var mixedToImproper = function(whole, num, den) {
if (isNaN(den) || den === 0) return { num: 0, den: 1 }; // Handle invalid denominator
if (isNaN(whole) || whole === 0) whole = 0;
if (isNaN(num) || num === 0) num = 0;
var improperNum = whole * den + num;
return { num: improperNum, den: den };
};
// Function to convert improper fraction to mixed number and simplify
var simplifyFraction = function(num, den) {
if (den === 0) {
return { whole: NaN, num: NaN, den: NaN, display: "Error: Division by zero" };
}
if (num === 0) {
return { whole: 0, num: 0, den: 1, display: "0" };
}
var commonDivisor = gcd(num, den);
num /= commonDivisor;
den /= commonDivisor;
// Ensure denominator is positive
if (den < 0) {
num = -num;
den = -den;
}
var whole = Math.floor(num / den);
var remainder = num % den;
if (remainder === 0) {
return { whole: whole, num: 0, den: 1, display: whole.toString() };
} else {
return { whole: whole, num: remainder, den: den, display: whole === 0 ? `${remainder}/${den}` : `${whole} ${remainder}/${den}` };
}
};
var calculateFraction = function() {
var num1_whole = parseFloat(document.getElementById("num1_whole").value);
var num1_num = parseFloat(document.getElementById("num1_num").value);
var num1_den = parseFloat(document.getElementById("num1_den").value);
var num2_whole = parseFloat(document.getElementById("num2_whole").value);
var num2_num = parseFloat(document.getElementById("num2_num").value);
var num2_den = parseFloat(document.getElementById("num2_den").value);
var operation = document.getElementById("operation").value;
var resultDisplay = document.getElementById("result");
// Default to 0 if fields are empty or not numbers
if (isNaN(num1_whole)) num1_whole = 0;
if (isNaN(num1_num)) num1_num = 0;
if (isNaN(num1_den) || num1_den === 0) num1_den = 1; // Default to 1 to avoid division by zero
if (isNaN(num2_whole)) num2_whole = 0;
if (isNaN(num2_num)) num2_num = 0;
if (isNaN(num2_den) || num2_den === 0) num2_den = 1; // Default to 1 to avoid division by zero
// Convert mixed numbers to improper fractions
var frac1 = mixedToImproper(num1_whole, num1_num, num1_den);
var frac2 = mixedToImproper(num2_whole, num2_num, num2_den);
var result_num, result_den;
if (operation === "add") {
result_num = frac1.num * frac2.den + frac2.num * frac1.den;
result_den = frac1.den * frac2.den;
} else if (operation === "subtract") {
result_num = frac1.num * frac2.den – frac2.num * frac1.den;
result_den = frac1.den * frac2.den;
} else if (operation === "multiply") {
result_num = frac1.num * frac2.num;
result_den = frac1.den * frac2.den;
} else if (operation === "divide") {
if (frac2.num === 0) {
resultDisplay.textContent = "Error: Division by zero";
return;
}
result_num = frac1.num * frac2.den;
result_den = frac1.den * frac2.num;
} else {
resultDisplay.textContent = "Invalid operation";
return;
}
var simplifiedResult = simplifyFraction(result_num, result_den);
if (isNaN(simplifiedResult.whole) && simplifiedResult.display.includes("Error")) {
resultDisplay.textContent = simplifiedResult.display;
} else {
resultDisplay.textContent = simplifiedResult.display;
}
};
var resetFields = function() {
document.getElementById("num1_whole").value = "";
document.getElementById("num1_num").value = "";
document.getElementById("num1_den").value = "";
document.getElementById("num2_whole").value = "";
document.getElementById("num2_num").value = "";
document.getElementById("num2_den").value = "";
document.getElementById("operation").value = "add";
document.getElementById("result").textContent = "";
};