# Calculator
This is a advanced calculator program that can evaluate arithmetic expressions and perform trigonometric calculations. It provides the following functionality:
- Arithmetic operations: addition, subtraction, multiplication, and division
- Support for complex expressions with multiple operators
- Unary operators: positive and negative signs
- Evaluation of expressions inside brackets
- Trigonometric functions: sin, cos, tan, cosec, sec, and cot
- Inverse trigonometric functions: asin, acos, atan
## Getting Started
//
## Usage
To evaluate an arithmetic expression, create an instance of the `Calculator` class and call the `evaluate` method, passing the expression as a string parameter. The method will return the result of the evaluation.
Here's an example:
```python
from calculator import Calculator
# Create an instance of the Calculator class
calculator = Calculator()
# Evaluate an arithmetic expression
result = calculator.evaluate('2 + 3 * 4')
print(result) # Output: 14
```
You can use the `evaluate` method to perform various calculations, including complex expressions and trigonometric functions.
## Supported Operations
### Arithmetic Operations
The calculator supports the following arithmetic operations:
- Addition: `+`
- Subtraction: `-`
- Multiplication: `*`
- Division: `/`
- Power: `^`
- Module: `%`
### Unary Operators
The calculator supports unary operators to represent positive and negative numbers:
- Positive: `+`
- Negative: `-`
### Evaluation of Expressions
The calculator can evaluate complex expressions with multiple operators and parentheses. It follows the standard order of operations (PEMDAS/BODMAS).
Here's an example:
```python
result = calculator.evaluate('(2 + 3) * 4')
print(result) # Output: 20
```
### Trigonometric Functions
The calculator provides trigonometric functions to calculate sine, cosine, tangent, cosecant, secant, and cotangent. The functions are case-insensitive.
- Sine: `sin`
- Cosine: `cos`
- Tangent: `tan`
- Cosecant: `cosec`
- Secant: `sec`
- Cotangent: `cot`
Here's an example:
```python
result = calculator.evaluate('sin(45) + cos(30)')
print(result) # Output: 1.573132185
```
### Inverse Trigonometric Functions
The calculator also supports inverse trigonometric functions to calculate arcsine, arccosine, and arctangent. The functions are case-insensitive.
- Arcsine: `asin`
- Arccosine: `acos`
- Arctangent: `atan`
- ArcCosecant: `acosec`
- ArcSecant: `asec`
- ArcCotangent: `acot`
Here's an example:
```python
result = calculator.evaluate('atan(1)')
print(result) # Output: 45.0
```
### Additional Functions
The calculator also supports the following additional functions:
- Square Root: `sqrt(x)`, where `x` is the number for which you want to calculate the square root. Example: `sqrt(16)` returns `4`.
- Absolute Value: `abs(x)`, where `x` is the number for which you want to calculate the absolute value. Example: `abs(-5)` returns `5`.
- Exponential: `exp(x)`, where `x` is the number for which you want to calculate the exponential value. Example: `exp(2)` returns `7.3890560989306495`.
- Logarithm: `logx(y)`, where `x` is the base and `y` is the number for which you want to calculate the logarithm. Example: `log10(100)` returns `2`.
Here's an example that includes the usage of these additional functions:
```python
result = calculator.evaluate('sqrt(16) + abs(-5) * exp(2)')
print(result) # Output: 40.945280495
```
## Validations
The supported validations in your `calculator.py` program are designed to check for various types of errors and ensure that the input expression is valid. Here is a description of each supported validation:
1. **Complex Expression**: This validation tests the evaluation of a complex expression involving multiple arithmetic operations. It verifies that the evaluated result matches the expected expression.
2. **Type Error**: This validation checks for an empty expression. If the expression is empty, it returns an error message indicating that the expression is empty.
3. **Parentheses Error**: This validation identifies errors related to parentheses in the expression. It checks for missing or mismatched opening and closing parentheses and returns appropriate error messages.
4. **Function Error**: This validation detects errors related to function calls. It checks for empty function calls and returns an error message indicating that the function call is empty.
5. **Keyword**: This validation verifies the correctness of function names and keywords used in the expression. It checks for inappropriate function calls and invalid keywords, returning corresponding error messages.
6. **Invalid Operators Error**: This validation identifies errors related to invalid operators in the expression. It checks for unsupported or invalid operator combinations and returns error messages indicating the presence of invalid operators.
7. **Incomplete Expression Error**: This validation checks for incomplete expressions, where an operation is missing an operand. It returns error messages indicating the specific location of the incomplete expression.
8. **Division by Zero**: This validation detects division by zero errors in the expression. It checks for divisions where the divisor is zero and returns error messages indicating the division by zero.
9. **Invalid Function**: This validation identifies errors related to invalid function calls. It checks for unsupported or invalid function names and returns error messages indicating the presence of an invalid function call.
10. **Missing Operand**: This validation checks for missing operands in the expression. It detects expressions where an operation is missing one of its operands and returns error messages indicating the location of the missing operand.
11. **Invalid Number Format**: This validation identifies errors related to invalid number formats in the expression. It checks for numbers with incorrect decimal formats and returns error messages indicating the presence of invalid number formats.
12. **Invalid Variable**: This validation detects errors related to unsupported or invalid variable names in the expression. It checks for variables with unsupported characters and returns error messages indicating the presence of an unsupported variable.
These supported validations help ensure that the expression provided to the calculator is well-formed and prevent errors during the evaluation process.
## Unit Tests
The `test_calculator.py` file contains unit tests for the `Calculator` class. You can run these tests to ensure that the calculator functions correctly.
To run the unit tests, execute the following command in the terminal:
```
python -m unittest test_calculator.py
```
The test cases cover various scenarios, including arithmetic operations, evaluation of expressions, unary operators, and trigonometric functions.
Raw data
{
"_id": null,
"home_page": "https://github.com/gkibria121/calculator",
"name": "gk-calculator",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "python,calculator,python calculator,gk calculator,advanced calculator",
"author": "Ahmed Kibria",
"author_email": "gkibria121@email.com",
"download_url": "https://files.pythonhosted.org/packages/6d/bb/71350b1d80d5afdd4ccfe2a1a4d90bbb87b81d983aa55baef5e950257439/gk_calculator-1.0.0.tar.gz",
"platform": null,
"description": "# Calculator\r\n\r\nThis is a advanced calculator program that can evaluate arithmetic expressions and perform trigonometric calculations. It provides the following functionality:\r\n\r\n- Arithmetic operations: addition, subtraction, multiplication, and division\r\n- Support for complex expressions with multiple operators\r\n- Unary operators: positive and negative signs\r\n- Evaluation of expressions inside brackets\r\n- Trigonometric functions: sin, cos, tan, cosec, sec, and cot\r\n- Inverse trigonometric functions: asin, acos, atan\r\n\r\n## Getting Started\r\n\r\n//\r\n\r\n## Usage\r\n\r\nTo evaluate an arithmetic expression, create an instance of the `Calculator` class and call the `evaluate` method, passing the expression as a string parameter. The method will return the result of the evaluation.\r\n\r\nHere's an example:\r\n\r\n```python\r\nfrom calculator import Calculator\r\n\r\n# Create an instance of the Calculator class\r\ncalculator = Calculator()\r\n\r\n# Evaluate an arithmetic expression\r\nresult = calculator.evaluate('2 + 3 * 4')\r\nprint(result) # Output: 14\r\n```\r\n\r\nYou can use the `evaluate` method to perform various calculations, including complex expressions and trigonometric functions.\r\n\r\n## Supported Operations\r\n\r\n### Arithmetic Operations\r\n\r\nThe calculator supports the following arithmetic operations:\r\n\r\n- Addition: `+`\r\n- Subtraction: `-`\r\n- Multiplication: `*`\r\n- Division: `/`\r\n- Power: `^`\r\n- Module: `%`\r\n\r\n### Unary Operators\r\n\r\nThe calculator supports unary operators to represent positive and negative numbers:\r\n\r\n- Positive: `+`\r\n- Negative: `-`\r\n\r\n### Evaluation of Expressions\r\n\r\nThe calculator can evaluate complex expressions with multiple operators and parentheses. It follows the standard order of operations (PEMDAS/BODMAS).\r\n\r\nHere's an example:\r\n\r\n```python\r\nresult = calculator.evaluate('(2 + 3) * 4')\r\nprint(result) # Output: 20\r\n```\r\n\r\n### Trigonometric Functions\r\n\r\nThe calculator provides trigonometric functions to calculate sine, cosine, tangent, cosecant, secant, and cotangent. The functions are case-insensitive.\r\n\r\n- Sine: `sin`\r\n- Cosine: `cos`\r\n- Tangent: `tan`\r\n- Cosecant: `cosec`\r\n- Secant: `sec`\r\n- Cotangent: `cot`\r\n\r\nHere's an example:\r\n\r\n```python\r\nresult = calculator.evaluate('sin(45) + cos(30)')\r\nprint(result) # Output: 1.573132185\r\n```\r\n\r\n### Inverse Trigonometric Functions\r\n\r\nThe calculator also supports inverse trigonometric functions to calculate arcsine, arccosine, and arctangent. The functions are case-insensitive.\r\n\r\n- Arcsine: `asin`\r\n- Arccosine: `acos`\r\n- Arctangent: `atan`\r\n- ArcCosecant: `acosec`\r\n- ArcSecant: `asec`\r\n- ArcCotangent: `acot`\r\n\r\nHere's an example:\r\n\r\n```python\r\nresult = calculator.evaluate('atan(1)')\r\nprint(result) # Output: 45.0\r\n```\r\n\r\n### Additional Functions\r\n\r\nThe calculator also supports the following additional functions:\r\n\r\n- Square Root: `sqrt(x)`, where `x` is the number for which you want to calculate the square root. Example: `sqrt(16)` returns `4`.\r\n\r\n- Absolute Value: `abs(x)`, where `x` is the number for which you want to calculate the absolute value. Example: `abs(-5)` returns `5`.\r\n\r\n- Exponential: `exp(x)`, where `x` is the number for which you want to calculate the exponential value. Example: `exp(2)` returns `7.3890560989306495`.\r\n\r\n- Logarithm: `logx(y)`, where `x` is the base and `y` is the number for which you want to calculate the logarithm. Example: `log10(100)` returns `2`.\r\nHere's an example that includes the usage of these additional functions:\r\n\r\n```python\r\nresult = calculator.evaluate('sqrt(16) + abs(-5) * exp(2)')\r\nprint(result) # Output: 40.945280495\r\n```\r\n\r\n## Validations\r\n\r\nThe supported validations in your `calculator.py` program are designed to check for various types of errors and ensure that the input expression is valid. Here is a description of each supported validation:\r\n\r\n1. **Complex Expression**: This validation tests the evaluation of a complex expression involving multiple arithmetic operations. It verifies that the evaluated result matches the expected expression.\r\n\r\n2. **Type Error**: This validation checks for an empty expression. If the expression is empty, it returns an error message indicating that the expression is empty.\r\n\r\n3. **Parentheses Error**: This validation identifies errors related to parentheses in the expression. It checks for missing or mismatched opening and closing parentheses and returns appropriate error messages.\r\n\r\n4. **Function Error**: This validation detects errors related to function calls. It checks for empty function calls and returns an error message indicating that the function call is empty.\r\n\r\n5. **Keyword**: This validation verifies the correctness of function names and keywords used in the expression. It checks for inappropriate function calls and invalid keywords, returning corresponding error messages.\r\n\r\n6. **Invalid Operators Error**: This validation identifies errors related to invalid operators in the expression. It checks for unsupported or invalid operator combinations and returns error messages indicating the presence of invalid operators.\r\n\r\n7. **Incomplete Expression Error**: This validation checks for incomplete expressions, where an operation is missing an operand. It returns error messages indicating the specific location of the incomplete expression.\r\n\r\n8. **Division by Zero**: This validation detects division by zero errors in the expression. It checks for divisions where the divisor is zero and returns error messages indicating the division by zero.\r\n\r\n9. **Invalid Function**: This validation identifies errors related to invalid function calls. It checks for unsupported or invalid function names and returns error messages indicating the presence of an invalid function call.\r\n\r\n10. **Missing Operand**: This validation checks for missing operands in the expression. It detects expressions where an operation is missing one of its operands and returns error messages indicating the location of the missing operand.\r\n\r\n11. **Invalid Number Format**: This validation identifies errors related to invalid number formats in the expression. It checks for numbers with incorrect decimal formats and returns error messages indicating the presence of invalid number formats.\r\n\r\n12. **Invalid Variable**: This validation detects errors related to unsupported or invalid variable names in the expression. It checks for variables with unsupported characters and returns error messages indicating the presence of an unsupported variable.\r\n\r\nThese supported validations help ensure that the expression provided to the calculator is well-formed and prevent errors during the evaluation process.\r\n\r\n\r\n\r\n## Unit Tests\r\n\r\nThe `test_calculator.py` file contains unit tests for the `Calculator` class. You can run these tests to ensure that the calculator functions correctly.\r\n\r\nTo run the unit tests, execute the following command in the terminal:\r\n\r\n```\r\npython -m unittest test_calculator.py\r\n```\r\n\r\nThe test cases cover various scenarios, including arithmetic operations, evaluation of expressions, unary operators, and trigonometric functions.\r\n",
"bugtrack_url": null,
"license": "",
"summary": "A advanced calculator program",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/gkibria121/calculator"
},
"split_keywords": [
"python",
"calculator",
"python calculator",
"gk calculator",
"advanced calculator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cf25a446a4bc32857fef1237213281298a7ca783afc364daf43243d6cfd76602",
"md5": "53cc478b58b407bccb8dd94fad29aa3b",
"sha256": "2d9aa1a3709a5db65b4b25d7d793d9b6cfc3743eb4bd70756bd6d446c45ab823"
},
"downloads": -1,
"filename": "gk_calculator-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "53cc478b58b407bccb8dd94fad29aa3b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 5138,
"upload_time": "2023-06-07T14:23:00",
"upload_time_iso_8601": "2023-06-07T14:23:00.794442Z",
"url": "https://files.pythonhosted.org/packages/cf/25/a446a4bc32857fef1237213281298a7ca783afc364daf43243d6cfd76602/gk_calculator-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6dbb71350b1d80d5afdd4ccfe2a1a4d90bbb87b81d983aa55baef5e950257439",
"md5": "ae38423fa628d0a1b1f5262a680f68eb",
"sha256": "f8e6d53bf4c38d6220de2aa10903287dac9f3ca2ce49648d2e5be34a19656979"
},
"downloads": -1,
"filename": "gk_calculator-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "ae38423fa628d0a1b1f5262a680f68eb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 4917,
"upload_time": "2023-06-07T14:23:03",
"upload_time_iso_8601": "2023-06-07T14:23:03.223825Z",
"url": "https://files.pythonhosted.org/packages/6d/bb/71350b1d80d5afdd4ccfe2a1a4d90bbb87b81d983aa55baef5e950257439/gk_calculator-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-07 14:23:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gkibria121",
"github_project": "calculator",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "gk-calculator"
}