Skip to main content

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)

Metadata

Release files for LargeNumbers 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for LargeNumbers 0.1.8
File Size Uploaded
LargeNumbers-0.1.8.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for LargeNumbers 0.1.8
File Interpreter ABI Platform
LargeNumbers-0.1.8-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 23.6 kB

Release files / LargeNumbers-0.1.8.tar.gz

Download URL LargeNumbers-0.1.8.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3b5ec636d5478c5e186afb720b709ad3141d830eaa55c2b3ac81e3a4cf628a05
BLAKE2b-256 checksum
How to use checksums
58acbfd3d7398965f1baf067879c4d22b2c35de9b381976d0d53df62c3db949e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.13

Release files / LargeNumbers-0.1.8-py2.py3-none-any.whl

Download URL LargeNumbers-0.1.8-py2.py3-none-any.whl
Size 12.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ccb22e7e4681bfcbfcd18debe8a18600dff94bdc543f44d995726fdf13c2bb4c
BLAKE2b-256 checksum
How to use checksums
e83e39b2d8569430e383e99fb7566977c141542b37417ef868a4eeb4473a9dfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.7.13

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page