3 ms·
I was surprised to see this in the article: $ pi=$(echo "scale=8; 4*a(1)" | bc -l) $ echo $pi 3.14159264 What? How did the last digit end up as '4'? Tha
by jeffparsons 5y ago
I was surprised to see this in the article:
$ pi=$(echo "scale=8; 4*a(1)" | bc -l)
$ echo $pi
3.14159264
What? How did the last digit end up as '4'? That's no kind of rounding I've ever heard of!
So I ran `bc` on my machine and tried:
[jeff@prism:~/path]$ bc -l
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=8;
4*a(1)
3.14159264
scale=16;
4*a(1)
3.1415926535897932
I guess you could say "learn bc before using it", but even after a quick skim of the man page, this is not what I would expect.
- saurik 5y agoSo I don't know what a(1) is supposed to represent (and am purposefully not looking it up before thinking about this due to your comment about the man page) but presumably it is pi/4 (as 4 times that is pi). If you take pi/4 and round it to 8 decimal digits of precision you get 0.78539816. If you multiply that by 4 (and round it to 8 decimal digits of precision, though this result already has that precision) you get 3.14159264. I guess you are assuming intermediate results somehow have tons of excess precision? (edit: After writing this I checked the man page and a is apparently arctangent.)
- schoen 5y agoa(1) here is the arctangent of 1. I hadn't thought about when bc does its truncation with the scale variable, but your explanation appears to be exactly right. With scale=8, a(1) returns .78539816, which is already truncated to 8 digits. There is some discussion on intermediate result truncation (not exactly in those terms) in the "EXPRESSIONS" section of the bc(1) man page. It notes that "Every expression has a scale", and elsewhere the man page notes that "All numbers are represented internally in decimal and all computation is done in decimal".
- patrec 5y ago> What? How did the last digit end up as '4'? That's no kind of rounding I've ever heard of! Have you memorized pi to nine significant places, or how did you even notice? BC implements arbitrary precision arithmetic with decimals (try echo 'scale=10000; 4*a(1)' | bc -l and make yourself a cup of coffee), so it's not that surprising that it will give you an incorrect final digit. To guarantee correct rounding of the last displayed digit you need to be able compute to arbitrary precision in the general case, I don't think bc does that and that scale does not just govern number of (trailing) digits displayed but also also precision of computation.
- brongondwana 5y ago> Have you memorized pi to nine significant places, or how did you even notice? Not the OP - but, yes. My memory runs to 3.1415926535 and gets a little fuzzy after that, I think it's 2949 or something without looking... hrm, nope; 8979. Good thing I wasn't sure. Definitely as much as any reasonable human scale thing and then some, but memorising a calculator screen full of Pi isn't that impressive. It used to be written above the stairwell in the maths dept at Uni, so I walked past it multiple times per week for years.
- Sharlin 5y ago> Have you memorized pi to nine significant places, or how did you even notice? At least my "go-to" approximation is 3.141592654 due to the nice descending 654 at the end.
- patrec 5y agoCan you explain how this "go-to approximation" is ever useful to you? I don't understand why anyone would memorize pi to more than 3 digits, other than by accident (such as the sibling, who just saw it on a campus wall countless times). Those 3 digits are good for ~0.05% error, plenty good enough for any mental calculation you might want to do. In any other context you would have pi trivially available as a constant anyway, right?
- Sharlin 5y agoMaybe "go-to" is the wrong word. It's just that those are the digits I remember off the top of my head. I don't remember whether I made an actual effort to memorize them as a kid, it's been over twenty years. But they stuck and are not easy to forget now.
- patrec 5y agoCool, makes sense, but I'd put that under the accidentally memorized camp :)