I refer to the book : Astronomical Algorithms (Second Edition) by Jean Meeus.
To calculate the values of position angle of the bright limb and position angle you will need to calculate the apparent RA and DEC of the Sun and the Moon.
Code the equations for the Sun RA and DEC using Chapter 25. Please note that L0 (equ. 28.2) is in error To calculate T (Julian century you will need to compute Julian day (Chapter 7).
Code equations for the Moon RA and Dec from the equations given in Chapter 47.
For the calculation of Apparent RA and Dec you will need to compute Nutation and Obliquity - Equations in Chapter 22.
BTW. For all my calculations I have "standardized" on using the equations on page 144 for the variables : D, M, Mdash, F, and Omega.
Fyi compare the equations for M given in this book ie. equations 25.2 and 47.3
- Chapter 48 provides a formula (48.1) for the illuminated fraction of the moons disc.
5.1 I used 48.2 and 48.3
5.2 To use equation 48.5 you will need to obtain or write the atan code yourself to resolve the angle X in the correct quadrant - I wrote a routine: ATAN2(sin_tl,cos_bl).
I have coded all the above to run on my HP Prime graphing calculator and they have proved to be very, very accurate.
For the purpose of accurate determination of the moon phase angle you will need to calculate UTC(date and time) using your zone longitude or better still your local longitude.