Gobsmacked by ksh93 floating point arithmetic.


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Gobsmacked by ksh93 floating point arithmetic.
# 15  
Old 10-18-2018
Hi C688...
This will probably attach itself to my previous...

Thanks, much like my reply above...

I can see it now.
# 16  
Old 10-18-2018
The for-loop comes from C, where it follows the form

Code:
for(statement1;  statement2;  statement3) {
}

which does effectively this:

Code:
statement1;

while(statement2) {
      statement3;
}

It's usually used for(var=0; var<10; var++) but any valid expression will do. Most languages with for() loops allow them to be flexible like that, though few languages can cram as many things into one expression as C Smilie

In the BASH/KSH adaptation, you can put anything inside each statement that you might put inside a (( )) block, including nothing.
# 17  
Old 10-27-2018
OK, using Corona688's code for SINE we now have Sin(), Cos(), NthRoot() and Sqrt() functions.
The 'e' and 'PI' constants are to 17 decimal places, NthRoot() and Sqrt() to 16 decimal places and Sin() and Cos() to 8 decimal places.
These precisions are good enough for basic mathematical tasks, after all KSH93 was not designed to send a spacecraft to Neptune. <wink>
Code:
#!/bin/ksh
# ksh_math.sh
# Basic Math extensions.

# CONSTANTS.
PI=3.14159265358979323
e=2.71828182845904523

# Degrees to radians.
TORADIANS=$(( PI/180.0 ))
# Radians to degrees.
TODEGREES=$(( 180.0/PI ))

# NTH ROOT of a positive floating point number.
# Called as:
# NthRoot NUMBER NTHROOT [PRECISION]
# ALL POSITIVE VALUES.
# NUMBER and NTHROOT can be floating point, optional PRECISION is an integer from 1 to 16.
NthRoot()
{
	# Make these local.
	typeset NUMBER var
	typeset NTHROOT var
	typeset PRECISION var

	NUMBER=$1
	NTHROOT=$2
	PRECISION=$3

	if [ "$PRECISION" = "" ] || [ $PRECISION -lt 1 ]
	then
		# My school log table(s) accuracy. ;o)
		PRECISION=4
	fi

	printf "%.${PRECISION}f\n" "$(( NUMBER**(1.0/NTHROOT) ))"
}

# SQUARE ROOT of a positive floating point number.
# Called as:
# Sqrt NUMBER [PRECISION]
# ALL POSITIVE VALUES.
# NUMBER can be floating point, optional PRECISION is an integer from 1 to 16.
Sqrt()
{
	# Make these local.
	typeset NUMBER var
	typeset NTHROOT var
	typeset PRECISION var

	NUMBER=$1
	NTHROOT=2.0
	PRECISION=$2

	NthRoot $NUMBER $NTHROOT $PRECISION
}

# SINE(angle) in degrees.
# Called as:
# Sin some_angle
# Google 'sin X degrees' values for first quadrant.
#
# 0	0.00000000000
# 15	0.25881904510
# 30	0.50000000000
# 45	0.70710678118
# 60	0.86602540378
# 75	0.96592582628
# 90	1.00000000000
#
# Courtesy of Corona688 translated and improved considerably from shell code I wrote.
Sin()
{
	# Make these local.
	typeset ANGLE var
	typeset SIN var

	# ANGLE used instead of RADIANS for consistency in accuracy.
	ANGLE="$1"
	SIN=0.0

	# Compensate for angles less the 0.0 degrees.
	while (( ANGLE<0.0 ))
	do
		(( ANGLE+=360.0 ))
	done

	# Ditto for angles greater than 360.0 degrees.
	while (( ANGLE>=360.0 ))
	do
		(( ANGLE-=360.0 ))
	done

	# Correct for the four quadrants.
	(( ANGLE>270.0 )) && (( ANGLE-=360 ))
	(( ANGLE>180.0 )) && ANGLE=$(( -(ANGLE-180) ))
	(( ANGLE>90.0 )) && ANGLE=$(( 180-ANGLE ))

	# Now convert to RADIANS.
	(( ANGLE*=TORADIANS ))

	# This loop is to calculate this Taylor series:
	# RADIAN-((RADIAN**3)/6.0)+((RADIAN**5)/120.0)-((RADIAN**7)/5040.0)+((RADIAN**9)/362880.0)-((RADIAN**11)/39916800.0)+((RADIAN**13)/6227020800.0) ... etc.
	# Factorial part 'F'.
	F=1
	for (( N=1; N<=13; ))
	do
		(( SIN+=(ANGLE**N)/F ))
		(( F*=++N ))
		(( F*=-(++N) ))
	done

	# Precision returned to 8 Decimal places.
	printf "%.8f\n" "$SIN"
}

# COSINE(angle) in degrees.
# Called as:
# Cos some_angle
Cos()
{
	# Make this local.
	typeset COS var

	COS=$(( $1 + 90.0 ))
	# Call the 'Sin()' function.
	Sin $COS
}

# ****************************************************
#
# All of below will be removed when finished.
# Test degrees.
echo ""
echo "SINE and COSINE of an ANGLE:"
echo ""
for x in $( seq 0.0 15.0 360.0 )
do
	MYSIN=$( Sin x )
	printf "%s" "SIN $x -> $MYSIN"
	MYCOS=$( Cos x )
	printf "%s\n" "		COS $x -> $MYCOS"
done

# Tests nth roots...
SQRT=$( Sqrt 813.7173 10 )
echo ""
echo "Floating Point Square Root:"
echo "KSH93 FP maths routines = $SQRT."
echo "From Google calculator = 28.5257304902."
echo ""
echo "Floating Point Number And Nth Root:"
NTHROOT=$( NthRoot 10.5 12.3 11 )
echo "KSH93 FP maths routines = $NTHROOT."
echo "From Google calculator = 1.21066369814."
echo ""
#
# ****************************************************

Results on OSX 10.13.6, default bash terminal calling ksh.
Note the code is sourced into current ksh shell...
Code:
Last login: Sat Oct 27 12:49:46 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ksh
AMIGA:uw> . ./ksh_math.sh

SINE and COSINE of an ANGLE:

SIN 0 -> 0.00000000		COS 0 -> 1.00000000
SIN 15 -> 0.25881905		COS 15 -> 0.96592583
SIN 30 -> 0.50000000		COS 30 -> 0.86602540
SIN 45 -> 0.70710678		COS 45 -> 0.70710678
SIN 60 -> 0.86602540		COS 60 -> 0.50000000
SIN 75 -> 0.96592583		COS 75 -> 0.25881905
SIN 90 -> 1.00000000		COS 90 -> 0.00000000
SIN 105 -> 0.96592583		COS 105 -> -0.25881905
SIN 120 -> 0.86602540		COS 120 -> -0.50000000
SIN 135 -> 0.70710678		COS 135 -> -0.70710678
SIN 150 -> 0.50000000		COS 150 -> -0.86602540
SIN 165 -> 0.25881905		COS 165 -> -0.96592583
SIN 180 -> 0.00000000		COS 180 -> -1.00000000
SIN 195 -> -0.25881905		COS 195 -> -0.96592583
SIN 210 -> -0.50000000		COS 210 -> -0.86602540
SIN 225 -> -0.70710678		COS 225 -> -0.70710678
SIN 240 -> -0.86602540		COS 240 -> -0.50000000
SIN 255 -> -0.96592583		COS 255 -> -0.25881905
SIN 270 -> -1.00000000		COS 270 -> 0.00000000
SIN 285 -> -0.96592583		COS 285 -> 0.25881905
SIN 300 -> -0.86602540		COS 300 -> 0.50000000
SIN 315 -> -0.70710678		COS 315 -> 0.70710678
SIN 330 -> -0.50000000		COS 330 -> 0.86602540
SIN 345 -> -0.25881905		COS 345 -> 0.96592583
SIN 360 -> 0.00000000		COS 360 -> 1.00000000

Floating Point Square Root:
KSH93 FP maths routines = 28.5257304902.
From Google calculator = 28.5257304902.

Floating Point Number And Nth Root:
KSH93 FP maths routines = 1.21066369814.
From Google calculator = 1.21066369814.

AMIGA:uw> Sin 45.1
0.70833984
AMIGA:uw> Sin 45.0
0.70710678
AMIGA:uw> Sin 44.9
0.70587157
AMIGA:uw> Cos 45.1
0.70587157
AMIGA:uw> Cos 45.0
0.70710678
AMIGA:uw> Cos 44.9
0.70833984
AMIGA:uw> Sqrt 10
3.1623
AMIGA:uw> Sqrt 10 10
3.1622776602
AMIGA:uw> NthRoot 9113.762 3.12   
18.5838
AMIGA:uw> NthRoot 9113.762 3.12 11
18.58377218033
AMIGA:uw> echo $(( $(Sqrt 10 16)*$(Sqrt 10 16) ))
10.0000000000000011
AMIGA:uw> x=$( NthRoot 9138052765.123 6.711 16 )
AMIGA:uw> echo $x
30.4970432230686335
AMIGA:uw> echo $(( x**6.711 ))
9138052765.12300491
AMIGA:uw> _

Hope you are all enjoying this exercise in futility... ;oD
This User Gave Thanks to wisecracker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BC calculation for floating (invalid arithmetic operator )

Hi, I wish to compare the CPU LOAD 1 min with 5mins and 15mins. If 1 min's CPU LOAd spike 3% compare to 5 mins or 15 mins CPU Load, it is warning. If 1 min's CPU LOAd spike 5% compare to 5 mins or 15 mins CPU Load, it is critical. However I received following code error, I google it and... (10 Replies)
Discussion started by: alvintiow
10 Replies

2. Shell Programming and Scripting

floating point arithmetic operation error

I am writing a script in zsh shell, it fetchs a number from a file using the awk command, store it as a variable, which in my case is a small number 0.62000. I want to change this number by multiplying it by 1000 to become 620.0 using the command in the script var2=$((var1*1000)) trouble is... (2 Replies)
Discussion started by: piynik
2 Replies

3. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

4. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

5. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

6. Shell Programming and Scripting

how to compare 2 floating point no.

Hi, Could any one tell me how to compare to floating point no. using test command. As -eq option works on only intergers. i=5.4 if then echo "equal" else echo "not equal" fi here output will be equal even though no. are unequal. Thanks, ravi (1 Reply)
Discussion started by: useless79
1 Replies

7. Programming

Floating point Emulator

what is floating point emulator(FPE)? where and why it is used? (1 Reply)
Discussion started by: pgmfourms
1 Replies

8. Linux

Floating Point Exception

Hi, I am compiling "HelloWorld" C progam on 32-bit CentOS and i want to execute it on 64-bit CentOS architecture. For that i copied the a.out file from 32-bit to 64-bit machine, but while executing a.out file on 64bit machine I am getting "Floating point exception error". But we can run... (3 Replies)
Discussion started by: Mandar123
3 Replies

9. Shell Programming and Scripting

Rounding off the value of Floating point value

Hello, i have some variables say: x=1.4 y=3.7 I wish to round off these values to : x = 2 (after rounding off) y = 4 (after rounding off) I am stuck. Please help. (7 Replies)
Discussion started by: damansingh
7 Replies

10. Programming

floating point problem

Hi all! Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next... (4 Replies)
Discussion started by: vijlak
4 Replies
Login or Register to Ask a Question