All Projects → rwpenney → spfpm

rwpenney / spfpm

Licence: other
Package for performing fixed-point, arbitrary-precision arithmetic in Python.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to spfpm

FixedPointsArduino
A fixed point arithmetic library for Arduino
Stars: ✭ 68 (+11.48%)
Mutual labels:  arithmetic, fixed-point
BenchmarksPythonJuliaAndCo
Benchmark(s) of numerical programs with Python (and Scipy, Pythran, Numba), Julia and C++.
Stars: ✭ 19 (-68.85%)
Mutual labels:  numerical-methods
goff
goff (go finite field) is a unix-like tool that generates fast field arithmetic in Go.
Stars: ✭ 71 (+16.39%)
Mutual labels:  arithmetic
pyFEM
A modular Finite Element Method solver implemented in Python
Stars: ✭ 16 (-73.77%)
Mutual labels:  numerical-methods
ProbabilityBoundsAnalysis.jl
Probability bounds analysis in Julia
Stars: ✭ 17 (-72.13%)
Mutual labels:  arithmetic
NumQM Basic
A mini-course offered to Undergrad physics students
Stars: ✭ 35 (-42.62%)
Mutual labels:  numerical-methods
evoapproxlib
Library of approximate arithmetic circuits
Stars: ✭ 23 (-62.3%)
Mutual labels:  arithmetic
30secondchallenge
Inspired by the newspaper puzzle my wife's grandma tests me with each time I visit.
Stars: ✭ 19 (-68.85%)
Mutual labels:  arithmetic
NAGPythonExamples
Examples and demos showing how to call functions from the NAG Library for Python
Stars: ✭ 46 (-24.59%)
Mutual labels:  numerical-methods
fpbinary
Fixed point package for Python.
Stars: ✭ 30 (-50.82%)
Mutual labels:  fixed-point
numerics
library of numerical methods using Armadillo
Stars: ✭ 17 (-72.13%)
Mutual labels:  numerical-methods
Surge
A Swift library that uses the Accelerate framework to provide high-performance functions for matrix math, digital signal processing, and image manipulation.
Stars: ✭ 4,945 (+8006.56%)
Mutual labels:  arithmetic
PyCannyEdge
Educational Python implementation of the Canny Edge Detector
Stars: ✭ 31 (-49.18%)
Mutual labels:  numerical-methods
imath
Arbitrary precision integer and rational arithmetic library
Stars: ✭ 92 (+50.82%)
Mutual labels:  arithmetic
FinEtools.jl
Finite Element tools in Julia
Stars: ✭ 126 (+106.56%)
Mutual labels:  numerical-methods
js-big-decimal
Work with large numbers on the client side with high precision.
Stars: ✭ 41 (-32.79%)
Mutual labels:  arithmetic
ComputationalMathematics
Lecture slides and homework assignments for MA5233 Computational Mathematics at the National University of Singapore.
Stars: ✭ 35 (-42.62%)
Mutual labels:  numerical-methods
verrou
floating-point errors checker
Stars: ✭ 39 (-36.07%)
Mutual labels:  arithmetic
AsFem
A Simple Finite Element Method program (AsFem)
Stars: ✭ 108 (+77.05%)
Mutual labels:  numerical-methods
herbie
Optimize floating-point expressions for accuracy
Stars: ✭ 614 (+906.56%)
Mutual labels:  numerical-methods

Simple Python Fixed-Point Module (SPFPM)

spfpm is a pure-Python toolkit for performing binary fixed-point arithmetic, including trigonometric and exponential functions.

The package provides:

  • Representations of values with a fixed number of fractional bits
  • Optional constraints on the number of whole-number bits
  • Interconversion between native Python types and fixed-point objects
  • Arithmetic operations (addition, subtraction, multiplication, division) of fixed-point numbers
  • Methods for computing powers, logarithms and exponents
  • Methods for computing trigonometric functions and their inverses
  • Computation of various mathematical constants, such as pi and log(2), to maximal precision for the chosen fixed-point resolution
  • Printing fixed-point numbers as decimal numbers, or as binary/octal/hexadecimal representations.
  • Support for numbers with thousands of bits of resolution

On a modern desktop PC, spfpm is typically capable of hundreds of thousands of arithmetic operations per second, i.e. over 100 kilo-FLOPS, even for a few hundred bits of resolution. As a pure-Python library SPFPM offers portability and interactivity, but will never compete with the performance of lower-level libraries such as mpmath, Boost multiprecision or GMP for heavyweight calculations.

Development currently targets Python versions 3.3 and later, although the library may also be usable with python-2.7. The latest version of spfpm can be found on GitHub.

Examples

After installation there are two main classes that you need from the FixedPoint module:

from FixedPoint import FXfamily, FXnum

you can create fixed-point numbers with the default (64-bit) resolution as follows:

x = FXnum(22) / FXnum(7)
y = FXnum(3.1415)
print(x - y)

Creating numbers with a specific precision requires use of the FXfamily class:

fam100 = FXfamily(100)
z = FXnum(1, fam100)
z2 = fam100(2)

One can then apply various computations such as:

print(z.atan() * 4)
print(z2.sqrt())

The FXfamily class also provides access to pre-computed constants which should be accurate to about 1/2 of the least significant bit (LSB):

print('pi = ', fam100.pi)
print('log2 = ', fam100.log2)
print('sqrt2 = ', fam100.sqrt2)

This produces the following printed values of those constants:

  • 3.141592653589793238462643383279
  • 0.69314718055994530941723212145 7
  • 1.414213562373095048801688724209

The struck-through digits show where the values computed at 100-bit precision differ from the true digits in the decimal representations of these constants. Alternatively, one could print these values in base-16:

print(FXfamily(400).pi.toBinaryString(logBase=4))

giving a hexadecimal value of Pi as:

  • 3.243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c89452821e638d01377be5466cf34e90c6cc0ac

which agrees exactly with the accepted result.

Licensing

All files are released under the Python PSF License and are Copyright 2006-2022 RW Penney.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].