I am trying to come up with a function that maps an integer X to the decimal 0.X without using a loop.
Examples:
I figured I could do something like this:
def mapToDec(x):
while x > 1:
x = x / 10
return x
However, I am looking for something more sophisticated that doesn't require a loop, something that would look like a mathematical function perhaps.
You have shown that there is such a function by implementing one. If you want one without a loop, how about
def mapToDec(x):
return x * 10 ** -(1 + math.floor(math.log10(x)))
x / 10 ** (1 + math.floor(math.log10(x)))
seems to work with better precisionThanks, that's pretty much what I was looking for.