Skip to main content

Math utilities in Python

Project description

Mathematics Utilities

Mathematics utilities in Python.

PyPI PyPI - Python Version PyPI - License PyPI - Downloads PyPI - Downloads Coverage Status GitHub code size in bytes GitHub last commit DeepSource

MathUtil class

Methods

Method Description Parameters Returns
average Calculates the weighted average of a list of numbers. numbers (list) The list of numbers to calculate the average of.
weights (list) The list of weights.
DecimalNumber The average of the numbers.
floor Returns the floor of a number. number The number to calculate the floor of.
digits Digits amount to cut from the number.
number type The floor of the number.
is_all_numbers Checks if all elements in a list are numbers. elements (list) The list of elements to check. bool True if all elements are numbers, False otherwise.
max Returns the maximum number from variable elements, which can be numbers or an iterable objects like list. *elements Variable arguments, which can be numbers or an iterable objects like list. number type The maximum number in the list.
min Returns the minimum number from variable elements, which can be numbers or an iterable objects like list. *elements Variable arguments, which can be numbers or an iterable objects like list. number type The minimum number in the list.
sum_0_to_n Returns the sum of numbers from 0 to n. n The number to sum up to. number type The sum of numbers from 0 to n.

Examples:

  • MathUtil.average

    Code

    from nrt_math_utils.math_utils import MathUtil
    
    # Calculate the weighted average of a list of numbers
    average = MathUtil.average([1, 2, 3, 4, 5], [1, 5, 6, 7, 8])
    
    print(average)
    

    Output

    3.592593
    
  • MathUtil.floor

    Code

    from nrt_math_utils.math_utils import MathUtil
    
    # Calculate the floor of a number
    floor = MathUtil.floor(3.14159, 2)
    
    print(floor)
    

    Output

    3.14
    
  • MathUtil.is_all_numbers

    Code

    from nrt_math_utils.math_utils import MathUtil
    
    # Check if all elements in a list are numbers
    is_all_numbers = MathUtil.is_all_numbers([1, 2, 3, 4, 5])
    
    print(is_all_numbers)
    

    Output

    True
    
  • MathUtil.max

    Code

    from nrt_math_utils.math_utils import MathUtil
    
    # Get the maximum number from a list
    max_number = MathUtil.max(1, [2, 7, [8, 9]], 3, 4, 5)
    
    print(max_number)
    

    Output

    9
    
  • MathUtil.min

    Code

    from nrt_math_utils.math_utils import MathUtil
    
    # Get the minimum number from a list
    min_number = MathUtil.min(1, [2, 7, [0, 9]], 3, 4, 5)
    
    print(min_number)
    

    Output

    0
    
  • MathUtil.sum_0_to_n

    Code

    from nrt_math_utils.math_utils import MathUtil
    
    # Get the sum of numbers from 0 to n
    sum_0_to_n = MathUtil.sum_0_to_n(5)
    
    print(sum_0_to_n)
    

    Output

    15
    

DecimalNumber class

Represents a decimal number with a fixed number of decimal places.

Examples:

  • Create a DecimalNumber object

    Code

    from nrt_math_utils.nrt_numbers import DecimalNumber
    
    # Create a DecimalNumber object with a value of 3.14159 and 2 decimal places
    decimal_number = DecimalNumber(3.14159, 2)
    
    print(decimal_number)
    

    Output

    3.14
    
  • Create a DecimalNumber object with default decimal places

    Code

    from nrt_math_utils.nrt_numbers import DecimalNumber
    
    # Create a DecimalNumber object with a value of 3.14159 and default 6 decimal places
    decimal_number = DecimalNumber(3.1415926535897)
    print(decimal_number)
    

    Output

    3.141593
    
  • Add two DecimalNumber objects

    Code

    from nrt_math_utils.nrt_numbers import DecimalNumber
    
    # Create two DecimalNumber objects
    decimal_number_1 = DecimalNumber(3.14159, 2)
    decimal_number_2 = DecimalNumber(2.71828, 2)
    
    # Add the two DecimalNumber objects
    result = decimal_number_1 + decimal_number_2
    
    print(result)
    

    Output

    5.86
    
  • Subtract a number from DecimalNumber

    Code

    from nrt_math_utils.nrt_numbers import DecimalNumber
    
    # Create a DecimalNumber object
    decimal_number = DecimalNumber(3.14159, 2)
    
    # Subtract a number from the DecimalNumber object
    result = decimal_number - 1.23456
    
    print(result)
    

    Output

    1.91
    
  • Multiply DecimalNumber with other number

    Code

    from nrt_math_utils.nrt_numbers import DecimalNumber
    
    # Create a DecimalNumber object
    decimal_number = DecimalNumber(3.14159, 2)
    
    # Multiply the DecimalNumber object with another number
    result = decimal_number * 2
    
    print(result)
    

    Output

    6.28
    
  • Compare DecimalNumber to other number

    Code

    from nrt_math_utils.nrt_numbers import DecimalNumber
    
    # Create a DecimalNumber object
    decimal_number = DecimalNumber(3.14159, 2)
    
    # Compare the DecimalNumber object to another number
    result = decimal_number > 3.14
    
    print(result)
    

    Output

    True
    

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

nrt_math_utils-1.0.4.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

nrt_math_utils-1.0.4-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file nrt_math_utils-1.0.4.tar.gz.

File metadata

  • Download URL: nrt_math_utils-1.0.4.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.0

File hashes

Hashes for nrt_math_utils-1.0.4.tar.gz
Algorithm Hash digest
SHA256 a285acccad51ce99a24ebbf75c27c568bcef9aeab2d32e31afd5040c62fc945c
MD5 45c441997f267d1555b42b32dc45af7f
BLAKE2b-256 ed2a796123816d0f9e033190765faee9f27bbf30fe6432c1746f544d254e6cd4

See more details on using hashes here.

File details

Details for the file nrt_math_utils-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: nrt_math_utils-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.0

File hashes

Hashes for nrt_math_utils-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2a5bb9d8cf507990bad17625ef68cc9fdaef89634a63b2d3d8078d84945e92ec
MD5 b72e27e5e2b6af97acdca32a53cb15ce
BLAKE2b-256 eecb5493102f6af9cc54aaeccf254f371c8ff2b8c32abfd8cadef58984170b4d

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