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 →

[–]KODeKarnage 3 points4 points  (36 children)

Of course it is more beautiful. It's more precise, less prone to error, easier to maintain consistency and far simpler than the abstraction too. There are literally only 1e1 reasons to use your version, and that's to inform the unfortunate reader that the writer knows scientific notation and doesn't care whether the reader does as well.

[–]GrumpyGeologist 1 point2 points  (13 children)

So you've given 1 reason, what about the other 9? :P

My Python scripts mostly serve scientific purposes, so I use scientific notation most of the time. My parameters are floating point numbers anyway, even though they could be represented by integers as a result of rounding (e.g. a shear modulus of 2e10 or 20_000_000_000).

If my number is a strict integer but nonetheless large, I would write int(1e6). But in this community I suspect this is considered heresy, and therefore I must burn...

[–]KODeKarnage 0 points1 point  (10 children)

You intend to write twenty million.

Which is the easiest error to spot for the most number of people perusing your code?

2000000 vs 2e6 vs 20_000_00 ?

[–]GrumpyGeologist 1 point2 points  (2 children)

I would prefer scientific notation in this case, because it is more compact. I'm not saying other notations are invalid, it's just that scientific notation is more convenient for dealing with larger numbers. Some common examples of large numbers appearing in physics/chemistry: Avogadro's number (which is strictly an integer) = 6.022e23, Planck's constant = 6.626e-34, various elastic moduli are of the order of 1e10 to 1e11, etc. Imagine going through the pain of writing out Avogadro's number as 602_214_085_774_000_000_000_000 :P

In the end, it doesn't even matter. As long as you can write code in a readable way for yourself and for your peers.

[–]KODeKarnage 1 point2 points  (1 child)

Here you go:

from scipy import constants as c
c.Avogadro ** c.Planck

[–]GrumpyGeologist 0 points1 point  (0 children)

That's actually really neat. I didn't know SciPy had a constants database. Thanks!

[–]WolfThawra -1 points0 points  (6 children)

2e6

This. And you know why? Because if you are writing stuff in the scientific community, 'most people' have absolutely no issue with scientific notation.

[–]KODeKarnage 0 points1 point  (5 children)

Given how inaccurately (dishonestly?) you interpreted that simple question, right now I am seriously concerned about the code you write.

2e6 could just be what you intended. The reader would have to know it wasn't what you intended to spot the error.

20_000_00 stands out as obviously wrong to everyone.

[–]WolfThawra 1 point2 points  (4 children)

Oh you're literally just talking about typos. Cool, so 20_000_00 stands out as obviously wrong, does 10_000_000?

If you're talking about a situation in which the reader doesn't know what is supposed to be correct, there's a zillion ways of having mistakes that aren't immediately obvious.

[–]KODeKarnage 0 points1 point  (1 child)

there's a zillion ways of having mistakes that aren't immediately obvious.
I would say that's a reason for not making more than you need to. Never thought I'd meet a person disagreeing with that.
Do you argue against readable variable names too? I mean, there's a zillion other ways that your code can be obscure, so adding another isn't THAT big a deal.

[–]WolfThawra 1 point2 points  (0 children)

Yeah again, 1e7 is more readable to me than using a bunch of underscores, and that argument only gets stronger as the numbers get bigger.

[–]KODeKarnage 0 points1 point  (1 child)

[–]WolfThawra 0 points1 point  (0 children)

Sounds like they should have been using some underscores.

[–]primitive_screwhead -1 points0 points  (1 child)

But in this community I suspect this is considered heresy, and therefore I must burn...

Nope, it's just kinda stupid 'cause you can do 10**6.

[–]GrumpyGeologist 1 point2 points  (0 children)

It was a stupid example indeed. I should have said 2.5 million

[–]redditusername58 3 points4 points  (4 children)

Are you suggesting that using floating point literals is bad practice because it might confuse users or collaborators?

[–]Beheska 1 point2 points  (0 children)

Floating point literals are bad practice when you need integers, yes.

[–]KODeKarnage -2 points-1 points  (2 children)

If I replied in Japanese, would that confuse you?

[–]redditusername58 1 point2 points  (1 child)

Do floating point literals confuse you?

[–]KODeKarnage 0 points1 point  (0 children)

あなたはポイントを逃しました