ubsuny / 23-Homework6G5

MIT License
0 stars 8 forks source link

Can I use nan_to_num to define a number that approaches zero, but is not exactly zero? #12

Open AhmedCode99 opened 10 months ago

WildJimmy commented 10 months ago

I answered this question here https://github.com/ubsuny/23-Homework6G1/issues/18

poojashresthacode commented 10 months ago

I have used this for preventing division by zero issues, and calculating the exponential only for non-zero elements.

def exp_minus_one_over_x(x): x = np.asarray(x, dtype=float) mask = x != 0 # Avoid division by zero result = np.zeros_like(x, dtype=float)

# Calculate the exponential only where x is not zero
result[mask] = np.exp(-1 / x[mask])

return result
poojashresthacode commented 10 months ago

still I have got these results. Simpson's rule result: 3.742912580939198e+214 Trapezoidal rule result: 2.8071844357043985e+214 Adaptive trapezoidal result: inf I am getting infinity for the adaptive trapezoidal result. What to do for removing it?