bc "scale" question.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bc "scale" question.
# 1  
Old 01-25-2012
bc "scale" question.

Can someone explain to me please why the scale=3 fails to give me an answer to 3 decimal places in the first example of calling bc below but succeeds in the second?

I know I can use a printf modifier to get the answer to 3 decimal places but I'd like to understand what's going on with bc.

Thanks.

Code:
start=$(date +%s.%N)
sleep 1
stop=$(date +%s.%N)

timeTaken=$(echo "scale=3; $stop - $start" | bc)
echo "timeTaken = $timeTaken"
# timeTaken = 1.004179356

num1="100"
num2="3"
result=$(echo "scale=3; $num1 / $num2" | bc)
echo "result = $result"
# result = 33.333

# 2  
Old 01-25-2012
scale is implementation dependent, meaning: it affects "some" operations. Algorithms also look at length - the number of significant digits in the operands.

That is why 10 /3 got scaled and the other one did not.

bc was/is a front end for another program, dc.
This was true in earlier versions of HPUX and BSD. The source (dc and bc @Bell Labs) worked that way.

Although bc is "POSIX" there are NO standards about how to implement scale or ibase, or obase; just slightly what bc -l entails, and not even what functions or values are declared. The only positive standard is bc -l sets scale to default 20.

Not much help, huh.
# 3  
Old 01-25-2012
Sometimes you have to do a pointless calculation to get "bc" to round.
In this example I am dividing by one:

Code:
timeTaken=$(echo "scale=3; ($stop - $start)/1" | bc)

Try it. It works on my version of "bc".
# 4  
Old 01-26-2012
Thanks for the information guys.

Methyl - dividing by 1 also results in the answer being given to 3 decimal places with my version of bc.

Am I correct in thinking that for portability people just have to assume that bc's scale (to reduce precision) does not actually work, so if a result needs to be given at a certain (reduced) precision they have to use a printf modifier or such like?

Jim - when you say bc is "POSIX" does that mean there is a list of programs that are guaranteed to be on POSIX compliant OSs? If so where would I find the list? I just did a search and couldn't find anything. Or are the "" around POSIX meant to say - strictly unofficial? Is there an unofficial list?

Cheers.
# 5  
Old 01-26-2012
Quote:
Originally Posted by gencon
[...]
Jim - when you say bc is "POSIX" does that mean there is a list of programs that are guaranteed to be on POSIX compliant OSs? If so where would I find the list? I just did a search and couldn't find anything. Or are the "" around POSIX meant to say - strictly unofficial? Is there an unofficial list?
Yes: bc
# 6  
Old 01-26-2012
Quote:
Originally Posted by radoulov
Yes: bc
Thanks Radoulov.

Would you guys say that all OS's which fall in the mostly POSIX-compliant category (Eg. Linux, OpenBSD, etc.) have all of those utilities installed? So it is not necessary when writing a bash script to have code that checks if bc, sed, tr, grep, and so on are installed on the system?

Cheers.

PS. Link to all the POSIX-compliant utilities, scroll down to 'Utilities': Shell & Utilities: Table of Contents
# 7  
Old 01-26-2012
Quote:
Originally Posted by gencon
Thanks Radoulov.

Would you guys say that all OS's which fall in the mostly POSIX-compliant category (Eg. Linux, OpenBSD, etc.)
have all of those utilities installed? So it is not necessary when writing a bash script to have code that checks if bc,
sed, tr, grep, and so on are installed on the system?
[...]
Yes, unless the system has been modified ...
Be aware though that most operating systems usually provide more than one implementation of those utilities
(for example POSIX grep vs. standard Solaris grep).
So in order to access the POSIX versions of the above mentioned programs
on some systems (Solaris comes to mind), you'll need to set your PATH:

Code:
PATH=`getconf PATH`:$PATH export PATH

or (if you're already in a POSIX compliant shell):

Code:
PATH=$(getconf PATH):$PATH export PATH

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Question about special variables: "-" and "$_"

both ksh/bash support this 2 special variables, Is there any document for reference? 1) "-" is $OLDPWD 2) "$_" is last argument of previous command. (4 Replies)
Discussion started by: honglus
4 Replies

6. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

7. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question