Skip to main content

radix-ops

Convert numbers - both integer and floating-point - from one base to another. Perform arithmetic operations in a given base.

Installation

pip install radix-ops

Usage

from radix import Num

a = Num(20, 16)    # The number 20 in base 16
print(a.to(2))     # Convert to base 2 (100000) - inplace conversion

a = Num(20, 16)
b = Num('ff', 16)  # FF or 255 in base 16
print(a + b)       # Result in base 16 (11F)

c = a.conv(2)      # To create a new instance upon conversion (to base 2)
print(a)           # 20
print(c)           # 100000

An easier way to evaluate expressions:

from radix import expr

# All numbers in base 16. result is a `Num` instance.
result = expr('a + b * c', 16)

print(result)  # prints 8E
print(result.to(10))  # prints 142

(Signed) binary numbers

>>> from radix import Bin  # `Bin` is a subclass of `Num`
>>> num = Bin(-7)
>>>
>>> # -ve number in 2's complement
>>> num
1001
>>> num.twos_compl()  # 2's complement
'1001'
>>> num.ones_compl()  # 1's complement
'1000'
>>> num.format(size=8, blanks_every=4)  # format 2's complement
'1111 1001'
>>> num.sign_mag()  # sign magnitude representation
'1111'
>>>
>>> # -10 in 2's complement
>>> Bin(-5) + Bin(3) - Bin(8)  # 2's complement arithmetic
10110

Examples

>>> from radix import Num, expr
>>> Num(value='FE', base=16).to(base=10)
254
>>> Num(1100, 2).to(10)
12
>>> Num(10.75).to(16)  # When base is 10, it can be omitted.
A.C
>>> Num(10.75).to(2)
1010.11
>>> pi = 3.141592653589793
>>> Num(pi).to(16)
3.243F6A8885
>>> Num(-1001, 2).to(10)
-9
>>> (Num('1a', 16) - Num('ff', 16)) * Num(2, 16)  # (26 - 255) * 2 = -458 = -0x1ca
-1CA
>>> expr('-fe', 16, show=True).to(10)
-Num('fe', 16)
-254
>>> expr('b / (a + 1.5) * 2', 12)
1.B15A50B68B
>>> from radix import dim_radix_compl, radix_compl
>>> dim_radix_compl(Num('012398'))  # 9's complement (diminished radix complement)
'987601'
>>> radix_compl(Num('012398'))  # 10's complement (radix complement)
'987602'

Note

  1. Base should be between 2 and 36.

  2. The .to method mutates the number, i.e. changes its value and base.

  3. Very large or very small floating-point numbers should be in quotes.

Metadata

Release files for radix-ops 0.3.5

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

Source distribution (sdist)

Source distribution for radix-ops 0.3.5
File Size Uploaded
radix-ops-0.3.5.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for radix-ops 0.3.5
File Interpreter ABI Platform
radix_ops-0.3.5-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / radix-ops-0.3.5.tar.gz

Download URL radix-ops-0.3.5.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2a85bbd03aac7bb1bac8f364b6cd2e31d3503ab3bdb8ba2b4794789de0935568
BLAKE2b-256 checksum
How to use checksums
e2ea85448b54e622be4ba9fad0f4274f6ce8a05c2e7c68f39b703913ea4ddba9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

Release files / radix_ops-0.3.5-py3-none-any.whl

Download URL radix_ops-0.3.5-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0dfabca17b7f170e444f09bef313e8dc10615cc8d9849a6f02e81b7f9e095c04
BLAKE2b-256 checksum
How to use checksums
7347c3705ddfe51750dfddbd407603f6b2d3c33068768bbd18c2ac243272df3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.3.5 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

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