Q: Is SQRT(n) possible in a POSIX compliant shell? A: Yes within limits.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Q: Is SQRT(n) possible in a POSIX compliant shell? A: Yes within limits.

Hi all...
This is just a fun project to see if it is possible to get a square root of a positive integer from 1 to 9200000 to 6 decimal places on a 64 bit architecture machine.
It is coded around dash and the results show the values from 0 to 10000.
Complex numbers can easily be catered for by taking the absolute value of the negative integer and placing the letter i at the end of the result(s).
Enjoy ripping it apart...
Code:
#!/usr/local/bin/dash
# sqrt_dash.sh
# Usage: [/full/path/to/][./]sqrt_dash.sh <integer_from_1_to_9200000>
# Fully POSIX compliant.
# Checked against Google for many values.
#
# Newton method integer maths only, POSIX compliant shell, (dash).
# Maximum positive integer, 64 bits, 9,223,372,036,854,775,807.
# Returns the square root of integer numbers from 1 to just over 9200000 to 6 places of decimals.
#
# Maximum positive integer, 32 bits, 2,147,483,647.
# Returns the square root of integer numbers from 1 to just over 2000 to 3 places of decimals.
#
# This is for 64 bit systems.
# Commented out is for 32 bit systems.

NUMBER=${1}
if [ ${NUMBER} -eq 0 ]
then
    # 32 bit systems.
    # echo "0.000"

    # 64 bit systems.
    echo "0.000000"

    exit
fi

# 32 bit systems.
# NUMBER=$(( NUMBER * 1000000 ))

# 64 bit systems.
NUMBER=$(( NUMBER * 1000000000000 ))

APPROX=$(( NUMBER / 2 ))
CLOSER=$(( (APPROX + NUMBER / APPROX) / 2 ))

while [ ${CLOSER} -ne ${APPROX} ]
do
        APPROX=${CLOSER}
        CLOSER=$(( (APPROX + NUMBER / APPROX) / 2 ))
done

# 32 bit systems.
# printf "%.3f\n" $(( APPROX ))e-3

# 64 bit systems.
printf "%f\n" $(( APPROX ))e-6

Results for numbers 0 to 10000 timed. OSX 10.14.6, default bash terminal.
Code:
Last login: Thu Oct 10 16:51:21 on ttys000
AMIGA:amiga~> cd desktop/Code/Shell
AMIGA:amiga~/desktop/Code/Shell> time for n in {0..10000}; do ./sqrt_dash.sh $n; done
0.000000
1.000000
1.414213
1.732050
2.000000
2.236067
2.449489
2.645751
2.828427
3.000000
3.162277
3.316624
3.464101
3.605551
3.741657
3.872983
4.000000
4.123105
4.242640
4.358898
........
........
99.879927
99.884933
99.889939
99.894944
99.899949
99.904954
99.909959
99.914963
99.919967
99.924971
99.929975
99.934978
99.939981
99.944984
99.949987
99.954989
99.959991
99.964993
99.969995
99.974996
99.979997
99.984998
99.989999
99.994999
100.000000

real    0m48.412s
user    0m18.066s
sys     0m18.601s

AMIGA:amiga~/desktop/Code/Shell> _


Last edited by wisecracker; 4 Weeks Ago at 04:02 PM.. Reason: Timer was wrong reset for dash.
This User Gave Thanks to wisecracker For This Post:
# 2  
;oDD

I showed it to the wife and I said 'what sticks out the most?'
She said the 'pink obviously!'

Anyhow here is an add-on to get Fixed Point Numbers SQRT from .100 to 9200000.000 using 'sqrt_bash.sh' edited to 32 bit mode.
All entries MUST be Fixed Point values even if they end in ?.000...
Code:
#!/usr/local/bin/dash
# FP_sqrt.sh
#
# Requires sqrt_dash.sh to work.
# This MUST be 3 decimal places only using 'sqrt_bash.sh' in 32 bit mode running in 64 bit.
#
# Usage: [full/path/to/][./]FP_sqrt.sh <.100_to_a_little_over_9200000.000>
#
# Note: numbers below 1.000 the input must be .??? format without the leading zero.
# The errors below .100 grow exponentially so values cannot to be trusted.

NUMBER=${1}

# Step 1, allocate this to _fractional_ part.
FRACT="${NUMBER##*.}"

# Step 2, get _whole_number_ part.
WHOLE="${NUMBER%%.*}"

# Step 3, put WHOLE and FRACT together and include 3 zeros.
NUMBER="${WHOLE}${FRACT}"'000'

# Step 4, call "sqrt_dash.sh" with new NUMBER into a variable.
NUMBER=$( ./sqrt_dash.sh "${NUMBER}" )

printf "%.3f\n" ${NUMBER}e-3

Results, confirmed by Google. Same MBP platform as always.

Code:
Last login: Fri Oct 11 15:15:40 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh .100
0.316
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh .250
0.500
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh .500
0.707
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh .750
0.866
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 1.000
1.000
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 2.000
1.414
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 20.000
4.472
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 200.000
14.142
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 333.333
18.257
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 777.777
27.889
AMIGA:amiga~/Desktop/Code/Shell> ./FP_sqrt.sh 9187233.598
3031.045
AMIGA:amiga~/Desktop/Code/Shell> _

# 3  
Paul Hsieh's square root page has a bunch of square root algorithms including using Newton's method:

Paul Hsieh's Square Root page

I used to play with these, lots o' fun.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Quote:
Originally Posted by jim mcnamara
Paul Hsieh's square root page has a bunch of square root algorithms including using Newton's method:

Paul Hsieh's Square Root page

I used to play with these, lots o' fun.
Neat, not an integer shell version though. It would be interesting if someone else has actually done it...
Working on Nth root at the moment, but I suspect it will be a step too far for a POSIX shell.

@Neo...

The dark green looks good...

Moderator's Comments:
Mod Comment Discussion on new forum tag and color changes moved here.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #140
Difficulty: Easy
Unix and the C programming language were both developed at AT&T.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Equivalent to let command in POSIX shell

Hi all, I am learning POSIX shell programming, and the book I read, uses the let command for integer arithmetic. I have downloaded and use the shellcheck program on Linux. This programs says: In POSIX sh, 'let' is undefined. See the screenshot attached. What is the POSIX... (1 Reply)
Discussion started by: johnprogrammer
1 Replies

2. What is on Your Mind?

Popularity-Boost for the POSIX-Shell in the Era of Containerized Computing?

Not even thinking that POSIX-Shell is deprecated, but I like working with bash very much, because of it's increased comfort and advanced functions. And in my world here it's available everywhere as default. Working with kubernetes now, it seems there is a paradigm shift in terms of resources.... (1 Reply)
Discussion started by: stomp
1 Replies

3. Shell Programming and Scripting

Pure POSIX shell scripting...

Hi all... This is more of a concensus question than help... As many of you know I am experimenting with the limitations of Pure POSIX shell scripting. Q: Is the directory /bin considered part of the Pure POSIX shell or must I stick entirely with the builtins only? The reason is I... (2 Replies)
Discussion started by: wisecracker
2 Replies

4. Shell Programming and Scripting

Is Rule 7 of POSIX shell grammar rules written correctly?

The POSIX shell standard grammar rules are at Shell Command Language I am trying to understand Rule 7 and I don't. I think there may be some mistakes there. I am not complaining about the standard; rather, I am concerned that my perception is wrong, and I don't understand something important.... (3 Replies)
Discussion started by: Mark_Galeck
3 Replies

5. UNIX for Dummies Questions & Answers

Soft and hard limits for nproc value in /etc/security/limits.conf file (Linux )

OS version : RHEL 6.5 Below is an excerpt from /etc/security/limits.conf file for OS User named appusr in our server appusr soft nproc 2047 appusr hard nproc 16384 What will happen if appusr has already spawned 2047 processes and wants to spawn 2048th process ? I just want to know... (3 Replies)
Discussion started by: kraljic
3 Replies

6. Shell Programming and Scripting

Logical expression in POSIX compliant Korn Shell

Hi, i want to check if a variable var1 is not a or b or c pseudo code: If NOT (var1 = a or var1 = b or var1 = c) then ... fi I want to use POSIX complaint Korn shell, and for string comparison For the following code, logical.sh #!/usr/bin/ksh var="j" echo "Var : $var" if ! { || ||... (12 Replies)
Discussion started by: ysrini
12 Replies

7. UNIX for Dummies Questions & Answers

Making times mysql compliant

I have the following file: Loc1,20080102,230100,0.5617,0.5617 Loc1,20080102,230400,0.5616,0.5616 Loc1,20080102,230700,0.5615,0.5615 Then I use the following code to turn the third column into something which to mysql has the time datatype: sed -i '' -e "s#,\(..\)\(..\)\(..\),#,\1:\2:\3,#"... (2 Replies)
Discussion started by: figaro
2 Replies

8. Programming

Is gcc compliant with the C++ standards

Hello Friends, I am a newbie and have started using different compilers and tools to make myself familiar with their workings. I wanted to know that how compliant is gcc with the C++ standards. It is pretty obvious that no compiler is close to being completely compliant, but if there are some... (7 Replies)
Discussion started by: hthapar
7 Replies

9. Programming

sqrt is not find???

I was writing a simple program in linux, which includes sqrt function of c. I included the math.h. But when I use gcc to compile it, it gave an error message: /home/murat/tmp/ccOv9upo.o(.text+0x4b): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status I... (2 Replies)
Discussion started by: murataht
2 Replies

10. Programming

sqrt

Hi! when i'm trying to compile this lite example on my linux machine I'll get errors and i don't know why.. #include <stdio.h> #include <math.h> /* needed by sqrt() */ int main() { printf("%f", sqrt(10.0)); return (0); } this is the error: /tmp/cc33hNVHK.o: In function... (1 Reply)
Discussion started by: CreamHarry
1 Replies

Featured Tech Videos