Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/rc/maths.rc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


# usage . /bin/maths.rc
# echo `{add 1 2 3 `{mul 1.3 `{sqrt `{div 89 45}}}

scale = 3

fn add {
	{
		echo scale'='$scale
		echo $* | sed 's/ / + /g'
	} | bc
}

fn sub {
	{
		echo scale'='$scale
		echo $* | sed 's/ / - /g'
	} | bc
}

fn mul {
	{
		echo scale'='$scale
		echo $* | sed 's/ / * /g'
	} | bc
}

fn div {
	{
		echo scale'='$scale
		echo $* | sed 's/ / \/ /g'
	} | bc
}

fn mod  {
	{
		echo scale'='1
		echo $* | sed 's/ / % /g'
	} | bc
}

fn pow {
	{
		echo scale'='$scale
		echo $1 '^' $2
	} | bc
}

fn exp {
	{
		echo 'scale = 20
	          define e(x) {
	               auto a, b, c, i, s
	               a = 1
	               b = 1
	               s = 1
	               for(i=1; 1; i++) {
	                    a *= x
	                    b *= i
	                    c = a/b
	                    if(c == 0) return s
	                    s += c
	               }
	          }'
		echo 'e('$1')'
	} | bc
}


fn floor {
	{
		echo scale'='0
		echo $1 '/1' 
	} | bc
}
		
fn ceil {
	echo '
scale=0
b = '$1' / 1
if (b != '$1') b++
print  b
' | bc -s
}

fn sqrt {
	{
		echo scale'='0
		echo 'sqrt(' $1 ')' 
	} | bc
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].