Lossless Number Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Spencer

    #1

    Lossless Number Conversion

    Is there any library for Python that implements a kind of universal
    number object. Something that, if you divide two integers, generates a
    ratio instead of a float, or if you take the square root of a negative,
    generates a complex number instead of raising an exception? Lisp has
    something like this, and it makes number crunching much more convenient.

    Chris
  • mensanator@aol.com

    #2
    Re: Lossless Number Conversion


    Chris Spencer wrote:[color=blue]
    > Is there any library for Python that implements a kind of universal
    > number object. Something that, if you divide two integers, generates a
    > ratio instead of a float, or if you take the square root of a negative,
    > generates a complex number instead of raising an exception? Lisp has
    > something like this, and it makes number crunching much more convenient.
    >
    > Chris[/color]

    The GMPY module has unlimited precision rationals:
    [color=blue][color=green][color=darkred]
    >>> from gmpy import *[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> r = mpq(1,1) # create the rational 1/1
    >>> for i in range(2,50):[/color][/color][/color]
    r = r + mpq(1,i) # add the rational 1/i to the running sum
    print r


    3/2
    11/6
    25/12
    137/60
    49/20
    363/140
    761/280
    7129/2520
    7381/2520
    83711/27720
    86021/27720
    1145993/360360
    1171733/360360
    1195757/360360
    2436559/720720
    42142223/12252240
    14274301/4084080
    275295799/77597520
    55835135/15519504
    18858053/5173168
    19093197/5173168
    444316699/118982864
    1347822955/356948592
    34052522467/8923714800
    34395742267/8923714800
    312536252003/80313433200
    315404588903/80313433200
    9227046511387/2329089562800
    9304682830147/2329089562800
    290774257297357/72201776446800
    586061125622639/144403552893600

    So you can keep absolute precision all the way to the end
    of the calculation. And you can always convert it to a float
    by dividing the numerator by the denominator:
    [color=blue][color=green][color=darkred]
    >>> print mpf(r.numer())/mpf(r.denom())[/color][/color][/color]
    4.4792053383294 250575604717929 6

    Comment

    • Andreas Kostyrka

      #3
      Re: Lossless Number Conversion

      Am Sonntag, den 28.08.2005, 21:36 +0000 schrieb Chris Spencer:[color=blue]
      > Is there any library for Python that implements a kind of universal
      > number object. Something that, if you divide two integers, generates a
      > ratio instead of a float, or if you take the square root of a negative,
      > generates a complex number instead of raising an exception? Lisp has
      > something like this, and it makes number crunching much more convenient.[/color]

      Yes and no. There isn't general solution. But for example you want to
      allow complex number, just use cmath.

      Andreas

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.1 (GNU/Linux)

      iD8DBQBDErdeHJd udm4KnO0RAkF9AK DQNbpEdi+8UwJe3 O/CCTWBq2Mx+ACeIF Zn
      WxWP3ZgRG4vmPt7 zemJNLDw=
      =6NkG
      -----END PGP SIGNATURE-----

      Comment

      • Raymond L. Buvel

        #4
        Re: Lossless Number Conversion

        Chris Spencer wrote:[color=blue]
        > Is there any library for Python that implements a kind of universal
        > number object. Something that, if you divide two integers, generates a
        > ratio instead of a float, or if you take the square root of a negative,
        > generates a complex number instead of raising an exception? Lisp has
        > something like this, and it makes number crunching much more convenient.
        >
        > Chris[/color]

        If you are on any Unix-like platform, you might want to check out clnum.
        It works like the gmpy package but has complex versions of rationals
        and arbitrary precision floats.


        Comment

        Working...