Skip to content

round function for bc

March 13, 2013

Here there is a function for bc to round a number at the specified decimal digit.
Of course in order to round to be effective, number to be number must have enough significant figures. It is possible to set them via bc ‘scale‘ setting it greather or equal than ‘desidered precision (d in the example below) + 1’

/* d is the desidered precision */
d = 3
/* scale is set to 'desidered precision (d) + 1 */
/* but any 'd + n', n >= 1, would be fine */
scale = d + 1
r = round(16/9,d)

Moreover there are two collateral useful function: int and dec.

The first one returns the integer part of a number. The second one returns the decimal part of a number.
functions.bc

define dec (n) {
	return (n - int(n));
}

define int (n) {
	auto s, r;
	s = scale;
	scale = 0;
	r = n / 1;
	scale = s;
	return (r);
}

define round (n,d) {
	auto s, t, i, f, end;

	s = scale;
	scale = d;
	# this is the decimal part of n after the 'd' decimal digits
	t = dec(n * 10 ^ d);
	n = (n * 10 ^ d - t) / (10 ^ d);

	delta = 0;
	i = 0;
	end = 0;
	while ( (i++ < (length(t) - d)) && (end != 1) ) { 		f = int(t * 10) 		if ( f >= 5 ) {
			delta = 1;
			end = 1
		}
		if ( (end == 0) && (f < 4) ) {
			end = 1;
		}
		if ( end == 0 ) {
			t = t * 10 - f;
		}
	}

	n += (delta / 10 ^ d);

	return (n);
}

Here a test suite that shows how to use too.
test_round.bc

define test_round (n,d,e) {
	auto r
	r = round(n,d);
	if ( r == e ) {
		print "test succeeded: round(",n,",",d,") == ",r,"\n"
	} else {
		print "test failed: round(",n,",",d,") returns ",r," while ",e," was expected\n"
	}
}

x= test_round(1.000,0,1)
x= test_round(1.400,0,1)
x= test_round(1.500,0,2)
x= test_round(1.900,0,2)

x= test_round(1.040,0,1)
x= test_round(1.440,0,1)
x= test_round(1.450,0,2)
x= test_round(1.490,0,2)

x= test_round(1.440,0,1)
x= test_round(1.444,0,1)
x= test_round(1.445,0,2)
x= test_round(1.449,0,2)

x= test_round(1.000,1,1.0)
x= test_round(1.400,1,1.4)
x= test_round(1.500,1,1.5)
x= test_round(1.900,1,1.9)

x= test_round(1.040,1,1.0)
x= test_round(1.440,1,1.4)
x= test_round(1.450,1,1.5)
x= test_round(1.490,1,1.5)

x= test_round(1.440,1,1.4)
x= test_round(1.444,1,1.4)
x= test_round(1.445,1,1.5)
x= test_round(1.449,1,1.5)

x= test_round(1.000,2,1.00)
x= test_round(1.400,2,1.40)
x= test_round(1.500,2,1.50)
x= test_round(1.900,2,1.90)

x= test_round(1.040,2,1.04)
x= test_round(1.440,2,1.44)
x= test_round(1.450,2,1.45)
x= test_round(1.490,2,1.49)

x= test_round(1.440,2,1.44)
x= test_round(1.444,2,1.44)
x= test_round(1.445,2,1.45)
x= test_round(1.449,2,1.45)

x= test_round(1.000,3,1.000)
x= test_round(1.400,3,1.400)
x= test_round(1.500,3,1.500)
x= test_round(1.900,3,1.900)

x= test_round(1.040,3,1.040)
x= test_round(1.440,3,1.440)
x= test_round(1.450,3,1.450)
x= test_round(1.490,3,1.490)

x= test_round(1.440,3,1.440)
x= test_round(1.444,3,1.444)
x= test_round(1.445,3,1.445)
x= test_round(1.449,3,1.449)

x= test_round(1.440,4,1.440)
x= test_round(1.444,4,1.444)
x= test_round(1.445,4,1.445)
x= test_round(1.449,4,1.449)

x= test_round(1,1,1.0)
x= test_round(1,1,1.0)
x= test_round(1,1,1.0)
x= test_round(1,1,1.0)

x= test_round(1,0,1)
x= test_round(1,0,1)
x= test_round(1,0,1)
x= test_round(1,0,1)

quit

Running the tests via ‘bc functions.bc test_round.bc‘ produces the following output.

test succeeded: round(1.000,0) == 1
test succeeded: round(1.400,0) == 1
test succeeded: round(1.500,0) == 2
test succeeded: round(1.900,0) == 2
test succeeded: round(1.040,0) == 1
test succeeded: round(1.440,0) == 1
test succeeded: round(1.450,0) == 2
test succeeded: round(1.490,0) == 2
test succeeded: round(1.440,0) == 1
test succeeded: round(1.444,0) == 1
test succeeded: round(1.445,0) == 2
test succeeded: round(1.449,0) == 2
test succeeded: round(1.000,1) == 1.0
test succeeded: round(1.400,1) == 1.4
test succeeded: round(1.500,1) == 1.5
test succeeded: round(1.900,1) == 1.9
test succeeded: round(1.040,1) == 1.0
test succeeded: round(1.440,1) == 1.4
test succeeded: round(1.450,1) == 1.5
test succeeded: round(1.490,1) == 1.5
test succeeded: round(1.440,1) == 1.4
test succeeded: round(1.444,1) == 1.4
test succeeded: round(1.445,1) == 1.5
test succeeded: round(1.449,1) == 1.5
test succeeded: round(1.000,2) == 1.00
test succeeded: round(1.400,2) == 1.40
test succeeded: round(1.500,2) == 1.50
test succeeded: round(1.900,2) == 1.90
test succeeded: round(1.040,2) == 1.04
test succeeded: round(1.440,2) == 1.44
test succeeded: round(1.450,2) == 1.45
test succeeded: round(1.490,2) == 1.49
test succeeded: round(1.440,2) == 1.44
test succeeded: round(1.444,2) == 1.44
test succeeded: round(1.445,2) == 1.45
test succeeded: round(1.449,2) == 1.45
test succeeded: round(1.000,3) == 1.000
test succeeded: round(1.400,3) == 1.400
test succeeded: round(1.500,3) == 1.500
test succeeded: round(1.900,3) == 1.900
test succeeded: round(1.040,3) == 1.040
test succeeded: round(1.440,3) == 1.440
test succeeded: round(1.450,3) == 1.450
test succeeded: round(1.490,3) == 1.490
test succeeded: round(1.440,3) == 1.440
test succeeded: round(1.444,3) == 1.444
test succeeded: round(1.445,3) == 1.445
test succeeded: round(1.449,3) == 1.449
test succeeded: round(1.440,4) == 1.4400
test succeeded: round(1.444,4) == 1.4440
test succeeded: round(1.445,4) == 1.4450
test succeeded: round(1.449,4) == 1.4490
test succeeded: round(1,1) == 1.0
test succeeded: round(1,1) == 1.0
test succeeded: round(1,1) == 1.0
test succeeded: round(1,1) == 1.0
test succeeded: round(1,0) == 1
test succeeded: round(1,0) == 1
test succeeded: round(1,0) == 1
test succeeded: round(1,0) == 1

From → Technology

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: