-1

I've written a program that calculates the infinite sum series by the formula. I've used a range function to calculate it but I came with a problem that range doesn't work with floats.

TypeError: 'float' object cannot be interpreted as an integer

I've tried to use numpy.arrange but it helps only when you want to use step arrange(start, stop, STEP).

import math
import numpy

x = float(input('x: '))
n = float(input('n: '))
s = 0
  while x >= 0.1:
    for i in range(1, n + 1):
      s = s + (math.pow(-1, i) * math.pow(x, 4 * i + 3)) / (numpy.factorial(2 * i + 1) * (4 * i + 3))

if x >= 1:
    break
print(s)

UPD: this how my program looks like now:` import math import numpy

 x = float(input('x: '))
 n = int(input('n: '))
 s = 0
 while x >= 0.1:
  for i in range(1, n + 1):
    s = s + (math.pow(-1, i) * math.pow(x, 4 * i + 3)) / (math.factorial(2 * i + 1) * (4 * i + 3))

if x >= 1:
    break
print(s)

` And such output:

W:\Labaratorni\Python\lab2_4\venv\Scripts\python.exe 
W:/Labaratorni/Python/lab2_4/main.py
x: 0.3
n: 3

No errors, it just stops.

  • 2
    You can't use a float as an argument to `range`. Also, fix your indentation. – alani Oct 04 '20 at 12:06
  • 1
    But `n = int(input('n: '))` should fix the problem. – alani Oct 04 '20 at 12:07
  • So if i want to calculate this i need to use something else instead of range? – Kristina Toronenko Oct 04 '20 at 12:07
  • @alani yes, but i need to use floats, it is a task – Kristina Toronenko Oct 04 '20 at 12:08
  • You can use an integer loop variable and then use it to calculate another variable which is floating point (e.g. `for i in range(....):` `f = start + i * step`, do stuff with `f`). Or you could use a `while` loop. – alani Oct 04 '20 at 12:10
  • 1
    it isn't clear what you want, exactly. What would `range(1, some_float + 1)` do? e.g `range(1, 3.5)`? Probably, you can just use a while-loop. – juanpa.arrivillaga Oct 04 '20 at 12:11
  • Your formula uses `factorial` (it's `numpy.math.factorial` btw), which only takes integers as argument, so there is no way your calculation would make sense if `i` is a float. `i` and `n` have to be integers. – Thierry Lathuille Oct 04 '20 at 12:14
  • @ThierryLathuille x is a float, n is an integer, there are no problems with i or n – Kristina Toronenko Oct 04 '20 at 12:15
  • @juanpa.arrivillaga I need to calculate this formula using n as integers and x is a float, but due to this x my program crushes – Kristina Toronenko Oct 04 '20 at 12:16
  • You didn't provide the complete error traceback, which you should absolutely do, so we can only guess which line caused the error. It looks like it is `range(1, n+1)`, as this is exactly the error that is raised if you pass it a float, which is the case in your code. So, again, `n` must be an int, which it currently isn't, because you defined it as a float in `n = float(input('n: '))` – Thierry Lathuille Oct 04 '20 at 12:19
  • I deleted numpy and used math liblary insted of numpy for factorial but when I input x as a float, the program just stops calculating (without crash) – Kristina Toronenko Oct 04 '20 at 12:23
  • The code in your question contains `numpy.factorial`.... What you have to do is: provide us with an **exact** copy of the code that raises the error, the **complete** error traceback, and sample input/expected output. Without that, your question just can't be answered. – Thierry Lathuille Oct 04 '20 at 12:26
  • This is now a completely different question - Please don't move goals this way, this is not how SO works. Your script doesn't stop, it just runs without end because you never update `x` inside your `while` loop. – Thierry Lathuille Oct 04 '20 at 12:37

1 Answers1

0

I believe that this should do what you want:

import math

x = float(input('x: '))
n = int(input('n: '))
s = 0
if 0.1 <= x <= 1:
    for i in range(1, n + 1):
        s += ((-1)**i * x**(4 * i + 3)) / (math.factorial(2*i + 1) * (4*i + 3))
        
print(s)

Key note is that the range function must take in integers, so you must convert n into an int.

I think that you've used a while loop where an if statement should have been used. Let us know if we've misinterpreted your objective, it may help to explicitly say what the summation is supposed to be.

Cameron Chandler
  • 407
  • 3
  • 10