📖 Overview
The Scientific Calculator API provides a comprehensive set of mathematical operations accessible via HTTP requests. All operations return JSON responses and support both GET and POST methods.
Features
✨ Basic Arithmetic
Addition, subtraction, multiplication, division, modulo, powers, and roots
📐 Trigonometry
Sin, cos, tan and their inverses. Support for degrees and radians
📊 Statistics
Mean, median, mode, variance, standard deviation, and more
🔢 Number Theory
GCD, LCM, prime checking, factorials, permutations, combinations
📈 Logarithmic
Natural log, log base 10, log base 2, custom bases, and exponentials
💯 Percentage
Calculate percentages, percentage of values, and percentage changes
🚀 Getting Started
Base URL
Request Format
All requests require an op parameter specifying the operation to perform.
HTTP Methods
- GET - Pass parameters in URL query string
- POST - Pass parameters in request body (form-data or JSON)
Response Format
All responses are returned in JSON format with the following structure:
➕ Basic Operations
Operation: op=add
Parameters:
- a (required) - First number
- b (required) - Second number
Example:
Operation: op=subtract
Parameters:
- a (required) - First number
- b (required) - Second number
Example:
Operation: op=multiply
Parameters:
- a (required) - First number
- b (required) - Second number
Example:
Operation: op=divide
Parameters:
- a (required) - Dividend
- b (required) - Divisor (cannot be zero)
Example:
Operation: op=modulo
Parameters:
- a (required) - Number
- b (required) - Divisor (cannot be zero)
Example:
Operation: op=power
Parameters:
- base (required) - Base number
- exponent (required) - Exponent
Example:
Operation: op=sqrt
Parameters:
- n (required) - Number (must be non-negative)
Example:
Operation: op=nthRoot
Parameters:
- n (required) - Number
- root (required) - Root degree
Example:
Operation: op=abs
Parameters:
- n (required) - Number
Example:
Operation: op=factorial
Parameters:
- n (required) - Non-negative integer (max 170)
Example:
📐 Trigonometric Functions
Operation: op=sin
Parameters:
- angle (required) - Angle value
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Operation: op=cos
Parameters:
- angle (required) - Angle value
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Operation: op=tan
Parameters:
- angle (required) - Angle value
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Operation: op=asin
Parameters:
- value (required) - Value between -1 and 1
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Operation: op=acos
Parameters:
- value (required) - Value between -1 and 1
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Operation: op=atan
Parameters:
- value (required) - Any number
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Operation: op=atan2
Parameters:
- y (required) - Y coordinate
- x (required) - X coordinate
- angleMode (optional) - 'deg' or 'rad' (default: 'rad')
Example:
Hyperbolic Functions
| Operation | Parameters | Example |
|---|---|---|
| op=sinh | x | ?op=sinh&x=1 |
| op=cosh | x | ?op=cosh&x=1 |
| op=tanh | x | ?op=tanh&x=1 |
| op=asinh | x | ?op=asinh&x=1 |
| op=acosh | x (≥1) | ?op=acosh&x=2 |
| op=atanh | x (-1 < x < 1) | ?op=atanh&x=0.5 |
📈 Logarithmic & Exponential Functions
Operation: op=ln
Parameters:
- n (required) - Positive number
Example:
Operation: op=log10
Parameters:
- n (required) - Positive number
Example:
Operation: op=log2
Parameters:
- n (required) - Positive number
Example:
Operation: op=log
Parameters:
- n (required) - Positive number
- base (optional) - Base (default: e)
Example:
Operation: op=exp
Parameters:
- n (required) - Exponent
Example:
🔢 Rounding Functions
| Operation | Parameters | Description | Example |
|---|---|---|---|
| op=round | n, precision (optional) | Round to nearest value | ?op=round&n=3.14159&precision=2 → 3.14 |
| op=floor | n | Round down to integer | ?op=floor&n=4.7 → 4 |
| op=ceil | n | Round up to integer | ?op=ceil&n=4.3 → 5 |
📊 Statistics Functions
Operation: op=mean
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=median
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=mode
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=variance
Parameters:
- values (required) - Array of numbers
- sample (optional) - true for sample variance, false for population (default: true)
Example:
Operation: op=stddev
Parameters:
- values (required) - Array of numbers
- sample (optional) - true for sample, false for population (default: true)
Example:
Operation: op=sum
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=product
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=min
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=max
Parameters:
- values (required) - Array of numbers
Example:
Operation: op=range
Parameters:
- values (required) - Array of numbers
Example:
🔬 Advanced Operations
Number Theory
Operation: op=gcd
Parameters:
- a (required) - First integer
- b (required) - Second integer
Example:
Operation: op=lcm
Parameters:
- a (required) - First integer
- b (required) - Second integer
Example:
Operation: op=isPrime
Parameters:
- n (required) - Integer to check
Example:
Combinatorics
Operation: op=permutation
Parameters:
- n (required) - Total items
- r (required) - Items to arrange
Example:
Formula: P(n,r) = n! / (n-r)!
Operation: op=combination
Parameters:
- n (required) - Total items
- r (required) - Items to choose
Example:
Formula: C(n,r) = n! / (r! × (n-r)!)
Percentage Operations
Operation: op=percentage
Parameters:
- value (required) - Part value
- total (required) - Total value
Example:
Operation: op=percentageOf
Parameters:
- percentage (required) - Percentage value
- total (required) - Total value
Example:
Operation: op=percentageIncrease
Parameters:
- original (required) - Original value
- new (required) - New value
Example:
Quadratic Equation Solver
Operation: op=quadratic
Parameters:
- a (required) - Coefficient of x²
- b (required) - Coefficient of x
- c (required) - Constant term
Example:
Formula: ax² + bx + c = 0
Conversion Functions
| Operation | Parameters | Description | Example |
|---|---|---|---|
| op=degToRad | degrees | Convert degrees to radians | ?op=degToRad°rees=180 → 3.14159 |
| op=radToDeg | radians | Convert radians to degrees | ?op=radToDeg&radians=3.14159 → 180 |
Random Number Generation
Operation: op=random
Parameters:
- min (optional) - Minimum value (default: 0)
- max (optional) - Maximum value (default: 1)
Example:
Operation: op=randomInt
Parameters:
- min (required) - Minimum value
- max (required) - Maximum value
Example:
Constants
Operation: op=constants
Parameters: None
Example:
⚠️ Error Handling
The API returns appropriate error messages for invalid inputs or operations. All errors follow this format:
Common Error Messages
| Error | Cause | Solution |
|---|---|---|
| No operation specified | Missing 'op' parameter | Add ?op=operation_name to request |
| Unknown operation: [name] | Invalid operation name | Check operation name spelling |
| Missing required parameter: [name] | Required parameter not provided | Add the missing parameter |
| Division by zero | Attempted to divide by 0 | Use non-zero divisor |
| Cannot calculate square root of negative number | Negative input for sqrt | Use non-negative number |
| Factorial not defined for negative numbers | Negative input for factorial | Use non-negative integer |
| Factorial too large to calculate | Input > 170 | Use smaller number (≤170) |
| Logarithm undefined for non-positive numbers | Zero or negative input for log | Use positive number |
| Arcsine domain error | Value outside [-1, 1] | Use value between -1 and 1 |
| Array cannot be empty | Empty array for statistics | Provide at least one value |
HTTP Status Codes
| Status Code | Meaning | When It Occurs |
|---|---|---|
| 200 | OK | Successful operation |
| 400 | Bad Request | Invalid parameters or operation error |
💡 Usage Examples
JavaScript / Fetch API
Python / Requests
PHP / cURL
jQuery / AJAX
cURL Command Line
❓ Help & Support
List All Operations
To get a complete list of all available operations grouped by category:
This returns a JSON object with all operations organized by category:
Quick Reference
Basic Math
add, subtract, multiply, divide, modulo, power, sqrt, abs, factorial
Trigonometry
sin , cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh
Logarithmic
ln, log, log10, log2, exp
Statistics
mean, median, mode, variance, stddev, sum, product, min, max, range
Number Theory
gcd, lcm, isPrime, factorial
Combinatorics
permutation, combination
Percentage
percentage, percentageOf, percentageIncrease
Other
round, floor, ceil, quadratic, random, randomInt, constants
Tips & Best Practices
- URL Encoding: Always URL-encode parameters, especially when passing negative numbers or special characters.
- Precision: Floating-point calculations may have minor precision errors. Use rounding functions when necessary.
- Array Format: Arrays can be passed as comma-separated values or JSON arrays. Both formats are supported.
- Angle Mode: Remember to specify angleMode=deg for trigonometric functions if working in degrees.
- Error Handling: Always check the success field in responses before using results.
- Rate Limiting: Be respectful with API usage. Consider caching results for repeated calculations.
- CORS: The API supports cross-origin requests, so it can be called from browser JavaScript.
📚 Complete Operation Reference
All Available Operations
| Category | Operation | Required Parameters | Optional Parameters |
|---|---|---|---|
| Basic | add | a, b | - |
| subtract | a, b | - | |
| multiply | a, b | - | |
| divide | a, b | - | |
| modulo | a, b | - | |
| power | base, exponent | - | |
| sqrt | n | - | |
| nthRoot | n, root | - | |
| abs | n | - | |
| factorial | n | - | |
| Trigonometry | sin | angle | angleMode |
| cos | angle | angleMode | |
| tan | angle | angleMode | |
| asin | value | angleMode | |
| acos | value | angleMode | |
| atan | value | angleMode | |
| atan2 | y, x | angleMode | |
| Hyperbolic | sinh | x | - |
| cosh | x | - | |
| tanh | x | - | |
| asinh | x | - | |
| acosh | x | - | |
| atanh | x | - | |
| Logarithmic | ln | n | - |
| log | n | base | |
| log10 | n | - | |
| log2 | n | - | |
| exp | n | - | |
| Rounding | round | n | precision |
| floor | n | - | |
| ceil | n | - | |
| Number Theory | gcd | a, b | - |
| lcm | a, b | - | |
| isPrime | n | - | |
| Combinatorics | permutation | n, r | - |
| combination | n, r | - | |
| Statistics | mean | values | - |
| median | values | - | |
| mode | values | - | |
| variance | values | sample | |
| stddev | values | sample | |
| sum | values | - | |
| product | values | - | |
| min | values | - | |
| max | values | - | |
| range | values | - | |
| Conversion | degToRad | degrees | - |
| radToDeg | radians | - | |
| Percentage | percentage | value, total | - |
| percentageOf | percentage, total | - | |
| percentageIncrease | original, new | - | |
| Equation | quadratic | a, b, c | - |
| Random | random | - | min, max |
| randomInt | min, max | - | |
| Utility | constants | - | - |
| Help | help | - | - |
🤔 Frequently Asked Questions
Q: Is there a rate limit on API requests?
A: Currently, there are no strict rate limits, but please use the API responsibly. Excessive usage may result in temporary restrictions.
Q: Can I use this API in production applications?
A: Yes, the API is stable and can be used in production. However, consider implementing proper error handling and caching for better performance.
Q: What is the maximum precision for calculations?
A: The API uses PHP's floating-point arithmetic, which provides approximately 15 decimal digits of precision. For higher precision requirements, consider using specialized libraries.
Q: How do I pass negative numbers in the URL?
A: URL-encode negative numbers or use POST requests. Example: ?op=add&a=-5&b=3 or encode as ?op=add&a=%2D5&b=3
Q: Can I batch multiple operations in one request?
A: No, each request performs a single operation. Make multiple requests for multiple calculations.
Q: Is HTTPS supported?
A: Yes, the API is accessible via HTTPS at https://www.promptbox.cn/api/calculator.php
Q: What happens if I pass invalid parameters?
A: The API will return a JSON error response with "success": false and an error message describing the issue.
Q: Are there client libraries available?
A: Currently, no official client libraries exist, but the API is simple enough to use directly with HTTP clients in any programming language.
Q: Can I perform matrix operations?
A: No, the current version does not support matrix operations. It focuses on scalar calculations and basic statistics.
Q: How accurate are trigonometric functions?
A: Trigonometric functions use PHP's built-in math functions, which are highly accurate for most applications. Minor floating-point errors may occur in edge cases.
📝 Version Information
Current Version: 1.0
Features
- ✅ Complete basic arithmetic operations
- ✅ Comprehensive trigonometric functions (regular and hyperbolic)
- ✅ Logarithmic and exponential functions
- ✅ Statistical operations for data analysis
- ✅ Number theory functions (GCD, LCM, prime checking)
- ✅ Combinatorics (permutations and combinations)
- ✅ Percentage calculations
- ✅ Quadratic equation solver
- ✅ Random number generation
- ✅ Angle mode support (degrees/radians)
- ✅ Comprehensive error handling
- ✅ CORS support for browser requests
- ✅ JSON response format
- ✅ GET and POST method support
Planned Features
- 🔜 Matrix operations
- 🔜 Complex number arithmetic
- 🔜 Polynomial operations
- 🔜 Calculus functions (derivatives, integrals)
- 🔜 Unit conversions
- 🔜 Financial calculations
- 🔜 Date/time calculations