Big float in python

bigfloat 0.4.0

Arbitrary-precision correctly-rounded floating-point arithmetic, via MPFR.

Ссылки проекта

Статистика

Метаданные

Лицензия: GNU Lesser General Public License v3 (LGPLv3) (GNU Library or Lesser General Public License (LGPL))

Сопровождающие

Классификаторы

  • Development Status
    • 4 — Beta
    • Developers
    • Education
    • Science/Research
    • OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
    • OS Independent
    • Python :: 2
    • Python :: 2.7
    • Python :: 3
    • Python :: 3.5
    • Python :: 3.6
    • Python :: 3.7
    • Python :: 3.8
    • Python :: Implementation :: CPython
    • Scientific/Engineering :: Mathematics

    Описание проекта

    The bigfloat package is a Python package providing arbitrary-precision correctly-rounded binary floating-point arithmetic. It is implemented as a Cython wrapper around the GNU MPFR library. A couple of lines of Python code should give the idea:

    >>> from bigfloat import * >>> with precision(200) + RoundTowardZero: . print(sqrt(2)) . 1.4142135623730950488016887242096980785696718753769480731766796 >>> with quadruple_precision: . const_pi() . BigFloat.exact('3.14159265358979323846264338327950280', precision=113)

    Features

    • Supports Python 2 (version 2.7) and Python 3 (version 3.5 or later).
    • Exactly reproducible correctly-rounded results across platforms; precisely-defined semantics compatible with the IEEE 754-2008 standard.
    • Support for mixed-type operations with Python integers and floats.
    • Support for emulating IEEE 754 arithmetic in any of the IEEE binary interchange formats described in IEEE 754-2008. Infinities, NaNs, signed zeros, and subnormals are all supported.
    • Easy control of rounding modes and precisions via Context objects and Python’s with statement.

    Documentation

    Full package documentation is hosted at Read the Docs. Read on for a quick tour.

    A quick tour

    The bigfloat package is small and simple to use. Here’s a quick tour of some of its features.

    For demonstration purposes, start with:

    Note that this import shadows some builtin Python functions, namely abs , max , min , pow , round and (on Python 2 only) cmp . In normal usage you’ll probably only want to import the classes and functions that you actually need.

    The main class is the BigFloat class:

    >>> BigFloat(1) # can be constructed from an integer, float or string BigFloat.exact('1.0000000000000000', precision=53) >>> BigFloat('3.14159') ** 2 / 6.0 # can combine with ints and floats BigFloat.exact('1.6449312880166664', precision=53) >>> BigFloat('0.1', precision(200)) # high-precision value from string BigFloat.exact('0.1000000000000000000000000000000000000000000000000000 0000000002', precision=200)

    Newly-created BigFloat instances refer to the current context to determine what precision and rounding modes to use. This current context is represented by a Context instance, and can be retrieved by calling getcontext :

    >>> getcontext() Context(precision=53, emax=1073741823, emin=-1073741823, subnormalize=False, rounding=ROUND_TIES_TO_EVEN)

    The precision(200) argument passed to the BigFloat constructor above is also an example of a Context :

    >>> precision(200) Context(precision=200)

    The context used for a calculation can be set using the setcontext function, but a better way to make a temporary change to the context is to use Python’s with statement:

    >>> with precision(1000): . print sqrt(2) . 1.41421356237309504880168872420969807856967187537694807317667973 7990732478462107038850387534327641572735013846230912297024924836 0558507372126441214970999358314132226659275055927557999505011527 8206057147010955997160597027453459686201472851741864088919860955 232923048430871432145083976260362799525140798964

    Here, sqrt is one of a number of mathematical functions that the bigfloat package exports. As you can see, these functions operate on integers and floats as well as BigFloat instances, but always return a BigFloat instance.

    Rounding modes can be controlled similarly. Here are upper and lower bounds for π, accurate to 53 significant bits:

    >>> with RoundTowardPositive: . const_pi() . BigFloat.exact('3.1415926535897936', precision=53) >>> with RoundTowardNegative: . const_pi() . BigFloat.exact('3.1415926535897931', precision=53)

    And as you’d expect, with statements like those above can be nested. Context objects can also be combined using addition:

    >>> with RoundTowardPositive + precision(24): . BigFloat(1) / 3 . BigFloat.exact('0.333333343', precision=24)

    Various Context objects corresponding to IEEE 754 interchange formats are predefined:

    >>> quadruple_precision Context(precision=113, emax=16384, emin=-16493, subnormalize=True) >>> half_precision Context(precision=11, emax=16, emin=-23, subnormalize=True) >>> with half_precision: log(2) . BigFloat.exact('0.69336', precision=11)

    Installation

    The bigfloat package is available on the Python package index, and can be installed in the usual way using easy_install or pip . Alternatively, the development sources may be downloaded from the project’s homepage on GitHub.

    For more comprehensive installation instructions, please see the full documentation.

    Feedback

    Feedback is welcome! Please use the GitHub issue tracker to report issues. Alternatively, you can contact Mark Dickinson directly at dickinsm @ gmail . com with suggestions, complaints, bug reports, etc.

    License

    The bigfloat package is copyright (C) 2009–2019 Mark Dickinson

    The bigfloat package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

    The bigfloat package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License along with the bigfloat package. If not, see .

    Источник

    The bigfloat package — high precision floating-point arithmetic¶

    The bigfloat package is a Python wrapper for the GNU MPFR library for arbitrary-precision floating-point reliable arithmetic. The MPFR library is a well-known portable C library for arbitrary-precision arithmetic on floating-point numbers. It provides precise control over precisions and rounding modes and gives correctly-rounded reproducible platform-independent results.

    The bigfloat package aims to provide a convenient and friendly Python interface to the operations and functions provided by the MPFR library. The main class, BigFloat , gives an immutable multiple-precision floating-point type that can be freely mixed with Python integers and floats. The Context class, when used in conjunction with Python’s with statement, gives a simple way of controlling precisions and rounding modes. Additional module-level functions provide various standard mathematical operations. There is full support for IEEE 754 signed zeros, nans, infinities and subnormals.

    Features¶

    • Supports Python 2 (version 2.6 or later) and Python 3 (version 3.2 or later).
    • Exactly reproducible correctly-rounded results across platforms; precisely-defined semantics compatible with the IEEE 754-2008 standard.
    • Support for mixed-type operations with Python integers and floats.
    • Support for emulating IEEE 754 arithmetic in any of the IEEE binary interchange formats described in IEEE 754-2008. Infinities, NaNs, signed zeros, and subnormals are all supported.
    • Easy control of rounding modes and precisions via Context objects and Python’s with statement.

    Introduction¶

    >>> from bigfloat import * >>> sqrt(2, precision(100)) # compute sqrt(2) with 100 bits of precision BigFloat.exact('1.4142135623730950488016887242092', precision=100) >>> with precision(100): # another way to get the same result . sqrt(2) . BigFloat.exact('1.4142135623730950488016887242092', precision=100) >>> my_context = precision(100) + RoundTowardPositive >>> my_context Context(precision=100, rounding='RoundTowardPositive') >>> sqrt(2, my_context) # and another, this time rounding up BigFloat.exact('1.4142135623730950488016887242108', precision=100) >>> with RoundTowardNegative: # a lower bound for zeta(2) . sum(1/sqr(n) for n in range(1, 10000)) . BigFloat.exact('1.6448340618469506', precision=53) >>> zeta(2) # actual value, for comparison BigFloat.exact('1.6449340668482264', precision=53) >>> const_pi()**2/6.0 # double check value BigFloat.exact('1.6449340668482264', precision=53) >>> quadruple_precision # context implementing IEEE 754 binary128 format Context(precision=113, emax=16384, emin=-16493, subnormalize=True) >>> next_up(0, quadruple_precision) # smallest subnormal for binary128 BigFloat.exact('6.47517511943802511092443895822764655e-4966', precision=113) >>> log2(_) BigFloat.exact('-16494.000000000000', precision=53) 

    Installation¶

    Where to get it¶

    The latest released version of the bigfloat package can be downloaded from its place at the Python Package Index. Development sources can be checked out from the project’s GitHub page.

    Prerequisites¶

    In order to use the bigfloat package you will need to have both the GMP and MPFR libraries already installed on your system, along with the include files for those libraries. See the MPFR homepage and the GMP homepage for more information about these libraries. Currently, MPFR version 2.3.0 or later is required.

    The bigfloat package works with Python 2 (version 2.6 or later) or Python 3 (version 3.2 or later), using a single codebase for both Python dialects.

    Installation¶

    Like most third party Python libraries, the bigfloat package is installed by means of the setup.py script included in the distribution. On many systems, installation should be as simple as doing:

    in the top-level directory of the unpacked distribution. You may need superuser privileges to install the library, for example with:

    sudo python setup.py install

    The MPFR and GMP libraries will need to be installed on your system prior to installation of bigfloat , along with any necessary development header files. On Linux, look for a package called something like libmpfr-dev or mpfr-devel , along with correspondingly named packages for GMP. If the libraries and/or include files are installed in an unusual place, it may be necessary to specify their location using environment variables on the command line. As an example, on my OS X 10.9 system, with MPFR and GMP installed in /opt/local/, I need to do:

    LIBRARY_PATH=/opt/local/lib CPATH=/opt/local/include python setup.py install

    Similarly, if installing from the Python package index using easy_install or pip , you may also need to add the necessary environment variables first.

    Detailed Documentation¶

Оцените статью