Post Reply 
Small challenge
04-23-2023, 11:10 AM (This post was last modified: 04-23-2023 11:15 AM by robve.)
Post: #16
RE: Small challenge
(04-23-2023 08:00 AM)EdS2 Wrote:  There must be several ways to compute x^10:
raise to power ten
raise to power 5 then square
square then raise to power 5
and in each case, do this using repeated multiplication, or by the power operator.

How differently accurate are each of those 6 ways on the machines considered?

Hang on, it's more than 6 - the squares and fifth powers can also be done in at least two ways, perhaps even three ways if x^2 is available.

(And there's also the less-accurate option of using log and exp and multiplication.)

Indeed! It is common to find exponentiation by squaring for integer powers, which tends to be more accurate than repeated multiplication and log*exp closed forms.

It's pretty straightforward to do. Here is the way I've implemented it in Forth850 MATH.FTH:

Code:
: F**       ( r1 r2 -- r3 )
  \ r1 and r2 cannot be both zero
  2DUP F0= IF 2OVER F0= IF -46 THROW THEN THEN
  2DUP 2DUP FTRUNC F= IF
    \ exponentiation by squaring
    2DUP F0< >R \ r2 is negative
    FABS F>D 1E0 2SWAP \ -- r1 1 ud
    IF -46 THROW THEN \ error when exponent ud exceeds 16 bits
    >R
    BEGIN
      R@ 1 AND IF 2OVER F* THEN
      R> 1 RSHIFT \ -- r1^n product u>>1
    DUP WHILE
      >R
      2SWAP 2DUP F* 2SWAP \ -- r1^n^2 product u>>1
    REPEAT
    DROP 2SWAP 2DROP \ -- product
    R> IF 1E0 2SWAP F/ THEN \ r2 was negative
  ELSE
    F^ \ performs 2SWAP FLN F* FEXP ;
  THEN ;

x^10 is broken down to three squarings and one multiplication \( \left(x\left((x^2)^2\right)\right)^2 \) hence 4 multiplications suffice.

- Rob

"I count on old friends to remain rational"
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Small challenge - J-F Garnier - 04-22-2023, 02:33 PM
RE: Small challenge - Valentin Albillo - 04-22-2023, 03:29 PM
RE: Small challenge - John Keith - 04-22-2023, 04:38 PM
RE: Small challenge - Massimo Gnerucci - 04-22-2023, 03:33 PM
RE: Small challenge - Valentin Albillo - 04-22-2023, 03:41 PM
RE: Small challenge - J-F Garnier - 04-22-2023, 03:41 PM
RE: Small challenge - Gerson W. Barbosa - 04-22-2023, 05:15 PM
RE: Small challenge - BruceH - 04-22-2023, 04:30 PM
RE: Small challenge - Gerson W. Barbosa - 04-22-2023, 05:29 PM
RE: Small challenge - Gerson W. Barbosa - 04-28-2023, 12:52 AM
RE: Small challenge - J-F Garnier - 04-28-2023, 07:13 AM
RE: Small challenge - J-F Garnier - 05-16-2023, 06:57 PM
RE: Small challenge - robve - 05-18-2023, 03:16 AM
RE: Small challenge - C.Ret - 04-22-2023, 06:30 PM
RE: Small challenge - Thomas Klemm - 04-22-2023, 07:24 PM
RE: Small challenge - J-F Garnier - 04-22-2023, 09:42 PM
RE: Small challenge - Guenter Schink - 04-25-2023, 09:56 PM
RE: Small challenge - John Keith - 04-25-2023, 11:46 PM
RE: Small challenge - Dave Britten - 04-27-2023, 02:30 PM
RE: Small challenge - Valentin Albillo - 04-23-2023, 12:58 AM
RE: Small challenge - C.Ret - 04-23-2023, 06:24 AM
RE: Small challenge - EdS2 - 04-23-2023, 08:00 AM
RE: Small challenge - robve - 04-23-2023 11:10 AM
RE: Small challenge - robve - 04-23-2023, 01:01 PM
RE: Small challenge - robve - 04-23-2023, 01:56 PM
RE: Small challenge - EdS2 - 04-23-2023, 02:08 PM
RE: Small challenge - J-F Garnier - 04-23-2023, 02:13 PM
RE: Small challenge - John Keith - 04-23-2023, 06:41 PM
RE: Small challenge - J-F Garnier - 04-24-2023, 10:11 AM
RE: Small challenge - Albert Chan - 04-24-2023, 12:58 PM
RE: Small challenge - brouhaha - 04-24-2023, 05:32 PM
RE: Small challenge - Albert Chan - 04-24-2023, 01:07 PM
RE: Small challenge - robve - 04-28-2023, 08:37 PM
RE: Small challenge - J-F Garnier - 04-24-2023, 01:35 PM
RE: Small challenge - John Keith - 04-24-2023, 06:54 PM
RE: Small challenge - Christoph Giesselink - 04-25-2023, 07:13 PM
RE: Small challenge - J-F Garnier - 04-25-2023, 08:49 PM
RE: Small challenge - J-F Garnier - 04-26-2023, 07:51 AM
RE: Small challenge - J-F Garnier - 04-27-2023, 07:31 PM
RE: Small challenge - EdS2 - 04-28-2023, 08:53 AM



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