Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: Fast and Accurate Trigs (HP12C Platinum)  Revised version. (/thread103573.html) 
Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Gerson W. Barbosa  12032006 Some days ago, while I was concerned about the HP33S trig bug I was warned of a bug in my own program (thanks, Tony!). In the next to last line below the program returned 0.156434566400, that is, a truncation in the last two significant digits. As Tony observed, this was because arccos(x) was computed as 90  arcsin(x). The fixed version uses the formula he has suggested, incidentally the same I had used used in an earlier version. However, the program is now 21 steps longer. The bug was not so critical, the worst case being arccos(9.999999999E01) returning 8.102847000E04 rather than 8.102846845E04. I hope this was the only and the last one :)
sin(9) 0.156434465041 This result is pretty close to that of Elektronika C315, later version: 9.00000588129. Gerson.
Edited: 3 Dec 2006, 12:48 a.m.
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  tony (nz)  12032006 Hi Gerson,
Congrats of your Magnum Opus! I was just idly wondering if you subtracted E13 from the pi/180 (which makes it very marginally more accurate) ... how would the forensic 9 degree test go? Maybe you'll get the 8.99999864267 like the 19BII (for example).
Cheers,
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Gerson W. Barbosa  12032006 Hello Tony, Thanks for the compliment, but Magnum Opus is a bit exaggerated :) For pi/180 I have used 0.01745329252 because it's close to the actual 12digit constant 0.0174532925199. I tried 0.0174532925198 as you suggested but I noticed no improvement (I obtained 9.00000588157). Although the last two digits are not truncated anymore, there is a lost of accuracy beginning in the seventh or the eighth significant digit for arguments equal or greater than 0.999999. This appears to be due to floating point errors and doesn't have anything to do with the choice of contants. I have to repeat the same tests in the table below with the older version. In case there isn't any noticeable improvement, perhaps I'll get back to the older version because it's slightly shorter. Anyway, this was a nice exercise for a Saturday afternoon and evening :)
x HP32SII HP25 HP12CP HP15C In this table the results are exact to 12 and 10 digits on the HP32SII and HP15C. The HP25 has errors beginning in the fifth significant digits. In this range the HP12CP program is closer to the HP15C than the HP25. Out of this critical range, the program has no problems. However the HP25, a commercial product in the mid 70's, presents an error in the tenth significant digit, when computing acos(0.9). Considering I was mostly interested in a fast running program, I am still pleased with the program. It's faster than the russian calculator in the link, if this can be used as a comparison. Of course I am also pleased with the overall accuracy, thanks to the extra digits in the Platinum. Cheers, Gerson.  _{ Edited to include this table:} After debugging (by using acos(x) = 2*atan(sqrt((1  x)/(1+x))), as suggested by Tony):
x HP32SII HP25 HP12CP HP15C _{The older version may be definitely discarded.}
Edited: 13 Dec 2006, 3:52 p.m. after one or more responses were posted
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  tony (nz)  12042006 Hi Gerson, I checked out the acos(.9999999) on the train this morning. This may be a coincidence but your HP12cp result is *exactly* what I get from a two term Taylor: (x  (x^3)/3)*180/PI = 0.0256234524170 where x=sqrt(1.9999999^2)/.9999999 To get 17765 for the last five places we need to prolong the Taylor. Hope this helps in diagnosing the problem.
Cheers,
Edited: 4 Dec 2006, 9:34 p.m.
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Gerson W. Barbosa  12042006 Hello Tony, This is really a weird coincidence since I haven't used Taylor's series. I have used a polynomial approximation instead.
The problem is due to floating point errors, as I suspected. Just take a look at this stepbystep example: acos(x) = atan(sqrt(1  x^2)/x) That is, the program gives the best answer given the 12CP is a 12digit calculator. It seems HP calculators use double precision in this critical range. Perhaps that's why they have pi to 20 or 24 four places built in. This problem doesn't occur with asin(x) because the error is not critical for arguments in that range. Perhaps I should go back to the older version, since it's shorter. After all, since the errors begin in the seventh digit there is no problem in truncating the last two digits, since they are wrong anyway.
Perhaps I should use those extra steps to implement the HP rounding philosophy as Rodger Rosenbaum has suggested (see my next reply) :) Regards, Gerson.
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  tony (nz)  12052006 Hi Gerson, Quote: OK, try acos(x) = 2*atan(sqrt((1  x)/(1+x)))
Cheers, Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Rodger Rosenbaum  12052006 Quote: Try this formula for acos: acos(x) = 2 * atan(SQRT(1x)/SQRT(1+x)), for x>0, and with appropriate changes for x<0. I think that when x is near 1, this formula will retain more accurate digits (if the atan function is accurate).
Quote: The Saturn based machines have a builtin 31 digit pi for range reduction of trig functions, and do 31 digit arithmetic operations during the range reduction.
That's why they can get accurate results from trig functions in radian mode whose arguments are as large as 10^19. I know of no other hand held calculators that can do that (most of them just error out for arguments that large).
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Gerson W. Barbosa  12052006 Tony: Quote: Rodger:
Quote:
Regards,
Gerson.
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Rodger Rosenbaum  12052006 Gerson, Those two formulas are mathematically equivalent, of course. But you will see a difference in the errors associated with the values they return. Sometimes one formula will return an exactly correct value, and other times the other one will. For example, for an input argument of .999999999966, the formula with only one square root gives the correct result. For an input argument of .999999999964, the formula with two square roots gives the correct result. These calculations were made on the HP50. Both formulas should give an error of only 1 or 2 units in the LSD for arguments near 1.
Unless you can find a way to get a couple of extra digits (guard digits) in your computations, you won't be able to get perfectly exact (to 10 or 12 digits) results.
Re: Fast and Accurate Trigs (HP12C Platinum)  Revised version.  Gerson W. Barbosa  12052006 Hello Rodger, Since the program runs on the 12C Platinum, only 10 significant digits can normally be entered by the user. So arguments up to 9.999999999E01 will be entered. And considering two guard digits are available and the atan(x) function is accurate to 11 significant digits I am confident it will be possible to replicate the HP15C answers, according to the tests I've done so far. And yes, I am convinced it's better to have always 10 correct digits than sometimes 11 and sometimes 12, so I decided to use the HP rounding philosophy, which looks wonderful :) The tests below have been performed on the HP12C Platinum. As you can see, the atan(x) function is accurate enough to display at least 10 correct digits. Thanks for the suggestion and the past "What should we get" threads. Regards, Gerson.

Edited: 5 Dec 2006, 6:12 p.m.
