-2

I am writing Python3 code which requires me to simpy remove the decimal. I tried the most simple thing I could think of

num = int(str(float(x)).replace('.', ''))

where x is generated using x = a*x*(1-x) where both a and x are float

but when the decimal number is representated in scientific notation it throws the following error:

    num = int(str(float(x)).replace('.', ''))
ValueError: invalid literal for int() with base 10: '4234481147613274e-05'

Though it works when it is not representated in scientific notation. What intrigued me was that when I used the same number in command line, it gave me the correct result

>>> x = 4234481147613274e-05
>>> x
42344811476.13274
>>> num = int(str(float(x)).replace('.', ''))
>>> num
4234481147613274

I check the following thread but it didn't help me (Convert scientific notation to decimals). Why is this happening?

EDIT: To be honest, I can't exactly write what I'm coding, as I'm not allowed to disclose it. But I'll still try to make it as clear as possible:

x = 0.55 and a is a number between 3.85681 and 4, and then I'm looping the value of x and finally the conversion:

a = 3.85681 + ((big_integer)%14319)*1e-5
for i in range(500): x = a*x*(1-x)
num = int(str(float(x)).replace('.', ''))

What I want is if x = 0.470912345, I want the num = 470912345. So I think the problem could be understandable why I didn't use x*10000 or something similar, because I don't want 0.452 to become 4520, but only 452.

sh.3.ll
  • 815
  • 7
  • 17
  • 3
    "Remove decimal point" is not a well-defined mathematical operation. – user2357112 Jul 24 '20 at 08:23
  • 4
    Why are you trying to do this in the first place? It's very likely that you should be doing something different, such as multiplying your numbers by a scale factor, or working with strings without involving floats at all. – user2357112 Jul 24 '20 at 08:27
  • 1
    What you're doing in the command line is a different thing. You really have `x = 4.234481147613274e-05` and then get the same result. – Matthew Strawbridge Jul 24 '20 at 08:30
  • Actually using the literal x = '4234481147613274e-05'; num = int(str(float(x)).replace('.', '')) works for me in a script (tried both an online repl and Jupyter notebook). – DarrylG Jul 24 '20 at 08:35
  • Okay, so you're doing something with the [logistic map](https://en.wikipedia.org/wiki/Logistic_map). Your computation is going to immediately start losing precision, and the number of digits needed to represent the "exact" values (which you're not computing) grow exponentially with the number of iterations. For your use case, are you really okay with getting some 15-digit integer when the true value would be an integer over 2^500 digits long? – user2357112 Jul 25 '20 at 00:05
  • Also, you don't seem to have a plan for distinguishing 0.05 and 0.5. Does the difference not matter for your use case? – user2357112 Jul 25 '20 at 00:07
  • @user2357112supportsMonica no it doesn't matter for both – sh.3.ll Jul 31 '20 at 05:42

1 Answers1

-1

float does not express decimal precisely.
for example, below code does not work, because x is 「4.234481147613274e-05」
(in my enviromnent)

>>> x = 4234481147613274e-20
>>> x
4.234481147613274e-05
>>> num = int(str(float(x)).replace('.', ''))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '423448114761e-05'

# cast to string
>>> str(float(x))
'4.23448114761e-05'

if you want to use decimal more precisely,
you should use decimal module.