Correct me if i'm wrong, but doesnt integer stop at like... 2.7bil?
And if i look at:
And make it easier (for me) to read: 9'223'372'030'926'249'001, which are (rounded up) 10 billion billions, or about 400 times the max of a 'reliable' integer value.
And as on overflow, they stop counting at the ~2.7bils
(overnighted and not used to such high numbers, might be mathematical incorrect proportions, but you get the idea)
One would need (afaik) a long int or int64, which are both (afaik) not available to the bash shell.
hth
So much for getting some sleep...
According to the standards, conforming shells must use signed long int (or something else that provides the same results when working with values in the range of a signed long int). With the bash being talked about on this thread:
shows the results required for a conforming shell in the 1st two examples and shows an allowed (and expected) result for the 3rd case where the last operand is out of the range of a long int (signed or unsigned).
I agree that it is hard to count the digits when looking at numbers that big, but the shell doesn't allow thousands separators in integer constants given to it in expressions nor when they are used as operands for shell built-ins expecting integer arguments.
Last edited by Don Cragun; 08-21-2015 at 01:03 PM..
Reason: Add note about large integer constants in the shell.
This User Gave Thanks to Don Cragun For This Post:
According to the standards, conforming shells must use signed long int (or something else that provides the same results when working with values in the range of a signed long int).
That is another stroke against bash being standards compliant, then. I recall getting 64-bit numbers on systems where 'long int' is 32 bit (though honestly was quite happy to have them).
That is another stroke against bash being standards compliant, then. I recall getting 64-bit numbers on systems where 'long int' is 32 bit (though honestly was quite happy to have them).
If a shell uses signed long long int (64-bits) in an environment where a signed long int is 32-bits, that is still conforming. Any operation using 64-bit ints instead of 32-bit ints will get exactly the same results for any arithmetic operation that does not cause overflow when using 32-bit signed values.
This User Gave Thanks to Don Cragun For This Post:
For those that don't know, ths is the method used...
Take all of the odd numbers in a series:-
1, 3, 5, 7, 9, 11, 13........
Then:-
[1] 1 = 1 = 1^2
[2] 1+3 = 4 = 2^2
[3] 4+5 = 1+3+5 = 9 = 3^2
[4] 9+7 = 1+3+5+7 = 16 = 4^2
[5] 16+9 = 1+3+5+7+9 = 25 = 5^2
[6] 25+11 = 1+3+5+7+9+11 = 36 = 6^2
[7] 36+13 = 1+3+5+7+9+11+13 = 49 = 7^2
And so on...
It is as simple as that, nothing more nothing less...
The linear addition by one of the square bracketed __line_numbers__ are the integer, (perfect), square root...
OK. I think I'm caught up on my sleep. And, I think I have a new script that meets all of the requirements (bash script using only integer arithmetic and only using shell built-ins running on Mac OS X on a MacBook Pro (assembled in mid 2014)). I am, however, using OS X version 10.10.5 instead of version 10.7.5 that wisecracker was using.
The bash script wisecracker provided in post #1 in this thread mostly works fine for relatively small values, but takes a LONG time for relatively large values. I was surprised that it said that 0 is not a perfect square instead of saying that the minimum value it would process is 1. I wanted to see what it would do when invoked as:
I wasn't surprised that it came up with the correct answer:
but a ps run shortly before it completed showed that it used more than 595 cpu minutes to compute that result.
While that was consuming one core of one cpu, I played around with Corona688's bash script in post #16 in this thread. It correctly reported that 0 is a perfect square (which surprised me since it always uses 1 as the low end of its binary search range). And, due to the way it calculates the stopping point for the binary search and the way it determines the value it is processing, it says:
even though the correct response would be:
When given invalid numbers and when given valid numbers that do not fit in a signed long integer, bash prints a diagnostic message when processing that value with Corona688's script and sometimes continues processing and sometimes terminates processing at that point.
I made a slight modification to Corona688's script to read values from a file instead of just processing one command line argument.
And, I produced the following bash script which performs a more traditional binary search. It uses a little bit of number theory to reduce the range of the binary search. (An x-1 or x digit integer, where x is even, will have a square root that is x/2 digits if it has an integer square root.) It performs lots of tests to verify that the input is a valid number and is in range for a signed long int.
It reads its input from a file named file and looks for two values on each line. The 1st value is the number to be processed. The 2nd value (if present) indicates the expected results for the 1st value (-1 if it is not a perfect square or the square root if it is a perfect square). If the expected value does not match the computed value, it prints a diagnostic. If the 2nd value is not present, the result is printed, but not verified.
With the following input in file:
it produces the output:
with average times:
for 10 runs. With the same input file, the modified version of Corona688's script produces the output:
with average times:
for 10 runs. Note that it is expected to run faster than the above script because it doesn't perform any error checking and it doesn't process the last two input lines because it dies on any of the last three input lines. I was surprised that the times were so close. (Apparently reducing the binary search range made up for the extra data verification tests.)
Note also that I am not saying that Corona688's script should perform any error checking; that was not a requirement for this project. I machine-generated most of the input file (after I found the anomalies in the results from the ksh script I presented earlier) and added code to verify that the results computed matched known results as a debugging aid.
These 3 Users Gave Thanks to Don Cragun For This Post:
As 0^2 is 0, I never even considered it to be a perfect square so I stand corrected.
My error; however my code can very easily be changed to cater for it.
As you say it is slow, but it was coded from an observation not from any mathematical set of algorithms, that is, every _line_no_ is the perfect square of the odd number series...
I have to find last delimiter in each line of a file and store the value after the last '/' in a variable in ksh script...Pls Pls help me:(The file is as shown below:
/opt/apps/cobqa/apps/abadv/bind/advc0007.bnd
/opt/apps/cobqa/apps/abbrio/bind/naac6115.bnd... (5 Replies)
I want to print only the lines that meet the criteria : "worde:" and "wordo;"
I got this far:
sed -n '/\(*\)\1e:\1o;/p;'
But it doesn't quite work.
Can someone please perfect it and tell me exactly how its a fixed version/what was wrong with mine?
Thanks heaps, (1 Reply)
Hi All,
I have a text file which looks like this:
computer programming
systems engineering
I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this:
computer programming
systems engineering
I am using... (3 Replies)
I've got an aix-box somewhere on the network and a PC on my desk. Nothing fancy so far.
The PC is made dual-boot:
- windowsXP with putty & winSCP
or
- slackware 13 with xfce4 installed.
The aix-box runs DB2 v8.2 and I've installed db2top to monitor the database.
db2top is a character... (0 Replies)
Here's my work of testing whether a number input is perfect or not..
echo Enter a number
read no
i=1
ans=0
while
do
if
then
ans='expr $ans + $i'
fi
i='expr $i + 1'
done
if
then
echo $no is perfect
else
echo $no is NOT perfect
fi (12 Replies)
Hi All,
I have a file of the following format.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user... (5 Replies)