Friday, June 23, 2017

HP Prime and Casio fx-CG 50: Phase of the Moon, C++ Translations to Calculator

HP Prime and Casio fx-CG 50: Phase of the Moon, C++ Translations to Calculator 

Moon Phase Program 

The following program calculates the approximate phase of the moon.  Calculation is based on the following:

1.  A base date of August 11, 1999 is used, which was a New Moon.
2.  The phases of the moon cycles approximately 29.530588853 days

The results is a phase indicator from 0 to 1. 

If the indicator is close to 0 or 1, we have a New Moon.
If the indicator above 0 but below 0.5, it’s the Waxing Crescent Moon.
If the indicator is about or exactly 0.5, we have a Full Moon.
Finally, for an indicator from 0.5 to 1, it’s the Waning Crescent Moon.

Examples:
June 23, 2017:  the indicator is 0.991190947
June 24, 2017:  the indicator is 0.025054136

Values are approximate.



HP Prime Program:  MOONPHASE

Input:  MOONPHASE(yyyy.mmdd)

EXPORT MOONPHASE(d)
BEGIN
// Moon phase
// 0,1: New; 0.5: Full
LOCAL t,p;
t:=DDAYS(1999.0811,d);
p:=FP(t/29.530588853);
RETURN p;
END;

Casio Program:  MOONPHSE

"EWS 2017-06-23"
"MONTH"?->M
"DAY"?->D
"YEAR"?->Y
Days_Prd(8,11,1999,M,D,Y)->T
Frac (T/29.530588853)->P

ClrText
Locate 1,1,"PHASE %:"
Locate 1,2,"0,1: NEW,0.5: FULL"
Locate 1,3,P


Source:  “Moon Phase”  voidware.  http://voidware.com/moon_phase.htm


Bonus:  From C to Calculator

The program presented on the source is written in C (C++?).  Translating from C to calculator language took a little research (confession: I’m not a C expert). 

var++  increment variable by 1.  (var + 1 → var)
var--  decrement variable by 1.  (var – 1 → var)
+=  storage addition (STO+,  var + value → var)
-=   storage subtraction (STO-, var – value → var)
*=  storage multiplication (STO*, var * value → var)
/=  storage division  (STO/,  var/value → var)
&   and
||  or
%  mod
‘! not

Source:  “C – Operators” Tutorials Point  - Simple Easy Learning https://www.tutorialspoint.com/cprogramming/c_operators.htm  Retrieved June 23, 2017

Eddie

This blog is property of Edward Shore, 2017