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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Solving the error runtimewarning: invalid value encountered in double_scalars
Next
Answered
Alice Antoine
  • 23
Alice Antoine
Asked: May 11, 20222022-05-11T18:35:28+00:00 2022-05-11T18:35:28+00:00In: Programs

Solving the error runtimewarning: invalid value encountered in double_scalars

  • 23

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:

import numpy
 
 d = numpy.array([[1089, 1093]])
 e = numpy.array([[1000, 4443]])
 answer = numpy.exp(-3 * d)
 answer1 = numpy.exp(-3 * e)
 res = answer.sum()/answer1.sum()
 print res

but in my program, I am getting the warning:

nan
 C:\Users\Desktop\test.py:16: RuntimeWarning: invalid value encountered in double_scalars
  res = answer.sum()/answer1.sum()

Can someone explain to me why the issue of the runtimewarning: invalid value encountered in double_scalars happened? Where have I gone wrong? Thank you!

runtimewarning
  • 2 2 Answers
  • 185 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    adminxy Expert
    2022-05-23T03:27:45+00:00Added an answer on May 23, 2022 at 3:27 am

    This is how I solve this problem using math manipulation. That is for the numerator.

    exp(-x)+exp(-y) = exp(log(exp(-x)+exp(-y)))
                    = exp(log(exp(-x)*[1+exp(-y+x)]))
                    = exp(log(exp(-x) + log(1+exp(-y+x)))
                    = exp(-x + log(1+exp(-y+x)))

    Where x=3* 1089 and y=3* 1093 are respectively. The argument for this exponential is

    -x + log(1+exp(-y+x)) = -x + 6.1441934777474324e-06

    You could also use the same procedure for the denominator, but log(1+exp(-z+k)) has already been rounded up to 0. This means that the argument to the exponential function at denominator can be simply rounded up to -z=-3000. Now you have your result.

    exp(-x + log(1+exp(-y+x)))/exp(-z) = exp(-x+z+log(1+exp(-y+x)) 
                                       = exp(-266.99999385580668)

    This is very close to what you would get if only the two leading terms were kept (i.e. The numerator’s first number 1089 and the denominator’s first number 1000 are the 1089 codes.

    exp(3*(1089-1000))=exp(-267)

    Let’s take a look at how close we are to the solution to Wolfram alpha ( Link).

    Log[(exp[-3*1089]+exp[-3*1093])/([exp[-3*1000]+exp[-3*4443])] -> -266.999993855806522267194565420933791813296828742310997510523

    This difference is +1.7053025658242404e-13. The approximation that we made at denominator was therefore fine.

    The final result is

    'exp(-266.99999385580668) = 1.1050349147204485e-116

    From Wolfram Alpha is ( Link).

    1.105034914720621496.. × 10^-116 # Wolfram alpha.

    It is also safe to use numpy.

    • 17
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. ferneparker
    2022-07-12T06:20:21+00:00Added an answer on July 12, 2022 at 6:20 am

    You can solve your problem now by:

    • You can choose a library that specializes in high-precision mathematics such as mpmath. This is less fun.
    • As an alternative to a larger weapon, you can do math manipulation as described below.
    • You can get a scipy/numpy function that is tailored to your needs
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.