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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-27-2012, 04:12 PM
|
#1
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Rep:
|
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?
|
|
|
06-27-2012, 04:20 PM
|
#2
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
06-27-2012, 04:28 PM
|
#3
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep:
|
It seems to be a blend of parentheses and order...
100*1/3 is fine, 100*(1/3) is not.
Weirdness.
|
|
|
06-27-2012, 04:30 PM
|
#4
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
06-27-2012, 05:30 PM
|
#5
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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'
|
|
|
All times are GMT -5. The time now is 05:58 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|