Page 1 of 3

Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 1:04 pm
by Sicro
Would be nice if we could write like that:

Code: Select all

1_000_000

0.103_850

$6B_C7_5E_2D_63_10_00_00

%1111_0100_0010_0100_000_0

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 3:29 pm
by skywalk
Tough call. Comparing C libs and other code without '_'s would be a nightmare.
And this opens the door for many bugs.
As well as slowing down the syntax checker.

What if a macro inserts an underscore and gets interpreted as a number?

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 3:31 pm
by BarryG
Why on earth would you want to write numbers like that? What's the advantage?

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 4:15 pm
by DoubleDutch
Could be useful, I don't see any compatibility issues.

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 4:25 pm
by Marc56us
We can (for decimal numbers)

Code: Select all

; Input
A$ = "1_000_000"
Debug A$
A = Val(ReplaceString(A$, "_", ""))
Debug A

; Output
Debug FormatNumber(1000000, 0, "", "_")
FormatNumber() PB 5.50

Code: Select all

1_000_000
1000000
1_000_000

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 4:34 pm
by Tenaja
BarryG wrote:Why on earth would you want to write numbers like that? What's the advantage?
The advantage is to make it instant to tell if you are writing 10000000 or 10000000. Or, rather, 10,000,000 vs 100,000,000. Without them, you have to count the digits. With them, it's an instant glance.
Tough call. Comparing C libs and other code without '_'s would be a nightmare.
And this opens the door for many bugs.
As well as slowing down the syntax checker.

What if a macro inserts an underscore and gets interpreted as a number?
I do not believe the syntax checker would be slowed down noticeably. You would only be adding one character to a number checker, which already permits at least 17. (0-9, A-F, and decimal) Adding the underscore will be trivial, and scanned just like the decimal. As for bugs, it would not increase them unless you are trying to convert to a new language, and that's going to require bugchecking anyway.

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 5:08 pm
by BarryG
Tenaja wrote:The advantage is to make it instant to tell if you are writing 10000000 or 10000000. Or, rather, 10,000,000 vs 100,000,000. Without them, you have to count the digits. With them, it's an instant glance.
Oh, I misunderstood. I thought they were like Data or something, rather than a single number on each example.

Thought: Rather than changing the compiler to deal with it, maybe the IDE could syntax-color the numbers instead, with slightly darker/lighter characters with each thousands section? Like this:

Image

At any rate, a pre-compiler tool could also easily do what Sicro wants.

Or use constants if you're going to use such large numbers, so it's easier to read in your code:

Code: Select all

#n_10_mill=10000000
#n_100_mill=100000000

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 6:30 pm
by #NULL
+1

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 7:25 pm
by Josh
1'000'000.00

I would prefer a single quote, as it is/was also used on pocket calculators. There should not be any conflicts with it.

Re: Allow underscore as separator for number literals

Posted: Sat Feb 20, 2021 11:45 pm
by Sicro
skywalk wrote:Tough call. Comparing C libs and other code without '_'s would be a nightmare.
And this opens the door for many bugs.
As well as slowing down the syntax checker.

What if a macro inserts an underscore and gets interpreted as a number?
I don't see a problem. The underscore is removed during compilation, so there is no difference between the number literals 1_000 and 1000. It is a syntax supported by new programming languages (for example Rust and Nim).
BarryG wrote:Thought: Rather than changing the compiler to deal with it, maybe the IDE could syntax-color the numbers instead, with slightly darker/lighter characters with each thousands section?
I find a symbol better than syntax coloring in that case. There are already many token types that can be colored and even more colors will not improve the readability of the code, but worsen it.
BarryG wrote:At any rate, a pre-compiler tool could also easily do what Sicro wants.
Sure, for many things own solutions can be developed. The problem is that then everyone also has to set up the special solution to be able to use the codes, which could certainly be too cumbersome for some.
BarryG wrote:Or use constants if you're going to use such large numbers, so it's easier to read in your code:

Code: Select all

#n_10_mill=10000000
#n_100_mill=100000000

Code: Select all

; this:
#N_Thousands = 1000
a = 5 * #N_Thousands + 155

; against:
b = 5'155
; or
c = 5_155
There are a lot of cases where constants make sense, but for such things rather not. The last two variants are definitely better.
Josh wrote:1'000'000.00

I would prefer a single quote, as it is/was also used on pocket calculators. There should not be any conflicts with it.
I also agree with this variant. The apostrophe is not yet used in the PureBasic language, unlike the underscore, so it would probably be more suitable.

Re: Allow underscore as separator for number literals

Posted: Sun Feb 21, 2021 2:15 am
by skywalk
We already have X = 1e3 ; 1000
Or X = 1e9; 1,000,000,000
Kill me if I have to type a google zero's with '_'s. :evil:

Re: Allow underscore as separator for number literals

Posted: Sun Feb 21, 2021 3:47 am
by BarryG
Sicro wrote:The apostrophe is not yet used in the PureBasic language
Yes it is, to convert literal characters to their ASCII value:

Code: Select all

For c = 'A' To 'Z'
  Debug Chr(c)
Next

Re: Allow underscore as separator for number literals

Posted: Sun Feb 21, 2021 4:32 am
by BarryG
Tenaja wrote:The advantage is to make it instant to tell if you are writing 10000000 or 10000000. Or, rather, 10,000,000 vs 100,000,000. Without them, you have to count the digits. With them, it's an instant glance.
Feature request posted here -> viewtopic.php?f=18&t=76794

Re: Allow underscore as separator for number literals

Posted: Sun Feb 21, 2021 4:55 am
by Tenaja
skywalk wrote:We already have X = 1e3 ; 1000
Or X = 1e9; 1,000,000,000
Kill me if I have to type a google zero's with '_'s. :evil:
Fred's not the type to break 20 years of code to force a feature request on everyone. Typically he makes new features like this optional.

Re: Allow underscore as separator for number literals

Posted: Sun Feb 21, 2021 5:47 am
by Tenaja
#NULL wrote:+1
+1 me too. An ide edit would require double clicking, which wouldn't likely be easier (for Fred) and wouldn't be as user friendly.