gavinhoward / bc

An implementation of the POSIX bc calculator with GNU extensions and dc, moved away from GitHub. Finished, but well-maintained.
https://git.gavinhoward.com/gavin/bc
Other
145 stars 29 forks source link

Not an issue, but a change proposal #78

Closed freebrowser1 closed 2 months ago

freebrowser1 commented 2 months ago

Excellent software !

This bc is MUCH faster than the bc supplied to Linux distros. I downloaded the source package, compiled under Ubuntu ARM and it worked. I did the same on Termux on an Android cellphone (which also has the slow 1.7 version) and it was blazingly fast as well ! It can be used for other bases up till sixteen. I changed it (locally, not on this Github site) to max base 36. Maybe that can be an option to publish it here.

gavinhoward commented 2 months ago

Thank you for your compliments!

As it turns out, it's the accompanying dc that only goes up to base 16. bc goes up to base 36.

There is a reason for base 36: according to the bc standard, it must be possible to set all input bases with a one character number. This is so any input base could be set no matter the current input base.

For example, say you write a function that should work in multiple bases. What if it temporarily has to set a new input base. How does it do that when the input base could be anything from binary to hexadecimal?

The answer is here (scroll down to where it says, "When either ibase or obase is assigned a single digit value...").

Now my bc does take that up to base 35, which is the base you can reach by using Z as the single digit value.

To test this, try this code:

$ bc
>>> obase=Z
>>> 35
01 00
>>> quit

I hope this helps.

gavinhoward commented 2 months ago

I realize I forgot to include an example with ibase.

$ bc
>>> ibase=Z
>>> 10
35
>>> ibase=6*6
>>> 10
36
>>> quit

So yes, my bc already goes up to base 36 for ibase too, although you do need some special way, like 6*6, to get it.

freebrowser1 commented 2 months ago

I have changed a few source files (find in the files in the accompanied zip lines with //!!). This allows using max base 36 with all digits and all UPPERCASE letters. When I and O are not wanted, use export NO_I_O_DIGITS=1 (or any value) before executing bc and 34 = @, 35 = #.

src.zip

gavinhoward commented 2 months ago

You did not read my comments. My bc already supports up to base 36.

Your changes are not needed.

Closing.

freebrowser1 commented 2 months ago

I checked again, downloaded the source kit, compiled it (Ubuntu arm64) and ran this: ' bin/bc -l <<< "ibase=obase=24; 2^G;"' This results in 04 17 18 16, i.e. decimal numbers as 'digits' which should only appear with base above 36. After applying my changes, it issues the correct value '4HIG'.

gavinhoward commented 2 months ago

This cannot be changed.

There is a standard for bc, and in that standard, it says,

For bases greater than 16, each digit shall be written as a separate multi-digit decimal number. Each digit except the most significant fractional digit shall be preceded by a single . For bases from 17 to 100, bc shall write two-digit decimal numbers; for bases from 101 to 1000, three-digit decimal strings, and so on.

So no, as much as I would like to change it, it cannot be changed.