Skip to main content

A high-precision Python package for converting floating-point numbers between different bases with scientific notation support

Project description

floating-base-converter

Python License Tests Coverage Precision Scientific Notation

High-performance floating-point number base conversion library supporting decimal, binary, octal, and hexadecimal number systems.

Features

  • Zero dependencies - Pure Python implementation
  • High precision - Configurable fractional precision (1-100 digits)
  • Scientific notation - Full support for exponential notation (e.g., 1.23e-4)
  • Multiple interfaces - Python API and CLI
  • Comprehensive validation - Input validation with detailed error messages
  • Full test coverage - 91% code coverage with extensive test suite

Installation

pip install floating-base-converter

API Reference

BaseConverter

from base_converter import BaseConverter

converter = BaseConverter(default_precision=10)

Core Methods

Method Description Example
decimal_to_binary(n, precision=None) Decimal → Binary 3.14"11.001000111101011100"
decimal_to_hex(n, precision=None) Decimal → Hexadecimal 255.5"FF.8"
binary_to_decimal(s, precision=None) Binary → Decimal "1010.01""10.25"
hex_to_decimal(s, precision=None) Hexadecimal → Decimal "A.8""10.5"
convert(n, from_base, to_base, precision=None) Universal converter ("FF", 16, 2)"11111111"

Input Types

  • Decimal: int, float, or str
  • Non-decimal: str only (e.g., "1010.01", "FF.8")

Scientific Notation Support

For decimal inputs, scientific notation is fully supported:

converter = BaseConverter()

# Scientific notation examples
converter.decimal_to_hex("1.23e-4", precision=10)    # → "0.00080F98FA"
converter.decimal_to_binary("6.626e-34", precision=50)  # → "0.000..."
converter.decimal_to_octal("1e5")                    # → "303240"

# Both uppercase and lowercase 'e' supported
converter.decimal_to_hex("1.5E-3")  # Same as "1.5e-3"

Note: Scientific notation is only supported for decimal (base 10) inputs.

Supported Prefixes

  • Binary: 0b1010 or 1010
  • Octal: 0o17 or 17
  • Hexadecimal: 0xFF or FF

CLI Interface

base-converter <number> -f <from_base> -t <to_base> [-p <precision>]

Examples

# Decimal to hexadecimal
base-converter 3.14159 -f 10 -t 16 -p 6
# Output: 3.14159 (decimal) = 3.243F3E (hexadecimal)

# Binary to decimal  
base-converter "1010.01" -f 2 -t 10
# Output: 1010.01 (binary) = 10.25 (decimal)

# Hexadecimal to binary
base-converter "FF.8" -f 16 -t 2 -p 4
# Output: FF.8 (hexadecimal) = 11111111.1 (binary)

Error Handling

from base_converter import ConversionError

try:
    result = converter.binary_to_decimal("102")  # Invalid binary digit
except ConversionError as e:
    print(f"Error: {e}")

Development

Setup

git clone https://github.com/yeabwang/floating-base-converter.git
cd floating-base-converter
pip install -e ".[dev]"

Testing

pytest                          # Run tests
pytest --cov=base_converter     # With coverage

Code Quality

black base_converter tests/     # Format code
flake8 base_converter tests/    # Lint code  
mypy base_converter/           # Type checking

Performance Benchmarks

Precision vs Speed & Memory

Precision Avg Time (ms) Memory (KB) Accuracy Use Case
10 0.93 3.0 10 digits General purpose, fastest
25 1.52 3.0 25 digits Financial calculations
50 1.74 4.2 50 digits Scientific computing
75 2.98 5.6 75 digits Research applications
100 2.53 7.0 100 digits Maximum precision

Cross-Base Conversion Performance

Conversion Time (ms) Complexity Notes
Decimal → Hex 0.37 O(n) Most efficient
Decimal → Binary 0.43 O(n) Longest output
Decimal → Octal 0.44 O(n) Base-8 conversion
Cross-conversions 0.37-0.44 O(n) Via decimal intermediate

Scientific Notation Overhead

Input Type Avg Time (ms) Overhead Performance Impact
Regular notation 0.50 Baseline Standard performance
Scientific (e0) 0.54 +7.6% Minimal overhead
Scientific (e+2) 0.54 +8.2% Minimal overhead
Scientific (e-4) 0.63 +25.6% Moderate overhead

Key Performance Insights

  • Linear precision scaling: Performance scales predictably from 0.93ms (10 digits) to 2.53ms (100 digits) with 2.7x scaling factor
  • Memory scaling: Memory usage scales with precision from 3.0KB (10 digits) to 7.0KB (100 digits)
  • Reasonable performance: Individual conversions complete in under 3ms for most operations
  • Precision-focused design: Prioritizes accuracy over raw speed for high-precision calculations
  • Scientific notation: Generally low overhead (<10%) except for small numbers with negative exponents

High Precision Arithmetic

The library uses Python's decimal module for arbitrary precision arithmetic, providing significant advantages over float-based implementations:

Precision Capabilities

  • Range: 1-100 fractional digits (doubled from previous 50-digit limit)
  • Accuracy: True arbitrary precision, not limited by IEEE 754 float precision (~17 digits)
  • Memory Efficiency: Benchmarks show progressive memory scaling from 3.0KB to 7.0KB

Example: 80-digit precision π conversion

from base_converter import BaseConverter

converter = BaseConverter()
pi_80_digits = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862"

# Convert to hexadecimal with 80 digits precision
hex_result = converter.decimal_to_hex(pi_80_digits, precision=80)
print(hex_result)
# Output: 3.243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C883A699724B1DAFA4F

Performance Benefits

  • Accuracy: True arbitrary precision, not limited by IEEE 754 float precision (~17 digits)
  • Memory: Efficient memory usage with predictable scaling based on precision requirements
  • Scalability: Maintains good performance even at 100+ digits with 2.7x scaling factor
  • Precision Focus: Prioritizes accuracy over raw speed for high-precision calculations

Algorithm Complexity

Time Complexity

  • Overall: O(log n + p) where n is input integer value, p is precision
  • Integer conversion: O(log n) - logarithmic in input size
  • Fractional conversion: O(p) - linear in requested precision
  • Scientific notation: Minimal parsing overhead (7.6-25.6%) + O(p) conversion

Space Complexity

  • Overall: O(p) - dominated by precision requirements
  • Memory usage: Scales linearly from 3.0KB (10 digits) to 7.0KB (100 digits)
  • Scalability: Predictable memory growth with precision level

Verified Performance Characteristics

  • Linear precision scaling: 10 digits → 100 digits shows 2.7x performance scaling
  • Logarithmic input scaling: Large integers show minimal performance impact
  • Linear memory scaling: Memory usage grows predictably with precision (3.0KB → 7.0KB)
  • Scientific notation efficiency: 7.6-25.6% overhead depending on exponent magnitude

License

MIT License. See LICENSE for details.

Technical Specifications

  • Python: 3.8+
  • Dependencies: None (runtime)
  • Precision: 1-100 fractional digits
  • Supported bases: 2 (binary), 8 (octal), 10 (decimal), 16 (hexadecimal)
  • Performance: O(log n + p) conversion time, O(p) memory scaling
  • Test Coverage: 91% with 30 passing unit tests

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

floating_base_converter-1.0.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

floating_base_converter-1.0.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file floating_base_converter-1.0.0.tar.gz.

File metadata

  • Download URL: floating_base_converter-1.0.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for floating_base_converter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f2959e04cee7d97f1fc60ed224943a9f17af352210e008fcb19cfc65182f136a
MD5 d496324013db57b4bd66c4b13c7bc2b8
BLAKE2b-256 0ba25d3787c40929ff8ff26d6cce26d2bb9684ed6c6b963320e4a6dd80d6ee41

See more details on using hashes here.

File details

Details for the file floating_base_converter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for floating_base_converter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf2582da59509db6e5a5946870ec02981c24246a5e2a6dea4583cba51c317440
MD5 5406efa73d8aea1c89c22eb86bdaa5da
BLAKE2b-256 230f1d24577c367aeb1f46b0e01d7726752d3664dd4d669232481d312607377f

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