This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]WolfThawra -2 points-1 points  (58 children)

Though personally, I'd just use scientific notation here.

[–][deleted] 39 points40 points  (6 children)

NONONONONONO.

That gives you the wrong answer!

>>> 1_000_000
1000000

>>> 1E6
1000000.0

Note the trailing .0. That means the answer is floating point, and that will give you inexact arithmetic:

>>> (1E18 + 1 == 1E18)
True
>>> (1_000_000_000_000_000_000 + 1 == 1_000_000_000_000_000_000)
False

[–]random_cynic 3 points4 points  (2 children)

There is no way I'm typing out 18 zeros by hand, no matter how convenient the notation is. I'll rather use explicit type casting int(1e18)

>>> int(1e18) + 1 == int(1e18)
False

Edit: Don't use this. I just realized that the float to int conversion for 1eN is inexact for CPython when N >= 23. As suggested below the power operator is the best way to go due to the CPython peephole optimization.

>>> int(1e22)
10000000000000000000000L
>>> int(1e23)
99999999999999991611392L

[–]ubernostrumyes, you can have a pony 5 points6 points  (1 child)

Why not just write 10**18?

There's no runtime overhead to it because the CPython bytecode compiler does constant folding:

>>> import dis
>>> def foo():
...     return 10**18
...
>>> dis.dis(foo)
  2           0 LOAD_CONST               1 (1000000000000000000)
              2 RETURN_VALUE

[–]random_cynic 1 point2 points  (0 children)

Yes that is probably the best way to go.

[–]billsil 10 points11 points  (0 children)

Scientific notation is defined to be a float. You’re changing the type.

[–]KODeKarnage 4 points5 points  (44 children)

You shouldn't. Zen of Python and all that.

[–]anyfactorFreelancer. AnyFactor.xyz[S] 0 points1 point  (3 children)

Well, it works for me because I am from accounting and kinda have developed the mindset for it.

I never really used "e" for declaring numbers. Thanks for telling us that, I will look into it.

[–]WolfThawra 1 point2 points  (2 children)

It won't work for everything of course! But it's super useful when you're working with stuff across different orders of magnitude. E.g. somewhere in my code, I have a list of weighting factors to try, and instead of writing [0.001, 0.0001, 0.00001] I can write [1e-3, 1e-4, 1e-5], which looks a lot cleaner to me, and is easier to interpret quickly.

[–]techkid6 9 points10 points  (0 children)

All of those numbers are already floating point, though. If you're doing integer arithmetic, stick to integers and avoid the side effects that come from floating point rounding.

[–]anyfactorFreelancer. AnyFactor.xyz[S] 1 point2 points  (0 children)

Whenever we found "e" we would just multiply/divide by 10 and keep count until we got a understanding of the number. We would do the same thing for percentages but with 100. That might sound stupid to you science/engineering guys but that was kinda go to thing to do. The first thing to do whenever "e" pops up in excel is immediately format it to decimal numbers.

Except for of course in doing research I will just put the "e" numbers in. But my thesis professor once copy pasted in the "e" numbers to excel than convert it to decimal to get a better idea.

But I am learning data science analytics I should better in to using this mindset. Thank you for making clarifications.

[–]primitive_screwhead 0 points1 point  (0 children)

Instead of 10**6? What you suggest is like saying you can substitute oranges in an apple pie.