Skip to main content

Simple arithmetic lib for students

Project description

__’rithmetic__

“rithmetic” is a budding python library which aims to provide arithmetic and math assistance to students using python to build math related functionality or apps.

How to Install:

Run the following command in Terminal/CLI –

  • pip install rithmetic

If you are updating to a new version of rithmetic:

  • pip install rithmetic --force-reinstall

After installation completes, run the following command in Terminal/CLI to see the welcome message –

  • rith

To check the version of rithmetic, run the following command in Terminal/CLI –

  • rith-version

License and terms of use:

rithmetic comes with the MIT license which means that anyone, anywhere can use it for any open source or even closed source application.

Current functionality:

  1. Convert a number from one base to another (Converters)
  2. Check/Verify if a number is from a specified base (Verifiers)
  3. Add, Subtract, Multiply and Divide any two numbers in desired base (Operators)
  4. Evaluate expressions with multiple arithmetic operations in desired base (Calculators)

Best way to import:

Import all the functions from rithmetic at once

from rithmetic import *

Functions:

Converters

base(num, fromB, toB)

Converts any number from one base to another. (supported bases are base-2 to base-16)

num – any number | can be int or float or str type

fromB – base of ‘num’ | can be both int and str type

toB – ‘num’ gets converted to this base | can be both int and str type

Returns – The converted number in int or str or float type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

number = base(1111,2,16)
print(number)

This will print ‘F’. As 1111 in binary gets converted to F in hexadecimal.

dectosub(num, toB)

Converts any number from Decimal to any other sub-decimal base. (supported bases are base-2 to base-9)

num – any number | can be int or float or str type

toB – ‘num’ gets converted to this base | can be both int and str type

Returns – The converted number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

number = dectosub(23,5)
print(number)

This will print 43. As 23 in decimal gets converted to 43 in base-5.

subtodec(num, fromB)

Converts any number from any sub-decimal base to Decimal. (supported bases are base-2 to base-9)

num – any number | can be int or float or str type

fromB – base of ‘num’ | can be both int and str type

Returns – The converted number in int or float type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

number = subtodec(25,6)
print(number)

This will print 17. As 25 in base-6 gets converted to 17 in decimal.

Quick single parameter converters

  • Decimal to another base:

dectob2(num)

dectob3(num)

dectob4(num)

dectob5(num)

dectob6(num)

dectob7(num)

dectob8(num)

dectob9(num)

dectob11(num)

dectob12(num)

dectob13(num)

dectob14(num)

dectob15(num)

dectob16(num)

num – any number | can be int or float or str type

Return – The converted number in int or str type OR ‘Invalid number’

  • Any base to Decimal

b2todec(num)

b3todec(num)

b4todec(num)

b5todec(num)

b6todec(num)

b7todec(num)

b8todec(num)

b9todec(num)

b11todec(num)

b12todec(num)

b13todec(num)

b14todec(num)

b15todec(num)

b16todec(num)

num – any number | can be int or float or str type

Return – The converted number in int or float type OR ‘Invalid number’

Verifiers

chkbase(num, base)

Checks if a number is from a specified base. (supported bases are base-2 to base-16)

num – any number | can be int or float or str type

base – base of ‘num’ to be checked | can be both int and str type

Returns – True OR False OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

check = chkbase('23F',16)
print(check)

This will print True. As 23F is from base-16.

Quick single parameter verifiers

chk2(num)

chk3(num)

chk4(num)

chk5(num)

chk6(num)

chk7(num)

chk8(num)

chk9(num)

chk10(num)

chk11(num)

chk12(num)

chk13(num)

chk14(num)

chk15(num)

chk16(num)

num – any number | can be int or float or str type

Return – True OR False OR ‘Invalid number’

Operators

add(num1, num2, Base)

Adds two numbers in desired base. (supported bases are base-2 to base-16)

num1 – any number | can be int or float or str type

num2 – any number, which will be added to ‘num1’ | can be int or float or str type

Base– base of num1 and num2 | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

sum = add('2a',12,11)
print(sum)

This will print 41. As (2a + 12) in base-11 is 41.

sub(num1, num2, Base)

Subtracts one number from another in desired base. (supported bases are base-2 to base-16)

num1 – any number | can be int or float or str type

num2 – any number, which will be subtracted from ‘num1’ | can be int or float or str type

Base– base of num1 and num2 | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

diff = sub(23,'6e',16)
print(diff)

This will print -4B. As (23 – 6E) in base-16 is -4B.

mul(num1, num2, Base)

Multiplies two numbers in desired base. (supported bases are base-2 to base-16)

num1 – any number | can be int or float or str type

num2 – any number, which will be multiplied with ‘num1’ | can be int or float or str type

Base– base of num1 and num2 | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

prod = mul(12,24,11)
print(prod)

This will print 288. As (12 * 24) in base-11 is 288.

div(num1, num2, Base)

Divides a number by another in desired base. (supported bases are base-2 to base-16)

num1 – any number | can be int or float or str type

num2 – any number, ‘num1’ will be divided by this | can be int or float or str type

Base– base of num1 and num2 | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’ OR ‘Cannot divide by zero’

Example:

from rithmetic import *

quo = div(22,13,13)
print(quo)

This will print 1.99999999999999. AS (22 / 13) in base-13 is 1.99999999999999

addmany(*nums, Base)

Adds multiple numbers together in desired base. (supported bases are base-2 to base-16)

*nums – a list or stream of numbers to be added together | can be int or float or str type

Base– base of *nums | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

nums = [12, 11, 14, 15, 12]
res = addmany(*nums, Base=6)
print(res)

This will print 112. As sum of the numbers in the list ‘nums’ is 112 in base-6

mulmany(*nums, Base)

Multiplies multiple numbers together in desired base. (supported bases are base-2 to base-16)

*nums – a list or stream of numbers to be multiplied together | can be int or float or str type

Base– base of *nums | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’

Example:

from rithmetic import *

nums = [1.2, 1.1, 1.4, 1.5, 1.2]
res = mulmany(*nums, Base=6)
print(res)

This will print 10.200515555555555555524. As product of the numbers in the list ‘nums’ is 10.200515555555555555524 in base-6

power(num1, num2, Base)

Calculates the exponent result of two numbers in desired base. (supported bases are base-2 to base-16)

num1 – any number | can be int or float or str type

num2 – any integer, which will be the power of ‘num1’ | can be int or float or str type

Base– base of num1 and num2 | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’ OR ‘Undefined’

Example:

from rithmetic import *

res = power(12,2,10)
print(res)

This will print 144. As 12 to the power of 2 is 144 in base-10.

Calculators

exp(expression, Base)

Parses through a string expression and calculates it’s result in desired base. (supported bases are base-2 to base-16)

expression – any arithmetical expression | should be str type

Base– base of calculation and numbers in the expression | can be both int and str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’ OR ‘Invalid brackets’ OR ‘Undefined power operation’ OR ‘Cannot divide by zero’ OR ‘Fractional powers not supported’ OR ‘Operator missing operand’ OR ‘Invalid operators’ OR ‘Empty brackets’ OR ‘Brackets without any number’

Example:

from rithmetic import *

res = exp('((101+101)11-10)/10^10-100',2)
print(res)

This will print 11. As the result of the entered expression is 11 in base-2.

exp10(expression)

Parses through a string expression and calculates it’s result in base-10.

expression – any arithmetical expression | should be str type

Returns – The resultant number in int or str type OR ‘Invalid number’ OR ‘Invalid base value’ OR ‘Invalid brackets’ OR ‘Undefined power operation’ OR ‘Cannot divide by zero’ OR ‘Fractional powers not supported’ OR ‘Operator missing operand’ OR ‘Invalid operators’ OR ‘Empty brackets’ OR ‘Brackets without any number’

Example:

from rithmetic import *

res = exp10('13.2((12.5-1.2*4)*3.12+7)-5.16/4*2.1')
print(res)

This will print 406.8078. As the result of the entered expression is 406.8078 in base-10.

Disclaimer – rithmetic functions do not round off any numbers and the max level of decimal points is set to 100. So, you will find some str type output values with a large number of decimal points (limited to a max of 100).

exp and exp10 both support ‘^’ and ‘**’ as exponent operators.

rithmetic does not support fractional powers yet.

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

rithmetic-0.0.16.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

rithmetic-0.0.16-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file rithmetic-0.0.16.tar.gz.

File metadata

  • Download URL: rithmetic-0.0.16.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for rithmetic-0.0.16.tar.gz
Algorithm Hash digest
SHA256 6c043a90e7644ac7969116d4f8be92a9dc7451290e955c75ff0a4618fe26101f
MD5 cd8ee41b41ee521d569562b75b17baac
BLAKE2b-256 ca40b2c29458397e0107af30d96fffdb32a852bca2b519247529e19f3b513aa6

See more details on using hashes here.

File details

Details for the file rithmetic-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: rithmetic-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for rithmetic-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 b056a9dcf58c663777ed093f56afa4bdbc8bcf57849c3eb61dde5b06b7169fee
MD5 8d8d85e1eb8c6d49d45e16b2026e6f6d
BLAKE2b-256 3a13cb0de6831e1a4f4ef8f6d52f54c79c3c3598a8e9e59b78e5aad89de47395

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