Home>

### divide by 0 in python

Error message

I get an error like the following, and it is said that it has been divided by zero, but I am in trouble because I do not know where it is dividing by zero.

``````Traceback (most recent call last):
File "/Users/matsumura/PycharmProjects/wave/pinknoise_generater.py", line 29, in<module>
a, b = synth_pinknoise (duration = 10.0, samprate = 48000)
File "/Users/matsumura/PycharmProjects/wave/pinknoise_generater.py", line 17, in synth_pinknoise
t = np.arange (start = 0, stop = duration, step = (int (1/samprate)))
ZeroDivisionError: float division by zero``````
Applicable source code
``````# coding: UTF-8
# Required libraries
import math
import numpy as np
import matplotlib
matplotlib.use ('TkAgg')
import matplotlib.pyplot as plt
# Define pink noise function
def synth_pinknoise (duration, samprate):
"" "
Generate pinknoise using Voss algorithm
http://www.firstpr.com.au/dsp/pink-noise/
"" "
f_low = 10 # lowest frequency to keep pink (Hz)
levels = math.ceil (np.log2 (samprate/f_low))
t = np.arange (start = 0, stop = duration, step = 1/samprate)
out = np.zeros (len (t))
x = np.random.normal (size = levels)
for n in range (math.ceil (samprate * duration)):
for m in range (levels):
if n% 2 ** (m + 1) == 0:
x [m] = np.random.normal ()
out [n] = np.random.normal () + np.sum (x)
return (out/max (abs (out)), t)

# Usage (Return values ​​are x: Pink noise signal, t: Time of each sample)
a, b = synth_pinknoise (duration = 10.0, samprate = 48000)
plt.plot (a, b)
plt.show ()``````

``````Traceback (most recent call last):
File "/Users/matsumura/PycharmProjects/wave/pinknoise_generater.py", line 29, in
a, b = synth_pinknoise (duration = 10.0, samprate = 48000)
File "/Users/matsumura/PycharmProjects/wave/pinknoise_generater.py", line 17, in synth_pinknoise
t = np.arange (start = 0, stop = duration, step = (int (1/samprate)))
ZeroDivisionError: float division by zero``````

`step = (int (1/samprate))`will give 0 to`step`, so inside`np.arange`Will cause a division by zero error. There is an internal calculation that divides by`step`(you can see it immediately when you imagine how to produce an arange result).