Q: RuntimeWarning: invalid value encountered in double_scalars in python . How to resolve this error I am trying to calculate implied volatility -
import pandas as pd
from scipy.stats import norm
from math import sqrt, exp, log, pi
import numpy as np
import scipy.stats as si

def d(sigma, S, K, r, t):
    d1 = 1 / (sigma * sqrt(t)) * (log(S/K) + (r + sigma**2/2) * t)

    d2 = d1 - sigma * sqrt(t)
    return d1, d2


def call_price(sigma, S, K, r, t, d1, d2):
    C = norm.cdf(d1) * S - norm.cdf(d2) * K * exp(-r * t)
    return C

#  S  = spot
#  K  = strike
#  C  = price of call as predicted by Black-Scholes model
#  r  = risk-free interest rate
#  t  = time to expiration expressed in years
#  C0 = price of call option from option chain


def impl_volit(S, K, r, t, C0):

    # S = 35450
    # K = 35500
    # r = 0.01
    # t = 6.0/365
    # C0 = 599.2

    #  We need a starting guess for the implied volatility.  We chose 0.5
    #  arbitrarily.
    vol = 0.5

    epsilon = 1.0  # Define variable to check stopping conditions
    abstol = 1e-4  # Stop calculation when abs(epsilon) < this number

    i = 0  # Variable to count number of iterations
    max_iter = 1e3  # Max number of iterations before aborting

    while epsilon > abstol:
        #  if-statement to avoid getting stuck in an infinite loop.
        if i > max_iter:
            break

        i = i + 1
        orig = vol
        d1, d2 = d(vol, S, K, r, t)
        function_value = call_price(vol, S, K, r, t, d1, d2) - C0
        vega = S * norm.pdf(d1) * sqrt(t)
        vol = -function_value/vega + vol

        epsilon = abs(function_value)
    return vol*100



print(impl_volit(36596.05, 30400, 3.81, 6, 6121.40))

I am getting below error:
c:\Users\Desktop\IV_Delta_calculator_cheke\conversion\impliedmoony.py:54: RuntimeWarning: divide by zero encountered in double_scalars
      vol = np.double(-function_value/vega + vol)
c:\Users\Desktop\IV_Delta_calculator_cheke\conversion\impliedmoony.py:8: RuntimeWarning: invalid value encountered in double_scalars
      d1 = np.double(1 / (sigma * sqrt(t)) * (log(S/K) + (r + sigma**2/2) * t))
    nan

Please anyone help me to resolve this..Thanks in advance
