LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-27-2012, 04:12 PM   #1
standard_output
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Rep: Reputation: Disabled
BC provides incorrect decimal results, but only on the last two decimal places...


Running RHEL 5.3 on a 2.6.18-128.el5 kernel. It's a VM.
bc v1.06
bash v3.2.25((1)-release (x86-64-redhat-linux-gnu)

My specific issue is getting bc to properly show decimals. I have a program that receives arbitrary numbers and then does percentage calculations based on what it gets - it is never correct, and troubleshooting bc has lead me to these conclusions:

If I do: echo "scale=2; (1/3)*100" | bc -l

I get: 33.00 (obviously incorrect)

If I do: echo "scale=2; (1/3)*101" | bc -l

I get: 33.33 (given rounding, this is close enough)

If I do: echo "scale=4; (1/3)*100" | bc -l

I get: 33.3300 (obviously incorrect, but it is interesting that bc chose to make the first two decimals correct)

I have tried using the -l option, not using the -l option, providing artificial decimal places for all constants in the equation (just in case bc likes to drop decimals when the input doesn't have them) and so on. I've swapped in printf for echo, and manually run the bc program without piping input. bc is consistently wrong in all cases where I use "100" as the multiplier.



What am I doing wrong?
 
Old 06-27-2012, 04:20 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Same results here on Fedora 15, 2.6.43.5, bc 1.06.95

I did notice that if you do 100*1/3 it works fine though:

Code:
$ echo "scale=4; 1/3*100" | bc -l
33.3300
$ echo "scale=4; 100*1/3" | bc -l
33.3333

No idea why, just providing a potential workaround.

Last edited by suicidaleggroll; 06-27-2012 at 04:22 PM.
 
Old 06-27-2012, 04:28 PM   #3
standard_output
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
It seems to be a blend of parentheses and order...

100*1/3 is fine, 100*(1/3) is not.

Weirdness.
 
Old 06-27-2012, 04:30 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
It's probably just order of operations and your scale=4. 1/3 is being computed as .3333 (only four decimal places since scale=4), then when multiplied by 100 you get 33.3300. If you do 100*1/3 it does the 100*1 first, giving you 100.0000, then divides that by 3 giving you the full precision 33.3333.
 
1 members found this post helpful.
Old 06-27-2012, 05:30 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
After more looking, that's definitely the problem. You can see it when multiplying by 101 as well, but it's not as noticeable.

I would remove the scale from your bc and just let it do the full precision computation, then pipe the result to printf for formatting.

Code:
echo "1/3*100" | bc -l | xargs printf '%7.4f\n'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] if statements with decimal places genderbender Programming 11 08-13-2010 12:21 AM
division with bash, WITH decimal places anonguy9 LinuxQuestions.org Member Success Stories 0 03-27-2009 06:03 PM
Math with 2 decimal places newbie_adm Linux - Newbie 1 09-15-2006 12:14 AM
rounding a large double to 2 decimal places linuxmandrake Programming 2 03-17-2006 03:31 PM
how to print 2 decimal places of a double? nadroj Programming 6 07-12-2005 11:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration