Post Reply 
Π day
03-15-2022, 08:49 PM (This post was last modified: 03-16-2022 03:15 AM by robve.)
Post: #22
RE: Π day
(03-15-2022 07:55 PM)Thomas Klemm Wrote:  
(03-15-2022 12:35 AM)robve Wrote:  In addition, I observed that summing the terms in reverse order may sometimes improve accuracy.
That's the point of Wirth's exercise.

Good point. I tried to convey this subtly as a minor contribution to that other thread by just restating a known fact that summing a series with monotonically decreasing magnitude should always be performed in reverse. It's easy to see why when you look at floating point addition that right shifts the mantissa of one of the operands until the exponents align. You want to start with the smallest terms, those who's exponents align most closely to the (initially) small sum.

(03-15-2022 07:55 PM)Thomas Klemm Wrote:  
(03-15-2022 12:35 AM)robve Wrote:  That's far more CPU power than the N=17 steps for the Sharp series to converge to 10 decimal places.

If you really care about CPU cycles you may want to avoid the expensive \(3^n\) calculation which usually uses \(\log\) and \(\exp\) under the hood.

To clarify, a better way is to perform full "strength reduction" as compilers do to optimize loops. Even better, for equidistant grids in one or more dimensions, functions can be more effectively optimized using the Chains of recurrences algebra, see https://dl.acm.org/doi/abs/10.1145/19034...NSgF9Z3eNA and our article https://dl.acm.org/doi/abs/10.1145/13755...aRdGHFiMLQ (our earlier work on the inverse CR algebra is used by the popular GCC compiler to optimize loops.)

After strength reduction we end up with one division, two multiplications, two additions and one negation per loop iteration:

S=0
P=1
X=1
Y=1
FOR I=0 TO N
  S=S+P/(X*Y)
  P=-P
  X=X*3
  Y=Y+2
NEXT I

Note that the first iteration can be stripped:

S=1
P=-1
X=3
Y=3
FOR I=1 TO N
  S=S+P/(X*Y)
  P=-P
  X=X*3
  Y=Y+2
NEXT I

and "operator strength reduction" then gives:

S=1
P=-1
X=3
Y=3
FOR I=1 TO N
  S=S+P/(X*Y)
  P=-P
  X=X+X+X
  Y=Y+2
NEXT I

However, and this is an important point, there is a limit to how aggressive we can be with strength reduction (via Chains of Recurrences) to optimize floating point operations, because of potentially introducing small errors that accumulate over large grids (not a problem when N is small). See https://dl.acm.org/doi/abs/10.1145/38410...6bSQ-bA_eg Error accumulation is not a problem for whole numbers, so \( 3^i \) is always safe to optimize as shown above.

PS. Division is typically the most expensive operation, which should be avoided at all cost, when possible.

PPS. Summing terms in the order of increasing magnitude can be important, but methods such as Kahan summation work generally best. Also binary tree summation is generally better (the proof is in my lecture notes on parallel computing).

- Rob

"I count on old friends" -- HP 71B,Prime|Ti VOY200,Nspire CXII CAS|Casio fx-CG50...|Sharp PC-G850,E500,2500,1500,14xx,13xx,12xx...
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Π day - robve - 03-14-2022, 03:35 AM
RE: Π day - Dave Britten - 03-14-2022, 11:44 AM
RE: Π day - Gerson W. Barbosa - 03-14-2022, 12:25 PM
RE: Π day - robve - 03-14-2022, 05:52 PM
RE: Π day - Dave Britten - 03-14-2022, 06:15 PM
RE: Π day - Gerson W. Barbosa - 03-14-2022, 11:53 AM
RE: Π day - EdS2 - 03-14-2022, 01:55 PM
RE: Π day - Gerson W. Barbosa - 03-14-2022, 06:06 PM
RE: Π day - EdS2 - 03-15-2022, 12:05 PM
RE: Π day - robve - 03-14-2022, 09:35 PM
RE: Π day - Gerson W. Barbosa - 03-14-2022, 10:30 PM
RE: Π day - robve - 03-14-2022, 02:10 PM
RE: Π day - Gerson W. Barbosa - 03-14-2022, 08:29 PM
RE: π day - Thomas Klemm - 03-14-2022, 09:17 PM
RE: Π day - robve - 03-15-2022, 04:56 PM
RE: Π day - ttw - 03-14-2022, 11:06 PM
RE: Π day - robve - 03-15-2022, 12:35 AM
RE: Π day - floppy - 04-02-2022, 11:12 AM
RE: Π day - Eddie W. Shore - 03-15-2022, 01:09 AM
RE: Π day - rprosperi - 03-15-2022, 12:25 PM
RE: Π day - Ren - 03-15-2022, 01:16 AM
RE: π day - Thomas Klemm - 03-15-2022, 07:55 PM
RE: Π day - robve - 03-15-2022 08:49 PM
RE: Π day - Thomas Klemm - 03-17-2022, 03:40 AM
RE: Π day - robve - 03-18-2022, 01:04 AM
RE: Π day - Thomas Klemm - 03-17-2022, 03:54 AM
RE: Π day - Gerson W. Barbosa - 03-17-2022, 11:39 AM
RE: Π day - Thomas Klemm - 03-17-2022, 12:29 PM
RE: Π day - Gerson W. Barbosa - 03-17-2022, 02:10 PM
RE: Π day - Ángel Martin - 03-18-2022, 09:07 AM
RE: Π day - Frido Bohn - 03-19-2022, 09:45 AM
RE: Π day - Ángel Martin - 03-19-2022, 11:17 AM
RE: Π day - Frido Bohn - 03-19-2022, 01:01 PM
RE: Π day - Frido Bohn - 03-19-2022, 03:13 PM
RE: Π day - DavidM - 03-17-2022, 08:25 PM
RE: Π day - Xorand - 03-18-2022, 03:06 AM
RE: Π day - Steve Simpkin - 03-18-2022, 04:31 AM
RE: Π day - MeindertKuipers - 03-18-2022, 10:48 AM
RE: Π day - Ángel Martin - 03-18-2022, 11:04 AM
RE: Π day - Ángel Martin - 03-19-2022, 11:18 AM
RE: Π day - Ren - 04-02-2022, 03:14 AM
RE: Π day - Ángel Martin - 03-20-2022, 07:39 AM
RE: Π day - Frido Bohn - 03-20-2022, 07:28 PM
RE: π day - Thomas Klemm - 03-21-2022, 07:24 AM
RE: Π day - Frido Bohn - 03-21-2022, 04:03 PM
RE: Π day - Albert Chan - 03-21-2022, 10:45 PM
RE: Π day - Gerson W. Barbosa - 03-24-2022, 01:36 AM
RE: Π day - Albert Chan - 03-26-2022, 03:59 PM
RE: Π day - Gerson W. Barbosa - 03-26-2022, 05:37 PM
RE: Π day - Thomas Klemm - 03-21-2022, 05:27 PM
RE: π day - Thomas Klemm - 03-21-2022, 05:54 PM
RE: π day - Thomas Klemm - 03-21-2022, 06:33 PM
RE: Π day - Albert Chan - 03-26-2022, 11:24 PM
RE: Π day - Albert Chan - 03-27-2022, 01:44 PM
RE: Π day - Albert Chan - 03-27-2022, 04:00 PM
RE: Π day - ttw - 03-31-2022, 02:04 AM



User(s) browsing this thread: 1 Guest(s)