# RuntimeWarning: invalid value encountered in divide

| | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

I have to make a program using Euler"s method for the "ball in a spring" model

``````from pylab import*
from math import*
m=0.1
Lo=1
tt=30
k=200
t=20
g=9.81
dt=0.01
n=int((ceil(t/dt)))
km=k/m
r0=[-5,5*sqrt(3)]
v0=[-5,5*sqrt(3)]
a=zeros((n,2))
r=zeros((n,2))
v=zeros((n,2))
t=zeros((n,2))
r[1,:]=r0
v[1,:]=v0
for i in range(n-1):
rr=dot(r[i,:],r[i,:])**0.5
a=-g+km*cos(tt)*(rr-L0)*r[i,:]/rr
v[i+1,:]=v[i,:]+a*dt
r[i+1,:]=r[i,:]+v[i+1,:]*dt
t[i+1]=t[i]+dt

#print norm(r[i,:])

plot(r[:,0],r[:,1])
xlim(-100,100)
ylim(-100,100)
xlabel("x [m]")
ylabel("y [m]")

show()
``````

I keep getting this error:

``````a=-g+km*cos(tt)*(rr-L0)*r[i,:]/rr
RuntimeWarning: invalid value encountered in divide
``````

I can"t figure it out, what is wrong with the code?

👻 Read also: what is the best laptop for engineering students?

## RuntimeWarning: invalid value encountered in divide __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

``````import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.
``````

Here is another example where something is run approximately once a minute:

``````import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
``````

2973

Answer #2

You can use the `sleep()` function in the `time` module. It can take a float argument for sub-second resolution.

``````from time import sleep
sleep(0.1) # Time in seconds
``````

## RuntimeWarning: invalid value encountered in divide __del__: Questions

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1

`Path` objects from the Python 3.4+ `pathlib` module also expose these instance methods:

Is there a ceiling equivalent of // operator in Python?

1 answers

I found out about the `//` operator in Python which in Python 3 does division with floor.

Is there an operator which divides with ceil instead? (I know about the `/` operator which in Python 3 does floating point division.)

165

Answer #1

You can just do upside-down floor division:

``````def ceildiv(a, b):
return -(-a // b)
``````

This works because Python"s division operator does floor division (unlike in C, where integer division truncates the fractional part).

This also works with Python"s big integers, because there"s no (lossy) floating-point conversion.

Here"s a demonstration:

``````>>> from __future__ import division   # a/b is float division
>>> from math import ceil
>>> b = 3
>>> for a in range(-7, 8):
...     print(["%d/%d" % (a, b), int(ceil(a / b)), -(-a // b)])
...
["-7/3", -2, -2]
["-6/3", -2, -2]
["-5/3", -1, -1]
["-4/3", -1, -1]
["-3/3", -1, -1]
["-2/3", 0, 0]
["-1/3", 0, 0]
["0/3", 0, 0]
["1/3", 1, 1]
["2/3", 1, 1]
["3/3", 1, 1]
["4/3", 2, 2]
["5/3", 2, 2]
["6/3", 2, 2]
["7/3", 3, 3]
``````

We hope this article has helped you to resolve the problem. Apart from RuntimeWarning: invalid value encountered in divide, check other __del__-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Javier Innsbruck

London | 2023-03-24

Maybe there are another answers? What RuntimeWarning: invalid value encountered in divide exactly means?. I just hope that will not emerge anymore

Boris Galleotti

Rome | 2023-03-24

I was preparing for my coding interview, thanks for clarifying this - RuntimeWarning: invalid value encountered in divide in Python is not the simplest one. I am just not quite sure it is the best method

Carlo OConnell

Tallinn | 2023-03-24

I was preparing for my coding interview, thanks for clarifying this - RuntimeWarning: invalid value encountered in divide in Python is not the simplest one. Checked yesterday, it works!

## Shop

Learn programming in R: courses

\$FREE

Best Python online courses for 2022

\$FREE

Best laptop for Fortnite

\$399+

Best laptop for Excel

\$

Best laptop for Solidworks

\$399+

Best laptop for Roblox

\$399+

Best computer for crypto mining

\$499+

Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically