👻 *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.

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).
```

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?

Answer #1

`os.remove()`

removes a file.`os.rmdir()`

removes an empty directory.`shutil.rmtree()`

deletes a directory and all its contents.

`Path`

objects from the Python 3.4+ `pathlib`

module also expose these instance methods:

`pathlib.Path.unlink()`

removes a file or symbolic link.`pathlib.Path.rmdir()`

removes an empty directory.

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.)

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:

- Italiano RuntimeWarning: invalid value encountered in divide
- Deutsch RuntimeWarning: invalid value encountered in divide
- Français RuntimeWarning: invalid value encountered in divide
- Español RuntimeWarning: invalid value encountered in divide
- Türk RuntimeWarning: invalid value encountered in divide
- Русский RuntimeWarning: invalid value encountered in divide
- Português RuntimeWarning: invalid value encountered in divide
- Polski RuntimeWarning: invalid value encountered in divide
- Nederlandse RuntimeWarning: invalid value encountered in divide
- 中文 RuntimeWarning: invalid value encountered in divide
- 한국어 RuntimeWarning: invalid value encountered in divide
- 日本語 RuntimeWarning: invalid value encountered in divide
- हिन्दी RuntimeWarning: invalid value encountered in divide

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

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

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!