sharkdp / numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
https://numbat.dev
Apache License 2.0
1.23k stars 52 forks source link

Add support for 'k'/'M' suffixes in integer numbers? #269

Open sharkdp opened 11 months ago

sharkdp commented 11 months ago

… to support inputs like 300k € or 20k people?

We could easily achieve this by setting unit k = 1000, but that would reduce the number of available one-character variable names even further.

triallax commented 11 months ago

We could easily achieve this by setting unit k = 1000, but that would reduce the number of available one-character variable names even further.

This could be gated behind a config flag, but I don't know if it's worth polluting the config file for something like this.

sharkdp commented 10 months ago

Yeah, I rather think this should be a special parser feature, if we decide to implement it.

SollyBunny commented 9 months ago

if it where a special parser feature what would happen when you do

let k = 3
20k

?

triallax commented 9 months ago

What immediately comes to mind is to make whitespace between the 20 and the k significant, so 20k would be 20_000 but 20 k would be 60. Admittedly though that doesn't seem ideal.

sharkdp commented 9 months ago

if it where a special parser feature what would happen when you do

let k = 3
20k

That is a good question. To avoid ambiguity, we would have to reserve the k identifier in this case as well.

What immediately comes to mind is to make whitespace between the 20 and the k significant

I'd rather not do that. Also, I like how we can just write things like or 2x, so I would like to keep the possibility to have identifiers 'attached' to numbers directly.

Maybe it's best to abandon this idea for now. Users can simply set unit k = 1000 in their init.nbt.

Please feel free to comment here if you think this should be reopened.

triallax commented 9 months ago

I'd rather not do that. Also, I like how we can just write things like 2π or 2x, so I would like to keep the possibility to have identifiers 'attached' to numbers directly.

I was thinking about only making it significant for k, M, etc, not for other identifiers. I do agree that it's not a very nice solution though.

sharkdp commented 9 months ago

I was thinking about only making it significant for k, M, etc, not for other identifiers

right. I think it's a viable option. I really dislike syntactic quirks in other languages, so I think we should maybe try to avoid things that users might trip over. And only introduce special cases if we are certain that there is a huge advantage.

rben01 commented 1 month ago

Since numbers cannot currently end in underscores, one option could be that _k, _M are the suffixes. So 20k is 60, 20_k is 20,000. Just a thought, not sure whether this is better enough than 20e3 to be worth it.

sharkdp commented 1 month ago

Thank you for the idea. That would be possible.

Just a thought, not sure whether this is better enough than 20e3 to be worth it.

Right. Let's maybe reopen this to collect a bit more feedback before we decide anything.