DSPRelated.com
Forums

Pi approximation games

Started by Tim Wescott May 1, 2012
John Devereux  <john@devereux.me.uk> wrote:

>John S <Sophi.2@invalid.org> writes:
>> I like the idea that both 22 and 7 each fit into a byte whereas 355 >> does not. And, 22/7 is hi by only .04%. Beautiful! > >We had a teacher that insisted it was exactly equal!
I recall the time when you couldn't consider 25.4 mm to be exactly one inch. But, they fudged enough standards so that it is now exact. Prior to that, neither the British inch nor the American inch measures 25.4 ... and they deviated from that value in opposite direction! S.
John Larkin <jlarkin@highlandtechnology.com> writes:

> On Tue, 01 May 2012 18:16:25 -0500, Tim Wescott <tim@seemywebsite.com> > wrote: > >>Instead of doing productive work, I just spent a few enjoyable minutes >>with Scilab finding approximations to pi of the form m/n. >> >>Because I'm posting to a couple of nerd groups, I can be confident that >>most of you probably know 22/7 off the tops of your heads. >> >>What interested me is how spotty things are -- after 22/7, the error >>drops for a bit until you get down to 355/113 (which, if you're at an >>equal level of nerdiness to me will ring a bell, but not have been >>swimming around in your brain to be found). >> >>But what's _really_ interesting, is that the next better fit isn't found >>until you get up to 52163/16604. Then things get steadily better until >>you hit 104348/33215 -- at which point the next lowest ratio which >>improves anything is 208341/66317, then 312689/99532. At this point I >>decided that I would post my answers for your amusement, and get back to >>being productive. >> >>Discrete math is so fun. And these newfangled chips are just destroying >>the joy, by making floating point efficient and cheap enough that you >>don't need to know little tricks like pi = (almost) 355/113. > > My old HP35 calculators have a key for pi. The newer ones hide it, a > tiny pastel shift key thing. So I just key in 3.14. Rob down the hall > uses 3. > > We are increasingly using floats in embedded stuff. Our ARM LPC3250 > has SIMD hardware FP operations.
Even the integer-only cortex M3s we use take less than a microsecond for most things. On a 72MHz STM32F2: Double Precision: 0.415us / 49.852 cycles /multiply 0.378us / 45.403 cycles /add 2.414us / 289.702 cycles /divide Single Precision: 0.194us / 23.350 cycles /multiply 0.250us / 30.052 cycles /add 0.610us / 73.202 cycles / divide -- John Devereux
spope33@speedymail.org (Steve Pope) writes:

> John Devereux <john@devereux.me.uk> wrote: > >>John S <Sophi.2@invalid.org> writes: > >>> I like the idea that both 22 and 7 each fit into a byte whereas 355 >>> does not. And, 22/7 is hi by only .04%. Beautiful! >> >>We had a teacher that insisted it was exactly equal! > > I recall the time when you couldn't consider 25.4 mm to be exactly > one inch. But, they fudged enough standards so that it is now exact.
Aha, good idea, we should standardize pi to a more convenient value! :) (Didn't they already try that?)
> Prior to that, neither the British inch nor the American inch > measures 25.4 ... and they deviated from that value in opposite > direction!
I suppose the american inch was bigger... -- John Devereux
On 02/05/2012 00:16, Tim Wescott wrote:
> Instead of doing productive work, I just spent a few enjoyable minutes > with Scilab finding approximations to pi of the form m/n. > > Because I'm posting to a couple of nerd groups, I can be confident that > most of you probably know 22/7 off the tops of your heads. > > What interested me is how spotty things are -- after 22/7, the error > drops for a bit until you get down to 355/113 (which, if you're at an > equal level of nerdiness to me will ring a bell, but not have been > swimming around in your brain to be found). > > But what's _really_ interesting, is that the next better fit isn't found > until you get up to 52163/16604. Then things get steadily better until > you hit 104348/33215 -- at which point the next lowest ratio which > improves anything is 208341/66317, then 312689/99532. At this point I > decided that I would post my answers for your amusement, and get back to > being productive. > > Discrete math is so fun. And these newfangled chips are just destroying > the joy, by making floating point efficient and cheap enough that you > don't need to know little tricks like pi = (almost) 355/113.
As a physicist I found the classic approximations pi^2 = g pi x 10^7 seconds in a year quite handy to within 1% slide rule accuracy -- Regards, Martin Brown
On 02/05/2012 01:59, Nico Coesel wrote:
> Tim Wescott<tim@seemywebsite.com> wrote: > >> Discrete math is so fun. And these newfangled chips are just destroying >> the joy, by making floating point efficient and cheap enough that you >> don't need to know little tricks like pi = (almost) 355/113. > > You can always declare a constant or use pi=4*arctan(1) although I > seldomly see the latter.
Dunno what it is like now, but arctan(1) was a risky choice on some old machines as the series convergence was at its worst for that argument value and the tradeoff between accuracy and speed could cause trouble. You were at the mercy of the trig library if you did it this way. -- Regards, Martin Brown
On 02/05/2012 01:16, Tim Wescott wrote:
> Instead of doing productive work, I just spent a few enjoyable minutes > with Scilab finding approximations to pi of the form m/n. > > Because I'm posting to a couple of nerd groups, I can be confident that > most of you probably know 22/7 off the tops of your heads. > > What interested me is how spotty things are -- after 22/7, the error > drops for a bit until you get down to 355/113 (which, if you're at an > equal level of nerdiness to me will ring a bell, but not have been > swimming around in your brain to be found). > > But what's _really_ interesting, is that the next better fit isn't found > until you get up to 52163/16604. Then things get steadily better until > you hit 104348/33215 -- at which point the next lowest ratio which > improves anything is 208341/66317, then 312689/99532. At this point I > decided that I would post my answers for your amusement, and get back to > being productive. > > Discrete math is so fun. And these newfangled chips are just destroying > the joy, by making floating point efficient and cheap enough that you > don't need to know little tricks like pi = (almost) 355/113. >
Wikipedia is often a great starting point for these sorts of things. It typically has enough information to give you some hints - but not so much that you can't have fun finding out more: <http://en.wikipedia.org/wiki/Pi#Continued_fractions> At university I remember a project that involved calculating all the digits of pi. It was written using a functional programming language (similar to Haskell) - the result was an unending list of the digits of pi. But since the language used lazy evaluation, it didn't bother calculating the entries until you tried to print them out. I used polynomial expansions of arctan() to do the sums.
spope33@speedymail.org (Steve Pope) wrote:

>John Devereux <john@devereux.me.uk> wrote: > >>John S <Sophi.2@invalid.org> writes: > >>> I like the idea that both 22 and 7 each fit into a byte whereas 355 >>> does not. And, 22/7 is hi by only .04%. Beautiful! >> >>We had a teacher that insisted it was exactly equal! > >I recall the time when you couldn't consider 25.4 mm to be exactly >one inch. But, they fudged enough standards so that it is now exact. > >Prior to that, neither the British inch nor the American inch >measures 25.4 ... and they deviated from that value in opposite >direction!
So you are saying the inch is in fact metric? -- Failure does not prove something is impossible, failure simply indicates you are not using the right tools... nico@nctdevpuntnl (punt=.) --------------------------------------------------------------

David Brown schrieb:

> At university I remember a project that involved calculating all the > digits of pi.
Hello, it is known since centuries that calculating all the digits of pi is not possible. Pi has an infinite number of digits. Bye
"John Devereux" <john@devereux.me.uk> wrote in message 
news:87fwbjozdw.fsf@devereux.me.uk...
> spope33@speedymail.org (Steve Pope) writes: > >> John Devereux <john@devereux.me.uk> wrote: >> >>>John S <Sophi.2@invalid.org> writes: >> >>>> I like the idea that both 22 and 7 each fit into a byte whereas 355 >>>> does not. And, 22/7 is hi by only .04%. Beautiful! >>> >>>We had a teacher that insisted it was exactly equal! >> >> I recall the time when you couldn't consider 25.4 mm to be exactly >> one inch. But, they fudged enough standards so that it is now exact. > > Aha, good idea, we should standardize pi to a more convenient value! :) > > (Didn't they already try that?) > >> Prior to that, neither the British inch nor the American inch >> measures 25.4 ... and they deviated from that value in opposite >> direction! > > I suppose the american inch was bigger... > > John Devereux
Boasting again? ;)
On 02/05/2012 11:21, Nico Coesel wrote:
> spope33@speedymail.org (Steve Pope) wrote: > >> John Devereux<john@devereux.me.uk> wrote: >> >>> John S<Sophi.2@invalid.org> writes: >> >>>> I like the idea that both 22 and 7 each fit into a byte whereas 355 >>>> does not. And, 22/7 is hi by only .04%. Beautiful! >>> >>> We had a teacher that insisted it was exactly equal! >> >> I recall the time when you couldn't consider 25.4 mm to be exactly >> one inch. But, they fudged enough standards so that it is now exact. >> >> Prior to that, neither the British inch nor the American inch >> measures 25.4 ... and they deviated from that value in opposite >> direction! > > So you are saying the inch is in fact metric? >
Most imperial units are defined in terms of the metric units these days. Originally they were only rough definitions (I believe an inch was variously defined as the length from a thumb joint to the end of the thumb, or alternatively as the length of three grains of barley). Then they were a bit more standardised (such as the length of a particular metal rod). But now they use specific metric definitions - so an inch is precisely 25.4 mm - and will stay that way even if the definition of a millimetre varies!