Skip to main content

A numeric type for Python storing floats in log space for increased precision, allowing positive and negative float computations (arithmetic and logical operations)

Project description

Class PyLogFloat

The PyLogFloat class transforms any float (positive or negative) in linear space into log_space and allows standard arithetic and logical operations in in linear space to be performed between two PyLogFloats.

Internally a float f is stored as follows:

  • the (natural) log of |f| is stored as in a numpy.float64 variable, p

  • the sign of f (‘-’, ‘+’ or ‘0’) are stored in a variable (-1, +1, 0) variable, sign

By storing the sign separately, both positive and negative floats can be stored in a pyLogFloat. Notice that a sign=0 corresponds to the case where f =0.

Arithmetic operators

The following arithmetic operators are implemented:

+=
-=
*=
/=
+
-
*
/

Multiplication and division in linear space simply becomes addition and subtraction in log space. To implement linear space addition, the standard approach applying the log1p function is used. A conditional use of log1p or exp1m is used for log value subtractions, following Mächler, M. Accurately Computing log(1 − exp(− |a|)) Assessed by the Rmpfr package Cran, The Comprehensive R Archive Network.

The internal sign variable is taken into account when performing arithmetic operations. For example, in a multiplication between PyLogFloats a and b, corresponding to a positive float and a negative float, respectively, the resulting PyLogFloat has:

result.p = a.p + b.p
result.sign = a.sign * b.sign

Other operators are treated correspondingly (addition and subtraction becomes somewhat more involved).

Logical operators

The following arithmetic operators are implemented:

<=
>=
<
>

In logical operations between PyLogfloats a and b, the internal sign variable is first compared (-1 < 0 < +1).

If the a.sign == b.sign:

  • a.sign == 0 <=> equality

  • a.sign > 0, return a.p [operator] b.p

  • a.sing <0, return !(a.p [operator] b.p)

Other functions

Power operators (lplf**p, where plf is a pylogfloat and p is a float or int) are implemented:

**= (equiv ipow(self, p))
** (equiv pow(self, p))

Logarithms and exponents of PyLogFloat are implemented, both as inplace (e.g., ilog) and standard that is returning the result of the operation (e.g., log):

ilog(self)
log(self
iexp(self)
exp(self)

All functions and operators, except the power operator, are implemented to only work with PyLogFloats. The power operator always has a PyLogFloat base variable and a float or int power variable.

PLEASE NOTE THAT THIS IS STILL WORK IN PROGRESS

While a rudimentary test file is included, the class is still largely untested. Further functions may or may not be implemeted, e.g., a factorial function may be of interest. As could, in certain instances, automatic conversion of arguments to PyLogFloats.

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

pylogfloat-0.1.4.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

pylogfloat-0.1.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pylogfloat-0.1.4.tar.gz.

File metadata

  • Download URL: pylogfloat-0.1.4.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for pylogfloat-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8785b280fd93d9e4f3c831a005349cb6354384db187b8c3991015ae6e5f95c7e
MD5 862742543e1ab1e6ab5c6f75b0262157
BLAKE2b-256 b5d0e5443f7193e63d0d71d53d0726d63d252cca3dadf40c8eb2da63651ba7fc

See more details on using hashes here.

File details

Details for the file pylogfloat-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pylogfloat-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for pylogfloat-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d519a30c04dd81c652fcf8937bf8be205a62d79289122533698ada216d1711f4
MD5 5121400c0162c2d671134a3f36f0aad8
BLAKE2b-256 3201f2a9bd97b51cf601dd34418bf759c14bfb32a9bb5fbf386c743096907cb6

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