home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / hp48 / 6416 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  2.2 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!gateway
  2. From: FELSON_ADJ@CCSUA.CTSTATEU.EDU
  3. Newsgroups: comp.sys.hp48
  4. Subject: Re: Power of 2
  5. Date: 19 Dec 1992 19:22:42 -0600
  6. Organization: UTexas Mail-to-News Gateway
  7. Lines: 49
  8. Sender: daemon@cs.utexas.edu
  9. Message-ID: <921219202242.26018622@CCSUA.CTSTATEU.EDU>
  10. NNTP-Posting-Host: cs.utexas.edu
  11.  
  12. ! From: kcooke@stein.u.washington.edu (Kenneth E. Cooke)
  13. ! akcs.joehorn@hpcvbbs.cv.hp.com (Joseph K. Horn) writes:
  14. ! >flynn@sabrina.dei.unipd.it [Salvatore A. Colomba] writes:
  15. ! >> I have a better one [to test for a power of 2]; I've not tested it,
  16. ! >> but it should work in principle...
  17. ! >> << DUP LN 2 LN / IP 2 ^ - >>
  18. ! >> It should give zero only if the number is EXACTLY a power of
  19. ! >> two...:)
  20. ! >Doesn't work at all.  I assume you meant
  21. ! >  << DUP LN 2 LN / IP 2 SWAP ^ - >>
  22. ! >which usually works, but fails for 2^19, 2^24, 2^29, 2^34, and 2^39.
  23. ! >Replacing the LN's with LOG's fixes all these cases but then fails for
  24. ! >2^6, 2^8, 2^35 and 2^38.
  25. ! >Algorithms which work in principle often fail in practice due to
  26. ! >round-off errors.  Good hypotheses deserve the honor of being tested.
  27. ! >-jkh-
  28. ! Here is a cleaner way to check for a power of 2.  As Joe points out, you 
  29. ! should not depend on "ideal" mathematics when working with a limited-
  30. ! precision machine.  You can sometimes get away with this kind of thing on
  31. ! the HP48, because it has an EXCELLENT implementation of floating-point
  32. ! arithmetic (thanks in part to Prof. Kahan).  My only regret is that they
  33. ! threw out the full IEEE version in the HP71.
  34. ! This is a user-code translation of the power-of-2 check in FFT.  If your
  35. ! binary word-size is N, it will work for 0 to 2^N - 1 (for binary integers).
  36. ! It considers #0 and #1 to be powers of 2.
  37. ! << NOT DUP 1 + OR 1 + #0 == >>
  38. ! The first "1 +" will fill the first 0.  If there was only one 0, the second
  39. ! "1 +" will cause it to roll-over to #0.  Sorry if this makes no sense :)
  40.  
  41.  
  42. Actually the problem was in using IP to get the integer...  numbers like 
  43. 1.9999999999999 will return 1 where 2 is the desired result.  I think
  44. using something like 5 RND instead of IP should do the trick.  Or 2 -
  45. ABS 0.0001 <
  46.  
  47.  
  48. __a__d__a__m__
  49.