Radix conversion module.
Project description
rdx
A radix conversion module.
Implementation of
Installation
pip install rdx
Usage
This module provides the following functions:
int_to_digits(n, b=10)
: Returns the digits of numbern
in baseb
.digits_to_int(d, b=10)
: Returns the number represented by the digitsd
in baseb
.>>> from rdx import * >>> int_to_digits(42, 2) [0, 1, 0, 1, 0, 1] >>> int_to_digits(42, 16) [10, 2] >>> digits_to_int([27, 1], 42) 69
int_to_len_digits(n, b=10)
: Returns the amount of digits the numbern
represented in baseb
needs.>>> int_to_len_digits(1024, 2) 11
digits_to_characters(d, alphabet=digits+ascii_letters)
: Converts integer digitsd
to characters of the given alphabet.characters_to_digits(s, alphabet=digits+ascii_letters)
: Converts characterss
of the given alphabet to integer digits.>>> digits_to_characters([0, 1, 9, 10, 15, 16]) ['0', '1', '9', 'a', 'f', 'g'] >>> characters_to_digits(['0', '1', '9', 'a', 'f', 'g']) [0, 1, 9, 10, 15, 16]
digits_to_string(d, alphabet=string.digits+string.ascii_letters)
: Converts integer digitsd
to a right-to-left string in the given alphabet.string_to_digits(s, alphabet=string.digits+string.ascii_letters)
: Converts a right-to-left strings
in the given alphabet to integer digits.>>> digits_to_string([0, 0, 15]) 'f00' >>> string_to_digits('f12') [2, 1, 15]
Conventions
Data type
Currently digits or characters are returned as lists. In the future this will be switched to tuples as they are more similar to strings, and a digit representation is similar to a string.
Digit or character arguments should be provided in form of an iterable, not an iterator, as in most functions the will be iterated twice and an iterator will be exhausted after the first run.
Ordering
As a digit representation a list of integers is used,
where every element represents a digit, all ordered in ascending positions.
So the lowest digit at position with index 0, will be at position 0.
But this also means, that when a list of digits is printed,
it will be ordered left-to-right,
opposed to the usual human-readable right-to-left.
E.g. 12
(twelve) corresponds to [2, 1]
.
int_to_len_digits(n)
vs len(int_to_digits(n))
len(int_to_digits(n))
is the naive and non-optimal approach.
int_to_len_digits(n)
calculates the number of digits intelligently as
int(log(n, b))+1
as mentioned
in the discussion.
But to avoid errors due to math.log
using floating point arithmetic
sympy.log
is used, adding a hefty overhead. A comparison for time critical applications:
(integers with just 3s were choosen because random values introduce to much
noise and because 100... seemed to risky for a systematic error and 33... lies
exactly between those 100...s on a logarithmic scale)
TODO
- put digits in tuples instead of lists
- non-standard positional numeral systems
- mixed radix
- factorial
- primorial
- ...
- negative base
- complex base
- mixed radix
License (MIT)
Copyright (c) 2023 Sebastian Gössl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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
Built Distribution
File details
Details for the file rdx-1.1.tar.gz
.
File metadata
- Download URL: rdx-1.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 170c099756969ff9a1603569e29d7017bfbb6f9e0164122e1aa9d3079ea0d069 |
|
MD5 | 9c9db85ce4ba10480226bb732710c63f |
|
BLAKE2b-256 | d09fad0cdb56e7bd6e60c0b8287351da67bc36e5deca1d3336f6134115ad38e4 |
File details
Details for the file rdx-1.1-py3-none-any.whl
.
File metadata
- Download URL: rdx-1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6885d6109cba62edecf53fbd94e7aac62085fadc7dbebe6bc41424f72dbc0d3 |
|
MD5 | 68a9d7cbcda8c4544689d4b6ca54f0e2 |
|
BLAKE2b-256 | 0a7fc4269d3c8a4297bcfa6aac76e393004492c8b00688f98c73dfdbdd83bb8e |