Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bc is ignoring scale option

Tags:

linux

bash

bc

I can't figure out why bc tool sometimes ignores the scale option.

Here is an example:

> echo 'scale=2; 2.777 - 1.4744' | bc 1.3026 

Expected result is:

1.30 

Additional information:

> bash --version GNU bash, version 2.05b.0(1)-release (x86_64-suse-linux) Copyright (C) 2002 Free Software Foundation, Inc. > bc --version bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. 
like image 385
dabest1 Avatar asked Dec 20 '12 00:12

dabest1


People also ask

What is scale in bc Linux?

scale defines how some operations use digits after the decimal point. The default value of scale is 0. ibase and obase define the conversion base for input and output numbers. The default for both input and output is base 10. last (an extension) is a variable with the value of the last printed number.

How do I exit bc?

Press ctrl+d will work to exit the program.

What is bc command?

The bc command is an interactive process that provides arbitrary-precision arithmetic. The bc command first reads any input files specified by the File parameter and then reads the standard input.


1 Answers

as Carl pointed out, if you check man page, you can find that line. it is about expression explanations. subtraction won't read scale variable. If you want to get the expected result (1.30), you could:

kent$  echo 'scale=2; (2.777 - 1.4744)/1' | bc  1.30 

/ operation will read scale variable.

like image 128
Kent Avatar answered Sep 21 '22 15:09

Kent