T
T
trickster20192021-11-21 14:42:45
Python
trickster2019, 2021-11-21 14:42:45

How to do a for 0.1 loop step in a Python loop?

Good afternoon. There is a mathematical task "Output the values ​​of the function f (x) on the interval [n-3; 1] with a step of 0.1", I wrote the cycle and the formula itself, but when I tried to set the step 0.1, I got an error stating that the floating figures are accurately transmitted as argument is impossible, in consequence of which the question is how to get around this error?

import math
n=12

for i in range(n-3, n+1, 0.1):
    a=(math.cos(i))/(2*math.tan(i))
    b1=(6-i)
    b2=math.exp(b1)
    ab=a+b2
    c=math.log(5*i-2)
    print("------------------------")
    print(str(i) + " | " + str(a))
    print("------------------------")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-11-21
@trickster2019

Try like this:

import numpy as np
import math
n=12

for i in np.arange(n-3, n+1, 0.1):
    a=(math.cos(round(i, 2)))/(2*math.tan(round(i, 2)))
    b1=(6-round(i, 2))
    b2=math.exp(b1)
    ab=a+b2
    c=math.log(5*round(i, 2)-2)
    print("------------------------")
    print(str(round(i, 2)) + " | " + str(a))
    print("------------------------")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question