Skip to main content

a simple calculator

Project description

Calculator

The Calculator class provides a basic calculator with memory storage, supporting various mathematical operations. It allows users to perform addition, subtraction, multiplication, division, and Nth root calculations. The calculator also provides functionality to reset its memory to its initial state.

Installation

You can install the Calculator package using pip:

    # Install package
    pip install calculator_sf

Usage

Creating a Calculator

from calculator_sf.calculator import Calculator

# Create a calculator with default memory (0.0)
calculator = Calculator()

# Create a calculator with an initial memory of 5.0
calculator_with_memory = Calculator(memory=5.0)

Class Methods

init(self, memory: float=0.0, decimal_places: int=3)

Initialize a new Calculator object with optional initial memory and decimal places.

Parameters:
memory (float, optional): The initial value for the calculator memory. Defaults to 0.0.
decimal_places (int, optional): The number of decimal places to round results to. Defaults to 3.

Raises:
No specific exceptions are raised by this method.

Returns:
None

get_memory(self) -> float

Get the current value stored in the calculator memory.

Parameters:
None

Raises:
No specific exceptions are raised by this method.

Returns:
float: The current numeric value stored in the calculator memory.

get_decimal_places(self) -> int

Get the current number of decimal places used for rounding in the calculator.

Parameters:
None

Raises:
No specific exceptions are raised by this method.

Returns:
int: The current number of decimal places used for rounding.

_round_result(self, result: float) -> float

Round a floating-point number to the specified decimal places.

Parameters:
result (float): The floating-point number to be rounded.

Raises:
ValueError: If the result is not a valid floating-point number.

Returns:
float: The rounded result with the specified number of decimal places.

_validate_argument(self, x) -> None

Validate the input argument to ensure it is either an integer or a float.

Parameters:
x (int or float): The input value to be validated.

Raises:
TypeError: If the input x is not of type int or float.

Returns:
None

Basic Operations

Addition

Parameters:
x (float): The value to be added to the memory.

Raises:
ValueError: If the input `x` is not a valid float.
        
Returns:
float: The updated value in the calculator memory after adding `x`.

Example:
To add 5.0 to the memory:
```python
calculator.add(5.0)
```

Subtraction

Parameters:
x (float): The value to be subtracted from the memory.

Raises:
ValueError: If the input `x` is not a valid float.
        
Returns:
float: The updated value in the calculator memory after subtracting `x`.

Example:
To subtract 2.0 from the memory:
```python
calculator.subtract(2.0)
```

Multiplication

Parameters:
x (float): The value to multiply the memory by.

Raises:
ValueError: If the input `x` is not a valid float.
        
Returns:
float: The updated value in the calculator memory after adding `x`.

Example:
To multiply the memory by 5:
```python
calculator.multiply(5.0)
```

Division

Parameters:
x (float): The value for the memory to be divided by.

Raises:
ValueError: If the input `x` is not a valid float.
ZeroDivisionError: If the input `x` is zero.
        
Returns:
float: The updated value in the calculator memory after dividing by `x`.

Example:
To divide the memory by 5:
```python
calculator.divide(5.0)
```

Nth root

Parameters:
        n (int): The exponent for the Nth root.

Raises:
    ValueError: If the input `n` is not a valid integer.
    ValueError: If the input `n` is less than or equal to 0.
        
Returns:
    float: The updated value in the calculator memory after taking the `n` root.

Example:
    To calculate the square root of the memory:
```python
calculator.n_root(2)
```

Testing

pip install pytest
pytest test_calculator.py

Contributing

Feel free to contribute by opening issues or pull requests. Bug reports, suggestions, and improvements are welcome!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

calculator_sf-0.5.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

calculator_sf-0.5.0-py2.py3-none-any.whl (5.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file calculator_sf-0.5.0.tar.gz.

File metadata

  • Download URL: calculator_sf-0.5.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.29.0

File hashes

Hashes for calculator_sf-0.5.0.tar.gz
Algorithm Hash digest
SHA256 01b3a1ce7a3b6fdda9315e6b23cbde218a53ba16a846285d851bb26635313f90
MD5 fde5634c49782f007e20a7c33e7cf8dd
BLAKE2b-256 608341ea9bf38bf379f2f4a0251c1270f48d1ded81cb9e006c3fdb09dd323c78

See more details on using hashes here.

File details

Details for the file calculator_sf-0.5.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for calculator_sf-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2a28c94beafd06e6a70e0be901a541dda97394b26d8d9077ae52771844fda290
MD5 604dc3a53b484df2db8b47ce04bd5da1
BLAKE2b-256 64c0cbb204b89274a2db4e5ccbe419d0b6e0cfed8c8c1cfa06130c7375bc4d63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page