bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: math operations with base#prefix


From: Kerin Millar
Subject: Re: math operations with base#prefix
Date: Mon, 18 Sep 2023 03:02:51 +0100
User-agent: Cyrus-JMAP/3.9.0-alpha0-745-g95dd7bea33-fm-20230905.001-g95dd7bea

Hi Victor,

On Sun, 17 Sep 2023, at 8:59 PM, Victor Pasko wrote:
> Hi,
>
> Could you please take a look at attached bug.bash.
>
> Maybe, not all math combinations were presented there or the test has
> duplications somehow.
> Here are results of several runs with test# as argument
>
>
> *% bash --version*GNU bash, version 5.2.15(3)-release (x86_64-pc-cygwin)
>
> Good test without argument but others with errors :(
> *% ./bug.bash*
>
> res1=010 good 010 base8
> res2=03 good 03 base8
> res=17 good result 17 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> res1=8 good result 8 base10
> res1=10 good result 10
> res1=10 good result 10
> res1=010 good result 010 base8
> base10-res1=10 good result 10
> res1=16 good result 16
>
>
> *% ./bug.bash 1*
> TESTCASE=1
> res1=010 good 010 base8
> res2=03 good 03 base8
> res=17 good result 17 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> ./bug.bash: line 29: let: res = base10#010 + base10#03 * 3: syntax error:
> invalid arithmetic operator (error token is "#010 + base10#03 * 3")

This seems like a misinterpretation of the manual. The manual states that 
numbers "take the form [base#]n, where the optional base is a decimal number 
between 2 and 64 representing the arithmetic base". As such, "base10" is not a 
decimal number between 2 and 64, whereas "10" would be.

> base10-res=19 good result 19 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> res1=8 good result 8 base10
> res1=10 good result 10
> res1=10 good result 10
> res1=010 good result 010 base8
> base10-res1=10 good result 10
> res1=16 good result 16
>
>
> *% ./bug.bash 2*
> TESTCASE=2
> res1=010 good 010 base8
> res2=03 good 03 base8
> res=17 good result 17 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> base10-res=19 good result 19 base10 (res1+3*res2)
> ./bug.bash: line 35: let: res = 10#res1: value too great for base (error
> token is "10#res1")

For numbers in the form "[base#]n", it isn't practically possible for n to be 
specified using a variable without prefixing it with a sigil (so that it is 
treated as a parameter expansion and injected). There is a very good reason for 
this: numbers in a base higher than 10 can require alphabetical letters to be 
expressed. Consider the following example.

$ echo $(( 16#ff ))
255

This is the appropriate outcome. It would be undesirable for "ff" to be treated 
as a variable name identifier there.

In your case, the error is that the letters "r", "e" and "s" have ordinal 
values that are too high to be valid for base 10, but they could have been 
valid for a higher base.

$ echo $(( 29#res1 ))
671090

-- 
Kerin Millar <kfm@plushkava.net>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]