I’m trying to convert an int to float. But I have to optimize my function even further.
It’s taking 3.3 seconds to run this function:
convertIntToFloat(100000000)
# 100000000.0
This is the function:
def convertIntToFloat(int):
returnValue = sum(range(int))/int*2+1
return returnValue
>Solution :
You can do something simple like this:
def convertIntToFloat(number):
return number + 0.0
Or this (although division is slower than addition):
def convertIntToFloat(number):
return number / 0.0