Allow underscore as separator for number literals

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Allow underscore as separator for number literals

Post 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
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow underscore as separator for number literals

Post 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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow underscore as separator for number literals

Post by BarryG »

Why on earth would you want to write numbers like that? What's the advantage?
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Allow underscore as separator for number literals

Post by DoubleDutch »

Could be useful, I don't see any compatibility issues.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Allow underscore as separator for number literals

Post 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
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Allow underscore as separator for number literals

Post 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.
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow underscore as separator for number literals

Post 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
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Allow underscore as separator for number literals

Post by #NULL »

+1
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Allow underscore as separator for number literals

Post 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.
sorry for my bad english
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Allow underscore as separator for number literals

Post 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.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow underscore as separator for number literals

Post 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:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow underscore as separator for number literals

Post 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
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow underscore as separator for number literals

Post 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
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Allow underscore as separator for number literals

Post 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.
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Allow underscore as separator for number literals

Post 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.
Post Reply