Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:51:59+00:00 2026-05-13T09:51:59+00:00

Is there a pure python implementation of fractions.Fraction that supports long s as numerator

  • 0

Is there a pure python implementation of fractions.Fraction that supports longs as numerator and denominator? Unfortunately, exponentiation appears to be coded in to return a float (ack!!!), which should at least support using decimal.Decimal.

If there isn’t, I suppose I can probably make a copy of the library and try to replace occurrences of float() with something appropriate from Decimal but I’d rather something that’s been tested by others before.

Here’s a code example:

base = Fraction.from_decimal(Decimal(1).exp())
a = Fraction(69885L, 53L)
x = Fraction(9L, 10L)

print base**(-a*x), type(base**(-a*x))

results in 0.0 <type 'float'> where the answer should be a really small decimal.

Update: I’ve got the following work-around for now (assuming, for a**b, that both are fractions; of course, I’ll need another function when exp_ is a float or is itself a Decimal):

def fracpow(base, exp_):
    base = Decimal(base.numerator)/Decimal(base.denominator)
    exp_ = Decimal(exp_.numerator)/Decimal(exp_.denominator)

    return base**exp_

which gives the answer 4.08569925773896097019795484811E-516.

I’d still be interested if there’s a better way of doing this without the extra functions (I’m guessing if I work with the Fraction class enough, I’ll find other floats working their way into my results).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:51:59+00:00Added an answer on May 13, 2026 at 9:51 am

    “Raise to a power” is not a closed operation over the rationals (differently from the usual four arithmetic operations): there is no rational number r such that r == 2 ** 0.5. Legend has it that Pythagoras (from whose theorem this fact so simply follows) had his disciple Hippasus killed for the horrible crime of proving this; looks like you sympathize wit Pythagoras’ alleged reaction;-), given your weird use of “should”.

    Python’s fractions are meant to be exact, so inevitably there are case in which raising a fraction to another fraction’s power will be absolutely unable to return a fraction as its result; and “should” just cannot be sensibly applied to a mathematical impossibility.

    So the best you can do is to approximate your desired result, e.g. by getting a result that’s not an exact fraction (floats are generally considered sufficient for the purpose) and then further approximating it back with a fraction. Most existing pure-Python implementations (there are many rationals.py files found around the net;-) prefer not to implement a ** operator at all, but of course there’s nothing stopping you from making a different design decision in your own implementation!-)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is cgi.escape but that appears to be implemented in pure python. It seems
I found a simple pure python blowfish implementation that meets my needs for a
Is there implementation of Git in pure Python?
Is there an XSLT library that is pure Python? Installing libxml2+libxslt or any similar
The reason? Pure nostalgia. Anyway, there was a standard for Basic that was published
I'm looking for a Ruby or Python implementation of the Git client that can
I was wondering if there is a python cognate to PHP's crypt() function that
There seem to be a few good pure Python SSH2 client implementations out there,
Are there any implementations of SSH in pure python? Preferably open source? I'm specifically
Are there any JavaScript (ECMAScript) implementations written in pure Python? It is okay even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.