Giter Club home page Giter Club logo

spfpm's Introduction

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
  • 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.

Development currently targets Python versions 3.2 and later, although the library should also work with python-2.7. The latest version of spfpm can be found on GitHub, with earlier versions also available on SourceForge.

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 at least 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 3
  • 0.69314718055994530941723212145 79
  • 1.414213562373095048801688724209 1

The struck-through digits show where the values computed at 100-bit precision differ from the true values 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-2018 RW Penney.

spfpm's People

Contributors

rwpenney avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.