Double factorial [wp34s]
|
02-12-2016, 12:03 AM
Post: #1
|
|||
|
|||
Double factorial [wp34s]
Code:
Program A: double factorial. This uses the formula used by Wolfram Alpha: \[x!!=2^{\frac{x}{2}-\frac{\cos \left ( \pi x \right )-1}{4}}\times \pi ^{\frac{\cos \left ( \pi x \right )-1}{4}}\times\left ( \frac{x}{2} \right )!\] 4 A --> 8 6 A --> 48 7.77 A --> 290.3223156096666 8.88 +/- A --> 0.01106808239125682 3.33 +/- A --> -1,065802995433050 5 +/- A --> 0.3333333333333334 Program B: rising double factorial. 9.8 ENTER 3 B --> 1595.832000000000 ; 9.8 * 11.8 * 13.8 = 1595.832 8.1 ENTER 4 B --> 13957.60410000001 ; 8.1 * 10.1 * 12.1 * 14.1 = 13957.6041 Program C: falling double factorial. 7.7 ENTER 2 C --> 43.88999999999996 ; 7.7 * 5.7 = 43.89 6.6 ENTER 3 C --> 78.93600000000006 ; 6.6 * 4.6 * 2.6 = 78.936 Program D: double factorial (integer arguments). This and programs B and C uses a more simple formula found at Wikipedia. 5 +/- D --> 0.3333333333333334 3 +/- D --> -1 4 D --> 8.000000000000001 |
|||
02-12-2016, 06:27 AM
Post: #2
|
|||
|
|||
RE: Double factorial [wp34s]
Nice Functions I considered implementing natively at one point....
cos(pi x) can be replaced by (-1)^x which should save a few steps at the start and be faster and more accurate. The "XEQ C RTN LBL C" sequence can be simplified to LBL C too I think. Likewise, the "XEQ C RTN" at the end can be replaced by GTO C - Pauli |
|||
02-12-2016, 07:49 AM
(This post was last modified: 02-12-2016 07:52 AM by Dieter.)
Post: #3
|
|||
|
|||
RE: Double factorial [wp34s]
(02-12-2016 06:27 AM)Paul Dale Wrote: cos(pi x) can be replaced by (-1)^x which should save a few steps at the start and be faster and more accurate. Let's not forget CNST, a handy command that IMHO is overlooked too often. CNST 86 is pi/2, and after rearranging the x!! formula to...
\(x!!=2^{\frac{x}{2}}\cdot\left(\frac{\pi}{2}\right)^{\frac{\cos \left ( \pi x \right )-1}{4}}\cdot \left(\frac{x}{2}\right)!\) ...the A program gets quite a bit shorter: Code: LBL A Dieter |
|||
02-12-2016, 10:45 AM
Post: #4
|
|||
|
|||
RE: Double factorial [wp34s]
CNST 86 isn't an officially supported command and cannot be entered from the keyboard...
Pauli |
|||
02-12-2016, 11:56 AM
Post: #5
|
|||
|
|||
RE: Double factorial [wp34s] | |||
02-12-2016, 02:21 PM
(This post was last modified: 02-12-2016 02:24 PM by Gerson W. Barbosa.)
Post: #6
|
|||
|
|||
RE: Double factorial [wp34s]
(02-12-2016 07:49 AM)Dieter Wrote:(02-12-2016 06:27 AM)Paul Dale Wrote: cos(pi x) can be replaced by (-1)^x which should save a few steps at the start and be faster and more accurate. 7/12th shorter, actually! The following preverves the Y register, but I need two more steps: Code:
Ideally, both Y and Z stack registers should be saved. Thus I would not need the alternative definition in program 01. The implementation of the rising and falling factorials might be more interesting, but they are defined in terms of the ordinary factorial and would take just a few steps each. Walter's Blue Book is worth looking at sometimes. If I only knew the whereabouts of mine... (hidden somewhere in the house). Thank you all for the optimization suggestions, formula rearrangement and comments. Gerson. |
|||
02-12-2016, 07:54 PM
Post: #7
|
|||
|
|||
RE: Double factorial [wp34s]
(02-12-2016 02:21 PM)Gerson W. Barbosa Wrote: The following preverves the Y register, but I need two more steps: Simply replace the initial ENTER ENTER || with #1/2 RCLx Y and it's one step less again. ;-) (02-12-2016 02:21 PM)Gerson W. Barbosa Wrote: Ideally, both Y and Z stack registers should be saved. Thus I would not need the alternative definition in program 01. Hmmm... this sounds quite challenging. Which does not mean it couldn't be done. ;-) Dieter |
|||
02-12-2016, 10:33 PM
(This post was last modified: 02-13-2016 10:42 PM by Paul Dale.)
Post: #8
|
|||
|
|||
RE: Double factorial [wp34s]
(02-12-2016 11:56 AM)Didier Lachieze Wrote: It may not be officially supported but it can be entered from the keyboard, CNST is in the P.FCN menu. I'd forgotten about that one. A side effect for allow indirect constants -- I don't remember why we included them but do remember doing it. The manual doesn't number the constants, instead the has user to count them. It also stops the documentation at the final user constant. [edit: this is incorrect] I'd assumed the various system constants after that are all subject to change if XROM needed modification. Pauli |
|||
02-12-2016, 11:52 PM
Post: #9
|
|||
|
|||
RE: Double factorial [wp34s]
(02-12-2016 07:54 PM)Dieter Wrote:(02-12-2016 02:21 PM)Gerson W. Barbosa Wrote: The following preverves the Y register, but I need two more steps: That's it! Thanks again! (02-12-2016 07:54 PM)Dieter Wrote:(02-12-2016 02:21 PM)Gerson W. Barbosa Wrote: Ideally, both Y and Z stack registers should be saved. Thus I would not need the alternative definition in program 01. Why not using register I? Its content would be changed in the next complex operation anyway. Or does this look like cheating? :-) Code:
|
|||
02-13-2016, 12:14 AM
Post: #10
|
|||
|
|||
RE: Double factorial [wp34s]
(02-12-2016 07:54 PM)Dieter Wrote: Hmmm... this sounds quite challenging. Which does not mean it couldn't be done. ;-) There are instructions included in the 34S for doing this kind of thing easily: Code: LBL A Not as good as XROM's xIN/xOUT but this version will nest and you've got the input value in a register for easy access and LastX is handled correctly. Not bad for five steps. - Pauli |
|||
02-13-2016, 12:57 AM
(This post was last modified: 02-13-2016 01:02 AM by emece67.)
Post: #11
|
|||
|
|||
RE: Double factorial [wp34s]
This doesn't use CNST. Only one more step than Dieter's:
Code: LBL A Corrupts all the (4 level) stack, but I'm planning to put it in XROM, so that will not be a problem. |
|||
02-13-2016, 01:36 AM
(This post was last modified: 02-13-2016 01:40 AM by Gerson W. Barbosa.)
Post: #12
|
|||
|
|||
RE: Double factorial [wp34s]
The following replaces my initial programs. Thank you all for your suggestions, your pointing out my obvious mistakes and for the wp34s instruction set refresh.
Code:
Program A: double factorial. Program B: rising double factorial. Program C: falling double factorial. Rising factorial: Code:
Falling factorial: Code:
|
|||
02-13-2016, 04:46 AM
Post: #13
|
|||
|
|||
RE: Double factorial [wp34s] | |||
02-13-2016, 06:53 AM
Post: #14
|
|||
|
|||
RE: Double factorial [wp34s]
(02-13-2016 04:46 AM)Paul Dale Wrote: In XROM there is no reason to not use the pi/2 constant. The name is defined symbolically and if it gets renumbered, the assembler deals with it. Is there a reason why these useful constants were not included in the regular CONST catalog as #pi/2, #ln2, #ln10, #sqrt2pi etc.? Is it the limited space again? Dieter |
|||
02-13-2016, 07:05 AM
Post: #15
|
|||
|
|||
RE: Double factorial [wp34s]
(02-13-2016 06:53 AM)Dieter Wrote: Is there a reason why these useful constants were not included in the regular CONST catalog as #pi/2, #ln2, #ln10, #sqrt2pi etc.? Is it the limited space again? Ask Walter Each exposed constant does consume extra bytes so space is an issue as always. I think it is twelve bytes extra (maybe a bit more) per exposed constant -- the question, as always, is when to stop. Remembering that: "pi 2 /" is three steps. Some of the constants are also virtually useless for the user, although after digamma was dropped there aren't as many of these. Pauli |
|||
02-13-2016, 09:15 AM
Post: #16
|
|||
|
|||
RE: Double factorial [wp34s]
(02-13-2016 04:46 AM)Paul Dale Wrote: In XROM there is no reason to not use the pi/2 constant. The name is defined symbolically and if it gets renumbered, the assembler deals with it. Thanks for the tip. I've removed some predefined constants in my build, so I try to avoid the use of CNST. "Num [pi]/2" worked fine. |
|||
02-13-2016, 07:00 PM
Post: #17
|
|||
|
|||
RE: Double factorial [wp34s]
(02-13-2016 07:05 AM)Paul Dale Wrote: the question, as always, is when to stop. Remembering that: "pi 2 /" is three steps. True – but this applies other constants as well. Some physical constants provided by the 34s are simply products or quotients of two others. Or consider cases like Φ0 and Kj – the one is simply the reciprocal of the other, so at least one of them is not required. And what about #eE? A simple 1 ex would be fine either, wouldn't it? Even without disturbing the stack like a manual [pi] 2 [/] would do. OK, constants like ln 2 or lg e can be obtained with two program steps, and they don't even cause stack problems (well, except losing LastX). Others do, so I would trade several of the present physical constants for a simple pi/2, pi/4 or some others. But you said that the assembler can handle several constants, I assume this applies to cases like ln 10 or sqrt(2 pi). Could you kindly provide a list of these? Dieter |
|||
02-13-2016, 07:50 PM
Post: #18
|
|||
|
|||
RE: Double factorial [wp34s]
(02-13-2016 07:00 PM)Dieter Wrote: But you said that the assembler can handle several constants, I assume this applies to cases like ln 10 or sqrt(2 pi). Could you kindly provide a list of these? The wp34s.op file can tell you. Here is an excerpt: Code: 0x2000 cmd # 1/2 Marcus von Cube Wehrheim, Germany http://www.mvcsys.de http://wp34s.sf.net http://mvcsys.de/doc/basic-compare.html |
|||
02-13-2016, 10:41 PM
Post: #19
|
|||
|
|||
RE: Double factorial [wp34s] | |||
02-14-2016, 05:44 AM
(This post was last modified: 02-14-2016 05:45 AM by Gerson W. Barbosa.)
Post: #20
|
|||
|
|||
RE: Double factorial [wp34s]
(02-13-2016 10:41 PM)Paul Dale Wrote:(02-12-2016 10:33 PM)Paul Dale Wrote: The manual doesn't number the constants, instead the has user to count them. It also stops the documentation at the final user constant. Speaking of factorials and constants, I've found quite by chance a remarkable approximation to a Gamma-related constant (13 SD!). It's the positive real root of the polynomial equation x^28 + x^27 + x^26 + ... + x^4 + x^3 + x^2 - 130615 = 0. I think the wp34s solver can handle that, but it might be easier to check this on the hp 50g: Code:
The four rising and falling factorials functions have been naively implemented. They might overflow when they shouldn't, like some comb and perm functions in some old calculators. Thanks, Walter, for helping. I haven't found my copy of the Blue Book yet, but at least I've found my spare wp34s with good batteries in it :-) Gerson. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)