Tampilkan postingan dengan label Free42. Tampilkan semua postingan
Tampilkan postingan dengan label Free42. Tampilkan semua postingan

Swiss Micros DM42, Free42, Plus42: FUNC command

Swiss Micros DM42, Free42, Plus42:   FUNC command


Turning Programs into User Functions


The FUNC command saves the stack contents and treats the program like it is a function.  This allows the program to work with a temporary stack which is "deleted" when the program reaches either END or RTN (and RTNYES, RTNN), while storing the original contents of the X stack in the L (LAST X) stack.  


FUNC requires a two digit argument.  The first digit is the stack levels used in input, while the second digit is the stack levels used in output.


FUNC 11:  Takes the argument from the X stack and returns the answer in X stack, while leaving the rest of the stack intact.

X:  f(x),  Y: y0,  Z: z0,  T:  t0,  Last X:  x0


FUNC 21:  Takes the arguments from the X and Y stacks and returns the answer in the X stack.

X:  f(x,y),  Y:  z0, Z: t0, T: t0, Last X: x0


FUNC22:  Takes the arguments from the X and Y stacks and returns the answers in the X and Y stacks.

X:  x from f(x,y),  Y:  y from f(x,y), Z:  z0,  T:  t0, Last:  x0


Example 1:  f(x,y) = (y + x)/(y - x)


Stack set up:

T = 24

Z = 15

Y = 45

X = 21


PR1:  Program 


00 { 17-Byte Prgm }

01▸LBL "PR1"

02 -

03 ENTER

04 ENTER

05 LASTX

06 +

07 LASTX

08 +

09 X<>Y

10 ÷

11 RTN

12 END


BEFORE PR1:

T=               70.0000

Z=               15.0000

Y=               45.0000

X=               21.0000


AFTER PR1:

T=               15.0000

Z=               15.0000

Y=               15.0000

X=                2.7500


FN1:  Using FUNC21


00 { 20-Byte Prgm }

01▸LBL "FN1"

02 FUNC 21

03 -

04 ENTER

05 ENTER

06 LASTX

07 +

08 LASTX

09 +

10 X<>Y

11 ÷

12 RTN

13 END


BEFORE FN1:

T=               70.0000

Z=               15.0000

Y=               45.0000

X=               21.0000


AFTER FN1: 

T=               70.0000

Z=               70.0000

Y=               15.0000

X=                2.7500



Example 2:  f(x,y) = x * y * √(x^2 + y^2)


Stack set up:

T = 64

Z = 11

Y = 15

X = 25



Note: The →POL command calculates that  x^2 + y^2 on the x stack


PR2:  Program 


00 { 17-Byte Prgm }

01▸LBL "PR2"

02 STO ST Z

03 X<>Y

04 STO× ST Z

05 →POL

06 RCL× ST Z

07 RTN

08 .END.


BEFORE PR2:

T=               64.0000

Z=               11.0000

Y=               15.0000

X=               25.0000


AFTER FN2:

T=               64.0000

Z=              375.0000

Y=                1.0304

X=           10,933.0348


FN2:  Using FUNC21


00 { 20-Byte Prgm }

01▸LBL "FN2"

02 FUNC 21

03 STO ST Z

04 X<>Y

05 STO× ST Z

06 →POL

07 RCL× ST Z

08 RTN

09 END


BEFORE FN2:

T=               64.0000

Z=               11.0000

Y=               15.0000

X=               25.0000


AFTER FN2:

T=               64.0000

Z=               64.0000

Y=               11.0000

X=           10,933.0348



As we can see, the FUNC command is a powerful command that allows programs to act like user functions, and is a great function for the DM42, Free42, and Plus42.  Unfortunately, FUNC is not available on the original HP 42S.


Until next time, happy programming,


Eddie 


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Swiss Micros DM42: Time Dilation

Swiss Micros DM42:  Time Dilation 



Introduction


The program DILATE calculates the time passage from the traveler's perspective:


t' = t * √(1 - (v/c)^2)


where c = Speed of Light = 299,792,458 m/s


We can also state the equation as:


t' = t * cos(arcsin (v/c)) 


because cos(arcsin(x)) = √(1 - x^2) for |x| ≤ 1



Why cos(arcsin(x)) = √(1 - x^2)?


Let Θ = arcsin x.  Then x = sin Θ.  Assume that -1 ≤ x ≤ 1.


From the trigonometric identity:


cos^2 Θ + sin^2 Θ = 1

(cos Θ)^2 +  (sin Θ)^2 = 1


With sin Θ = x,


(cos Θ)^2 + x^2 = 1

(cos Θ)^2 = 1 -  x^2

cos Θ = √(1 - x^2)

cos(arcsin x) = √(1 - x^2)



Swiss Micros DM42 Program:  DILATE

Also:  Free42, Plus42


00 {52-Byte Prgm}

01  LBL "DILATE"

02  "TIME?"

03  PROMPT

04  "VEL (M/SEC)?"

05  PROMPT

06  299792458

07  ÷

08  ASIN

09  COS

10  ×

11 "T'= "

12  ARCL ST X

13  AVIEW

14  RTN

15  END



Example


A spaceship traveling at a velocity of 186,000,000 m/s (about 416,070,150 mph) for 1 year from our perspective.


XEQ DILATE

TIME?  1

VEL (M/SEC)? 186000000

 

Result:  0.78426 year would have passed on the spaceship, which is about 9 months and almost 13 days


Until next time,


Eddie 


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


DM42: Recursive Fibonacci by Marko Draisma

DM42:  Recursive Fibonacci by Marko Draisma


The programs on today's entry is provided from Marko Draisma and they are provided with permission and a request.  Gratitude to Marko.


DM42 (and Free42/Plus42) Program: FIBO (Marko Draisma)


This recursive program generates the nth Fibonacci number.  FIBO is a recursive program because it calls itself in the program steps.


LBL "FIBO"

LSTO "N"

X>0?

GTO 00

0

RTN

LBL 00

1

RCL "N"

X>Y?

GTO 01

1

RTN

LBL 01

RCL "N"

1

-

XEQ "FIBO"

LSTO "M"

RCL "N"

2

-

XEQ "FIBO"

RCL "M"

+

END



1st Fibonacci number:  1 FIBO returns 1

2nd Fibonacci number:  2 FIBO returns 1

3rd Fibonacci number:  3 FIBO returns 2

4th Fibonacci number:  4 FIBO returns 3

5th Fibonacci number:  5 FIBO returns 5

6th Fibonacci number:  6 FIBO returns 8

7th Fibonacci number:  7 FIBO returns 13


The LSTO Command


This is the local store command.  Local variables are created only for the purpose of the program and are deleted when the program hits either a return command (RTN) or an end command (END).  


Variables created by the LSTO command are named variables.  We cannot create local variables to numeric registers.   There is no storage arithmetic with LSTO.  


LSTO is available for the Swiss Micros DM42, Free42, and the Plus42.


Execution Time of FIBO


The execution time of FIBO depends on how large the entry n. Calculating the 50th Fibonacci number, 12,586,269,025, takes over 12 minutes. Marko provides a fast tail recursion version, as presented next.  


DM42 (and Free42/Plus42) Program: FIBO2 (Marko Draisma)


00 { 80-Byte Prgm }

01▸LBL "FIBO2"

02 FS? 01

03 GTO 01

04 LSTO "N"

05 1

06 LSTO "B"

07 0

08 LSTO "A"

09 SF 01

10 XEQ "FIBO2"

11 RTN

12▸LBL 01

13 RCL "N"

14 X≠0?

15 GTO 02

16 CF 01

17 RCL "A"

18 RTN

19▸LBL 02

20 RCL "A"

21 ENTER

22 RCL+ "B"

23 LSTO "A"

24 X<>Y

25 LSTO "B"

26 RCL "N"

27 1

28 -

29 LSTO "N"

30 XEQ "FIBO2"

31 END


Getting the 50th Fibonacci number with FIBO2 is snap.   


Thanks once again to Marko Draisma.


See you next time,


Eddie 


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


HP and Swiss Micros 41C and 42S Series: Stack Storage Operations

HP and Swiss Micros 41C and 42S Series: Stack Storage Operations


Introduction  


The HP 41C and HP 42S families, including the Swiss Micros DM41, DM42, Free42, and Plus42, includes the ability to execute storage arithmetic on any of the four* stack levels X, Y, Z, and T.  


The storage arithmetic operator takes whatever value is in the X stack and applies it to any stack level.  If we enter a number before the stack arithmetic operation, the stack lifts first, then executes the stack arithmetic operation.   See the examples section for illustrations.  


*Some calculators allow for a bigger number of stack levels, like 8.  



Examples



Example 1:  


Keystrokes:  3 [ ENTER ] [ ENTER ] 1 [ ENTER ]

Stack:

T:  3

Z:  3

Y:  1

X:  1


Keystrokes:  41:  [ STO ] [ + ] [ . ] (Y)    42:  [ STO ] [ + ] [ . ]  (ST Y)

Stack:

T:  3

Z:  3

Y:  2

X:  1


Keystrokes:  3 

Stack:

T:  3

Z:  2

Y:  1

X:  3_



Keystrokes:   41:  [ STO ] [ + ] [ . ] (Y)    42:  [ STO ] [ + ] [ . ]  (ST Y)

Stack:

T:  3

Z:  2

Y:  4

X:  3



Example 2:  


Keystrokes:  3 [ ENTER ] [ ENTER ] 1 [ ENTER ]

Stack:

T:  3

Z:  3

Y:  1

X:  1


Keystrokes:  41:  [ STO ] [ + ] [ . ] (Z)    42:  [ STO ] [ + ] [ . ]  (ST Z)

Stack:

T:  3

Z:  4

Y:  1

X:  1


Keystrokes:  3 

Stack:

T:  4

Z:  1

Y:  1

X:  3_



Keystrokes:   41:  [ STO ] [ + ] [ . ] (Z)    42:  [ STO ] [ + ] [ . ]  (ST Z)

Stack:

T:  4

Z:  4

Y:  1

X:  3



Note:  Casio fx-991EX Week - September 5, 2022 to September 9, 2022 


Eddie 


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Backlink 9999 Traffic Super

Order Now...!!!!