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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:56:54+00:00 2026-05-24T02:56:54+00:00

I’m doing some math with really big numbers (I’m using Python, but this question

  • 0

I’m doing some math with really big numbers (I’m using Python, but this question isn’t Python specific). For one value, I have a formula that gives me f(t) = Pr(X < t). I want to use this formula to get Pr(X >= t) = 1 - f(t). Because f(t) returns values very close to zero, I’ve been using a log transform and storing log( f(t) ) instead of f(t). My log( f(t) ) is on the order of -1e5 or so.

For multiplying, this works quite well. log( f(t) * g ) = log( f(t) ) + log(g).

But, it’s very difficult to compute log( 1 - f(t) ) using only log( f(t) ); I could, of course, temporarily exponentiate the value I store and compute log( 1 - exp( log( f(t) ) ), but that will return log( 1 - 0.0 ) = 0.0 because log( f(t) ) is so close to zero.

You might ask, “Why do you care? If it’s close to zero, then 1 minus it is very close to 1.” Well, that’s a good point you’ve made. You’re a smart cookie.

The trouble is I want to use this to rank values, so I really care if one is log(0.999) and the other is log(0.9999). You might also ask, “Well, why don’t you just rank the log( f(t) ) and then reverse the order to get the rankings for log( 1 - f(t) ).” Again, I cannot help but point out how great your questions are. It really has been a pleasure talking to you.

But here’s the problem: I don’t just want to rank by 1 - f(t); I actually want to rank based on Pr(X >= t) * g(t) = (1 - f(t)) g(t). After taking logs, I get log( 1 - f(t) ) + log( g(t) ); ranking based on f(t) alone won’t give the correct answer.

In the past I wrote a little Python function to compute log(a + b) from log(a) and log(b):

def log_add(logA,logB):
    if logA == log(0):
        return logB
    if logA<logB:
        return log_add(logB,logA)
    return log( 1 + math.exp(logB-logA) ) + logA

It helps by first normalizing them so that they’re close together and then exponentiating when they’re close together.

Unfortunately, I wasn’t able to get the same trick to work for my subtraction because there is no normalization factor that will bring log(1) and log( f(t) ) close together because they’re so far apart.

Does anyone know how to solve this? It seems like such a classic kind of problem; I’d really expect/hope/pray that there is a clever function that operates on the bit level that can give me log(1-x) from log(x). Also, if you know how it works, I’d really, really love to know.

Cheers!
Oliver

  • 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-24T02:56:55+00:00Added an answer on May 24, 2026 at 2:56 am

    If log(f(t)) is indeed -1e5 (or of similar order of magnitude) then 0.0 is the best floating point representation of log(1-f(t)). Indeed, f(t) = exp(-1e5) so, by the Taylor series that dmuir mentioned, log(1-f(t)) = -exp(-1e5) (this is actually not an exact equality, but it is an extremely good approximation). Now, -exp(-1e5) = -3.56e-43430, but there are no floating point numbers between 0 and -4e-324, so the best floating point representation is 0.0.

    So, what you want to do is impossible with standard floating point numbers.

    Does this matter? You say that want to rank based on Pr(X >= t) * g(t) = (1 - f(t)) g(t), which is equivalent to ranking by log( 1 - f(t) ) + log( g(t) ). We found above that log(1-f(t)) = -3.56e-43430, so this term is only going to make a difference if the different values of log(g(t)) differ by no more than this tiny number and if your computation is accurate enough that it can distinguish by these tiny numbers (if you use standard floating point numbers, then your computation will never be accurate enough). In other words, if log(f(t)) is indeed -1e5 or similar, than you can just rank by g(t).

    However, it may be that log(f(t)) is of the order of -1e5, but that it sometimes takes values closer to zero like -10 or -1. In that case, you cannot just ignore it and you have to indeed rank by log(1-f(t)) + log(g(t)). You should write this using the math.log1p function: rank by log1p(-f(t)) + log(g(t)). The reason is that if f(t) is close to zero, then log(1-f(t)) is inaccurate but log1p(-f(t)) is accurate. If f(t) is extremely close to zero, such as when log(f(t)) = -1e5, then log1p(-f(t)) = 0.0 because that is the best it can do using standard floating point numbers.

    I am using the expression “standard floating point numbers” for a reason. It is possible to use floating point numbers with more precision, and if you really want to capture tiny numbers like -3.56e-43430 that is what you should do. One possibility is in Python is mpmath (unfortunately, it does not seem to support the log1p function). Be warned that this is much slower than standard floating point numbers, and as I said I don’t think you’ll need it. However, it’s worth a go if you want to understand these issues better.

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

Sidebar

Related Questions

No related questions found

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.