floating point arithmetic? - hack job workaround now included

51dusty

Senior Member
Feb 11, 2009
144
13
0
bville, md
is there a way to do floating point arithmetic in terminal?...or would bc binary need to be included since busybox does not have it? as of now you get a syntax error if using fp numbers in expression..or 0 when using division and result is a floating point.

Code:
# echo $(( 1 + 1 ))
echo $(( 1 + 1 ))
2
# echo $(( 1.0 + 1.0 ))
echo $(( 1.0 + 1.0 ))
arith: syntax error: " 1.0 + 1.0 "
# echo $(( 1 / 2 ))
echo $(( 1 / 2 ))
0
 
Last edited:

lbcoder

Senior Member
Jan 21, 2009
2,622
99
0
sh/bash has no native support for floating point math, so your solution must involve a binary executable. You can either use bc, or you can write a very simple C program and compile it for this platform....


i.e.,

Code:
//math.c
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char ** argv){
float a = atof(argv[1]);
char op = argv[2][0];
float b = atof(argv[3]);
if (op == '+') printf("%f\n",a+b);
else if (op == '-') printf("%f\n",a-b);
else if (op == 'x') printf("%f\n",a*b);
else if (op == '/') printf("%f\n",a/b);
return 0;
}
$ ./math 1.5 + 2
3.500000
$ ./math 1.5 x 2
3.000000
$ ./math 1.5 - 2
-0.500000
$ ./math 1.5 / 2
0.750000

Oh and FYI, don't forget you can use variables in there, i.e.
$ A=1.5
$ B=2
$ OP=/
$./math $A $OP $B
0.750000
 

51dusty

Senior Member
Feb 11, 2009
144
13
0
bville, md
sh/bash has no native support for floating point math, so your solution must involve a binary executable. You can either use bc, or you can write a very simple C program and compile it for this platform....


i.e.,

Code:
//math.c
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char ** argv){
float a = atof(argv[1]);
char op = argv[2][0];
float b = atof(argv[3]);
if (op == '+') printf("%f\n",a+b);
else if (op == '-') printf("%f\n",a-b);
else if (op == 'x') printf("%f\n",a*b);
else if (op == '/') printf("%f\n",a/b);
return 0;
}
$ ./math 1.5 + 2
3.500000
$ ./math 1.5 x 2
3.000000
$ ./math 1.5 - 2
-0.500000
$ ./math 1.5 / 2
0.750000

Oh and FYI, don't forget you can use variables in there, i.e.
$ A=1.5
$ B=2
$ OP=/
$./math $A $OP $B
0.750000
thanks for the info. very helpful.
 

51dusty

Senior Member
Feb 11, 2009
144
13
0
bville, md
script workaround

i have constructed a workaround for doing fp math for determining partition sizes.

it's not pretty, but it gets the job done.

basically, it goes like this:
1. check to see if the number passed to the function is an integer
2. if not, i search the string for a "."
3. if the search turns up a "." (and units in GB) i break the number into two whole numbers (the integer portion..before the decimal, and the fraction portion after the decimal).
4. do the appropriate math on the integer section.
5. do the appropriate math on the fraction section, then divide by 10^#of digits after the decimal place.
6. add the two numbers together and voila! a hack job, floating point calculation.

Code:
ValidateSizeArg() {
	# check for zero-length arg to protect expr length
	[ -z "$1" ] && ShowError "zero-length argument passed to size-validator"
		
	SIZEMB=
	ARGLEN=`expr length $1`
	SIZELEN=$(($ARGLEN-1))
	SIZEARG=`expr substr $1 1 $SIZELEN`
	SIZEUNIT=`expr substr $1 $ARGLEN 1`
	
	# check if SIZEARG is an integer
	if [ $SIZEARG -eq $SIZEARG 2> /dev/null ] ; then
		# look for G
		[ "$SIZEUNIT" == "G" ] && SIZEMB=$(($SIZEARG * 1024))
		# look for M
		[ "$SIZEUNIT" == "M" ] && SIZEMB=$SIZEARG
		# no units on arg
		[ -z "$SIZEMB" ] && SIZEMB=$1
	# check if SIZEARG is a floating point number, GB only
	elif [ `expr index "$SIZEARG" .` != 0 ] && [ "$SIZEUNIT" == "G" ]  ; then
		INT=`echo "$SIZEARG" | cut -d"." -f1`
		FRAC=`echo "$SIZEARG" | cut -d"." -f2`
		SIGDIGITS=`expr length $FRAC`
		
		[ -z "$INT" ] && INT=0
		INTMB=$(($INT * 1024))
		FRACMB=$((($FRAC * 1024) / (10**$SIGDIGITS)))
		SIZEMB=$(($INTMB + $FRACMB))
	# it's not a valid size
	else
		ShowError "$1 is not a valid size"
	fi
	
	# return valid argument in MB
	FUNC_RET=$SIZEMB
}
 

jugg2000

Senior Member
Jun 13, 2009
210
0
0
I was a basic/qbasic/gwbasic programmer in my younger days (like 12-14 yrs old)...

I feel ashamed that I have no idea of what this stuff is anymore. Thank God for guys like you.