This scientific calculator provides a range of functions for complex calculations. It's designed to assist students, engineers, and scientists with tasks ranging from basic arithmetic to advanced trigonometry, logarithms, and statistics.
Result:
function calculateScientific() {
var expression = document.getElementById("expression").value;
var resultDiv = document.getElementById("result");
// Basic validation to ensure input is not empty
if (expression.trim() === "") {
resultDiv.innerHTML = "Please enter a calculation.";
return;
}
try {
// Use eval() for simplicity in this example, but be aware of its security risks in production environments
// For a robust calculator, a proper parsing and evaluation library would be recommended.
// For this specific purpose, we need to handle common math functions and constants.
// Replace common constants and functions with their JavaScript equivalents
var safeExpression = expression.replace(/pi/gi, Math.PI.toString());
safeExpression = safeExpression.replace(/e/gi, Math.E.toString());
// Handle trigonometric functions (ensure they use radians)
safeExpression = safeExpression.replace(/sin\(/gi, 'Math.sin(');
safeExpression = safeExpression.replace(/cos\(/gi, 'Math.cos(');
safeExpression = safeExpression.replace(/tan\(/gi, 'Math.tan(');
safeExpression = safeExpression.replace(/asin\(/gi, 'Math.asin(');
safeExpression = safeExpression.replace(/acos\(/gi, 'Math.acos(');
safeExpression = safeExpression.replace(/atan\(/gi, 'Math.atan(');
safeExpression = safeExpression.replace(/log\(/gi, 'Math.log('); // Natural logarithm
safeExpression = safeExpression.replace(/log10\(/gi, 'Math.log10('); // Base-10 logarithm
safeExpression = safeExpression.replace(/sqrt\(/gi, 'Math.sqrt(');
safeExpression = safeExpression.replace(/abs\(/gi, 'Math.abs(');
safeExpression = safeExpression.replace(/pow\(/gi, 'Math.pow(');
safeExpression = safeExpression.replace(/exp\(/gi, 'Math.exp(');
safeExpression = safeExpression.replace(/round\(/gi, 'Math.round(');
var result = eval(safeExpression);
if (typeof result === 'number' && !isNaN(result)) {
resultDiv.innerHTML = result;
} else {
resultDiv.innerHTML = "Invalid calculation or result.";
}
} catch (error) {
resultDiv.innerHTML = "Error: " + error.message;
}
}
.calculator-container {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
margin-bottom: 15px;
color: #333;
}
.calculator-container p {
text-align: center;
margin-bottom: 20px;
color: #555;
font-size: 0.9em;
}
.inputs {
margin-bottom: 15px;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input[type="text"] {
width: calc(100% – 20px);
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
}
.calculator-container button {
display: block;
width: 100%;
padding: 12px 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.result-container {
margin-top: 20px;
padding: 15px;
background-color: #e9ecef;
border-radius: 4px;
text-align: center;
}
.result-container h3 {
margin-top: 0;
margin-bottom: 10px;
color: #333;
}
#result {
font-size: 1.2em;
font-weight: bold;
color: #0056b3;
word-wrap: break-word;
}