Sponsored Content
Top Forums Shell Programming and Scripting Q: Is SQRT(n) possible in a POSIX compliant shell? A: Yes within limits. Post 303039666 by wisecracker on Friday 11th of October 2019 10:51:01 AM
Old 10-11-2019
;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> _

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. UNIX for Dummies Questions & Answers

sqrt in bash

Hi, i have a the following script: #!/bin/bash a=3 b=9 let "c= b*a" let "d=sqrt $c " echo $d But when i execute the code, it gives me the an error saying: line 5: let: d=sqrt 27 : syntax error in expression (error token is "27 ") Can any body tell me what I'm doing wrong? (5 Replies)
Discussion started by: limadario
5 Replies

4. 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

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

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

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

8. 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

9. 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

10. OS X (Apple)

Generate a random number in a fully POSIX compliant shell, 'dash'...

Hi all... Apologies for any typos, etc... This took a while but it didn't beat me... Although there are many methods of generating random numbers in a POSIX shell this uses integer maths and a simple C source to create an executable to get epoch to microseconds accuracy if it is needed. I take... (8 Replies)
Discussion started by: wisecracker
8 Replies
SQRT(3) 						     Linux Programmer's Manual							   SQRT(3)

NAME
sqrt, sqrtf, sqrtl - square root function SYNOPSIS
#include <math.h> double sqrt(double x); float sqrtf(float x); long double sqrtl(long double x); Link with -lm. Feature Test Macro Requirements for glibc (see feature_test_macros(7)): sqrtf(), sqrtl(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or cc -std=c99 DESCRIPTION
The sqrt() function returns the nonnegative square root of x. RETURN VALUE
On success, these functions return the square root of x. If x is a NaN, a NaN is returned. If x is +0 (-0), +0 (-0) is returned. If x is positive infinity, positive infinity is returned. If x is less than -0, a domain error occurs, and a NaN is returned. ERRORS
See math_error(7) for information on how to determine whether an error has occurred when calling these functions. The following errors can occur: Domain error: x less than -0 errno is set to EDOM. An invalid floating-point exception (FE_INVALID) is raised. CONFORMING TO
C99, POSIX.1-2001. The variant returning double also conforms to SVr4, 4.3BSD, C89. SEE ALSO
cbrt(3), csqrt(3), hypot(3) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2008-08-05 SQRT(3)
All times are GMT -4. The time now is 07:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy