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 8453207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:42:34+00:00 2026-06-10T11:42:34+00:00

I was checking out doctest and copied the factorial example to my editor. Since

  • 0

I was checking out doctest and copied the factorial example to my editor. Since using recursion felt more functional programming, I felt like changing the example like this;

def factorial(n):
    # ... omitted
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")

    if n == 0:
        return 1
    else:
        return factorial(n  - 1) * n

After this change, one of the tests failed;

Failed example:
    factorial(30.0)
Expected:
    265252859812191058636308480000000L
Got:
    2.6525285981219103e+32

What is the reason of this difference?

  • 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-06-10T11:42:35+00:00Added an answer on June 10, 2026 at 11:42 am

    Try running with factorial(30) instead of factorial(30.0). floating point addition isn’t exact like integer addition, so you’ll start seeing errors after a while.

    Consider:

    >>> 1e20 + 1 == 1e20 #True
    

    This is because you don’t have enough precision (bits) to store both of those numbers uniquely. (a typical python float has 64 bits meaning you have 2**64 unique combinations — somewhere around 1.84e19 options. However, the maximum size for a python float is sys.float_info.max which is about 1.8e308 on most systems, so there is no way to store each integral value uniquely — especially when you consider that floats can hold a lot more than just integer values)

    Personally, I would just cast the value of n to an integer inside your factorial function since factorial is only defined for integers — possibly checking to make sure that abs(int(n) - n) < 1e-5 or something to that effect.

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

Sidebar

Related Questions

After checking out code for the first time from a repository into Eclipse using
I am checking out the OutputCacheProvider in ASP.NET 4.0 and using it to store
I was checking out clock() on cplusplus.com . Their example involves making the process
I was checking out the source of apples Video recording example and I came
I was checking out this example at tutsplus and this is what I found.
Using paypal_base.dll and Payflow_dotNET.dll Have the following setup: SetExpressCheckoutRequestType.SetExpressCheckoutRequestDetails.AllowNote = 1 When checking out
I'm using SVNANT for checking out my project directory on my Windows XP machine.
On checking out a very large svn repository I would like to throttle the
i'm checking out some newbie Google Maps API tutorial code. They have an example
Subversion 1.6.6 using the commend line client Importing and checking out project for the

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.