Skip to main content

In this repository, a library has been provided that allows you to perform the four basic operations of addition, subtraction, multiplication and division on large and very large numbers.

Project description

Large Numbers

In this repository, a library has been provided that allows you to perform the four basic operations of addition, subtraction, multiplication and division on large and very large numbers. We tried to make this library as fast and efficient as possible.

Note: In this library, powering is not supported yet, but it will be supported in the next versions.

Installation

To install, you can use pip as follows.

pip install LargeNumbers

To install from the repository, just clone the project, change the path to the corresponding directory and then install lib using pip.


git clone https://github.com/HRSadeghi/LargeNumbers.git

cd LargeNumbers

pip install .

Usage

The easiest way to use this library is as follows,

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

a = LargeNumber('125763678041689463179.45761346709461437894')
b = LargeNumber('-746011541145.47464169741644487000085')

# Negation
print(-a)
print(-b)

# Addition and Subtraction
print(a+b)
print(a-b)
print(-a+b)

# Multiplication
print(a*b)

# Division
largeNumberFormat.precision = 100
largeNumberFormat.return_fracation = False

print(a/b)


In the above code snippet, because the number of digits for the division operation may be very large, so a maximum can be defined for it using largeNumberFormat.precision. If the number of digits appearing in the division process is more than the number of digits allocated for largeNumberFormat.precision, then a letter L appears at the end of the number (this letter has no effect in further calculations).

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

largeNumberFormat.precision = 2

a = LargeNumber('1')
b = LargeNumber('7')

print(a/b)

You can also define one of the numbers as a string, int or float,

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

a = LargeNumber('125763678041689463179.45761346709461437894')
b = '-746011541145.47464169741644487000085'

# Ops
print(a+b)
print(a-b)
print(a*b)
print(a/b)



a = '125763678041689463179.45761346709461437894'
b = LargeNumber('-746011541145.47464169741644487000085')

# Ops
print()
print(a+b)
print(a-b)
print(a*b)
print(a/b)

But if the input is a string, you cannot negate it first.

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

a = '125763678041689463179.45761346709461437894'
b = LargeNumber('-746011541145.47464169741644487000085')

print(-a+b)

# In this case, you will encounter the following error
"""
TypeError: bad operand type for unary -: 'str'
"""

You can also give input numbers as fractions,


from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

a = LargeNumber('1/2')
b = LargeNumber('-3/14')

# Ops (return the result as a fraction)
largeNumberFormat.return_fracation = True

print(a+b)
print(a-b)
print(a*b)
print(a/b)
##

# Ops (return the result as a decimal)
largeNumberFormat.precision = 5
largeNumberFormat.return_fracation = False

print()
print(a+b)
print(a-b)
print(a*b)
print(a/b)
##

It is also possible to give numbers as input and get the output as a fraction or non-fraction,

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

a = LargeNumber('2.142')
b = LargeNumber('-3/14')

# Ops (return the result as a fraction)
largeNumberFormat.return_fracation = True

print(a+b)
print(a-b)
print(a*b)
print(a/b)
##

a = LargeNumber('1.134')
b = LargeNumber('-1.57')

# Ops (return the result as a decimal)
largeNumberFormat.return_fracation = True

print()
print(a+b)
print(a-b)
print(a*b)
print(a/b)
##

Recurring decimal (repeating decimal)

Numbers such as $\dfrac{1}{3}$, $\dfrac{1}{7}$ and similar numbers do not have a finite decimal representation. Therefore, we are facing a problem to perform division in these numbers. But these numbers can be shown in periodic form. As a result, $\dfrac{1}{3}$ can be represented by $0.\overline{3}$ and $\dfrac{1}{7}$ by $0.\overline{142857}$.

Here, the letter R is used to show the beginning of the period. Therefore, we represent a number like $0.\overline{3}$ with 0.R3, a number like $0.\overline{7}$ with 0.R7 and a number like $0.12\overline{42}$ with 0.12R42.

According to this way of representation, we can apply the four operations of addition, subtraction, multiplication and division on the same representation.

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

largeNumberFormat.return_repeating_form = True
largeNumberFormat.return_fracation = False
largeNumberFormat.precision = 30


a = LargeNumber('0.R81')
b = LargeNumber('0.134R1')

# Ops 
print(a+b)
print(a-b)
print(a*b)
print(a/b)
##


a = LargeNumber('0.12R81')
b = LargeNumber('0.665')

# Ops 
print()
print(a+b)
print(a-b)
print(a*b)
print(a/b)
##


a = LargeNumber('1/7')
b = LargeNumber('0.665')

# Ops 
print()
print(a+b)
print(a-b)
print(a*b)
print(a/b)
##

In the above code snippet, largeNumberFormat.return_repeating_form specifies whether the number is in recurring (repeating) form or not. If the number of digits in the periodic display exceeds the number of digits dedicated to the largeNumberFormat.precision, the number will not be displayed recurringly and an L will appear at the end of the number (this letter has no effect in further calculations).

from LargeNumbers.LargeNumber import LargeNumber, largeNumberFormat

largeNumberFormat.return_repeating_form = True
largeNumberFormat.return_fracation = False
largeNumberFormat.precision = 6

a = LargeNumber('1')
b = LargeNumber('7')

print(a/b)

largeNumberFormat.precision = 5

print()
print(a/b)

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

LargeNumbers-0.1.8.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

LargeNumbers-0.1.8-py2.py3-none-any.whl (12.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file LargeNumbers-0.1.8.tar.gz.

File metadata

  • Download URL: LargeNumbers-0.1.8.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for LargeNumbers-0.1.8.tar.gz
Algorithm Hash digest
SHA256 3b5ec636d5478c5e186afb720b709ad3141d830eaa55c2b3ac81e3a4cf628a05
MD5 2d2c855709a7302371dd0a44a6513360
BLAKE2b-256 58acbfd3d7398965f1baf067879c4d22b2c35de9b381976d0d53df62c3db949e

See more details on using hashes here.

File details

Details for the file LargeNumbers-0.1.8-py2.py3-none-any.whl.

File metadata

  • Download URL: LargeNumbers-0.1.8-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.13

File hashes

Hashes for LargeNumbers-0.1.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ccb22e7e4681bfcbfcd18debe8a18600dff94bdc543f44d995726fdf13c2bb4c
MD5 25bc45743501f0d8991befd62b1ba876
BLAKE2b-256 e83e39b2d8569430e383e99fb7566977c141542b37417ef868a4eeb4473a9dfa

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