var expression = ";
var display;
function init() {
display = document.getElementById('calcDisplay');
display.value = '0';
}
function appendChar(char) {
if (display.value === 'Error') {
clearAll();
}
if (expression === " && char === '.') {
expression = '0.';
} else if (expression === '0' && char !== '.') {
expression = char;
} else {
expression += char;
}
display.value = expression;
}
function appendOperator(op) {
if (display.value === 'Error') {
clearAll();
}
var lastChar = expression.slice(-1);
if (['+', '-', '*', '/', '^', '.'].includes(lastChar)) {
expression = expression.slice(0, -1) + op;
} else {
expression += op;
}
display.value = expression;
}
function appendFunction(func) {
if (display.value === 'Error') {
clearAll();
}
if (expression === '0' || expression === ") {
expression = func;
} else {
var lastChar = expression.slice(-1);
if (/\d/.test(lastChar) || lastChar === ')') {
expression += '*' + func; // Implicit multiplication
} else {
expression += func;
}
}
display.value = expression;
}
function appendConstant(constName) {
if (display.value === 'Error') {
clearAll();
}
if (expression === '0' || expression === ") {
expression = constName;
} else {
var lastChar = expression.slice(-1);
if (/\d/.test(lastChar) || lastChar === ')') {
expression += '*' + constName; // Implicit multiplication
} else {
expression += constName;
}
}
display.value = expression;
}
function appendParenthesis(paren) {
if (display.value === 'Error') {
clearAll();
}
if (paren === '(' && expression !== '0' && (/\d/.test(expression.slice(-1)) || expression.slice(-1) === ')')) {
expression += '*('; // Implicit multiplication
} else if (expression === '0' && paren === '(') {
expression = '(';
} else {
expression += paren;
}
display.value = expression;
}
function clearAll() {
expression = ";
display.value = '0';
}
function backspace() {
if (display.value === 'Error') {
clearAll();
return;
}
expression = expression.slice(0, -1);
if (expression === ") {
display.value = '0';
} else {
display.value = expression;
}
}
function calculateFactorial(n) {
if (n 1; i–) {
res *= i;
}
return res;
}
function factorial() {
if (display.value === 'Error') {
clearAll();
return;
}
try {
var num = parseFloat(eval(expression));
if (isNaN(num) || !Number.isInteger(num) || num < 0) {
display.value = 'Error';
expression = '';
return;
}
var result = calculateFactorial(num);
display.value = result;
expression = result.toString();
} catch (e) {
display.value = 'Error';
expression = '';
}
}
function changeSign() {
if (display.value === 'Error') {
clearAll();
return;
}
try {
var num = parseFloat(eval(expression));
if (isNaN(num)) {
display.value = 'Error';
expression = '';
return;
}
var result = -num;
display.value = result;
expression = result.toString();
} catch (e) {
display.value = 'Error';
expression = '';
}
}
function calculateResult() {
if (display.value === 'Error') {
clearAll();
return;
}
var currentExpression = expression;
// Replace scientific functions and constants with Math equivalents
currentExpression = currentExpression.replace(/sin\(/g, 'Math.sin(');
currentExpression = currentExpression.replace(/cos\(/g, 'Math.cos(');
currentExpression = currentExpression.replace(/tan\(/g, 'Math.tan(');
currentExpression = currentExpression.replace(/log\(/g, 'Math.log10('); // Base 10 log
currentExpression = currentExpression.replace(/ln\(/g, 'Math.log('); // Natural log
currentExpression = currentExpression.replace(/sqrt\(/g, 'Math.sqrt(');
currentExpression = currentExpression.replace(/pi/g, 'Math.PI');
currentExpression = currentExpression.replace(/e/g, 'Math.E');
// Handle power operator '^' with '**' (JavaScript exponentiation operator)
currentExpression = currentExpression.replace(/\^/g, '**');
try {
var result = eval(currentExpression);
if (isNaN(result) || !isFinite(result)) {
display.value = 'Error';
} else {
display.value = result;
expression = result.toString(); // Set expression to result for further calculations
}
} catch (e) {
display.value = 'Error';
}
}
// Initialize the display when the script loads
window.onload = init;
Scientific Calculator Online Free
A scientific calculator is an essential tool for students, engineers, scientists, and anyone who needs to perform complex mathematical calculations beyond basic arithmetic. Unlike a standard calculator, a scientific calculator includes functions for trigonometry, logarithms, exponents, roots, and more, making it indispensable for various academic and professional fields.
Key Features of This Scientific Calculator:
Basic Arithmetic: Perform addition, subtraction, multiplication, and division.
Trigonometric Functions: Calculate sine (sin), cosine (cos), and tangent (tan) for angles.
Logarithmic Functions: Compute natural logarithm (ln) and base-10 logarithm (log).
Exponents and Roots: Easily calculate powers (xy) and square roots (√).
Constants: Access mathematical constants like Pi (π) and Euler's number (e).
Factorial: Compute the factorial of a non-negative integer (x!).
Parentheses: Group operations to control the order of calculation.
Clear Functions: 'AC' (All Clear) to reset the calculator and 'DEL' (Delete) to remove the last character.
How to Use This Calculator:
Input Numbers: Click the number buttons (0-9) to enter digits. Use the '.' button for decimals.
Basic Operations: Click '+', '-', '*', '/' for standard arithmetic.
Scientific Functions:
For functions like sin, cos, tan, log, ln, sqrt: Click the function button, then enter the number or expression in parentheses. E.g., sin(30).
For powers (xy): Enter the base number, click 'xy', then enter the exponent. E.g., 2^3.
For factorial (x!): Enter the number, then click 'x!'. E.g., 5!.
For constants (π, e): Click the respective button to insert the constant into your expression.
Parentheses: Use '(' and ')' to define the order of operations. For example, (2 + 3) * 4.
Equals: Click '=' to calculate the result of the current expression.
Clear: Use 'AC' to clear the entire display and reset the calculator. Use 'DEL' to remove the last character entered.
Change Sign: Use '+/-' to toggle the sign of the current number or result.
Examples of Calculations:
Basic:15 * 3 + 7 = 52
Trigonometry:sin(90) = 1 (assuming degrees, though JavaScript's Math.sin expects radians, so sin(Math.PI / 2) would be 1)
This free online scientific calculator is designed to be user-friendly and efficient for all your advanced calculation needs. Enjoy using it for your studies, work, or everyday problem-solving!