Google Answers Logo
View Question
Q: I need an equation or C or C++ function to know the position of the sun ( Answered,   1 Comment )
Subject: I need an equation or C or C++ function to know the position of the sun
Category: Science > Astronomy
Asked by: juanluissoldi-ga
List Price: $15.00
Posted: 25 Mar 2005 07:00 PST
Expires: 24 Apr 2005 08:00 PDT
Question ID: 500251
I'm trying to make a software for which I need a function (or at least
an ecuation) that receives latitude, longitude, date, hour and minute,
and gives me the position of the sun. I'm not sure about how the
scientists describe the position of the stars in the sky, but it could
be described by giving the direction (north, south, east, west, or I
guess, an angle to know exactly the cardinal point) and the angle with
the horizon. So, if we say that the sun is to the west, and its angle
with the horizon is 0 or 180, then we would be in the sunset and the
sun would be a half visible. If the ecuation or function gives me
something like that it would be perfect.
I think that the atmosphere can change the apparent position of the
sun when it's near the horizon. If it is,  I need to know the apparent
position and not the real position.
Subject: Re: I need an equation or C or C++ function to know the position of the sun
Answered By: livioflores-ga on 25 Mar 2005 15:04 PST
Hi juanluissoldi!!

I think that I found what you need, but please do not consider this
answer ended until you feel satisfied with it. If you find it unclear
or incomplete use the clarification feature to request for a
clarification. i will gladly assist you until you get the needed

The position of the sun is generally given as an azimuth and altitude value.
Azimuth represents the horizontal angle of the sun relative to true
north. This angle is always positive in a clockwise direction from
north when viewed from above. Altitude represents the vertical angle
the sun makes with the horizontal ground plane. It is given as an
angle in the range 0 < alt < 90.

Start from the page "Basics of Positional Astronomy and Ephemerides"
to get the basic knowledge regarding to the positional concepts:
"The observer is located at the centre of his "celestial sphere" with
zenith Z above his head and the horizon N-E-S-W. The Sun, Moon or any
other celestial body can be identified by the two coordinates altitude
h and azimuth alpha (horizontal coordinates). Altitude is the angular
distance above the horizon (0 < h < 90), and azimuth the angular
distance, measured along the horizon, westwards from the south point S
(in astronomy) or eastwards from the north point N in nautics (0 <
alpha <360)."

"Astronomical Algorithms":
This page is associated with the above one and give you formulas for calculations .

"Basic program: position of the Sun":
I am pretty sure that at this page you will find what you need, it
give you the formulas, explanations, examples and a sample program.

"Calculating the days from J2000":
associated page for the above, to calculate the days before J2000.0

Additional help and references can be found at:
"| SQUARE ONE | Solar Position":
Very helpful source.

"Astronomy Answers: Position of the Sun":
This is a more advanced page (in my understand).

To compare your results you can use this online calculator:
"Calculation of Sun Position, Sunrise and Sunset"

Another example of this calculations is here:

Search strategy:
"position of the sun"
"position of the sun" azimuth

I hope that this helps you. and remember to feel free to request for a
clarification if you need it.


Request for Answer Clarification by juanluissoldi-ga on 30 Mar 2005 11:59 PST
I've tried to make my own functions using the formulas gived in
This one is the less confused of all the web pages, but anyway, my
results doesn't match with the results of the java script on this web
This is a little sample of what I have done:

double JulianDay(int year, int month, int date, double UT)
        if (month<=2) {month=month+12; year=year-1;}
        return (int)(365.25*year) + (int)(30.6001*(month+1)) - 15 +
1720996.5 + date + UT/24.0;
double Declination(double iJulianDate)
        double t = 2 * M_PI * ((iJulianDate - 1) / 365.0);

        return 0.322003
                - 22.971 * cos(t)
                - 0.357898 * cos(2*t)
                - 0.14398 * cos(3*t)
                + 3.94638 * sin(t)
                + 0.019334 * sin(2*t)
                + 0.05928 * sin(3*t);

So I called the Declination function giving it the result of the
JulianDay function (which I'm pretty sure is working all right), but I
didin't get the same result that I got on the action script on the web
page. I don't know what is wrong.
Another thing you did't tell me about is about that change that I said
the atmosphere can make to the apparent position of the sun.
I hope you can help me.

Clarification of Answer by livioflores-ga on 30 Mar 2005 20:42 PST

I am not so skilled in this topic, just researched and find what I
think you need, but please give me some time to make a more in-depth
research to check your formulas and find the additional info

Thank you.

Clarification of Answer by livioflores-ga on 04 Apr 2005 08:07 PDT

Excuse the delay, I am trying to understand this stuff and working to
find a better answer for you. May be tonight I will give you my

Thank you for your understanding. 

Clarification of Answer by livioflores-ga on 04 Apr 2005 23:50 PDT

I checked the Julian day calculation part and it is rght, it is working fine.

In regards to the second part, the Declination calculator I think that
there are some variables definitions mistakes (may be I wrong,
remember that I am learning with you and do not know nothing about
programming), you wrotte:
double Declination(double iJulianDate)
        double t = 2 * M_PI * ((iJulianDate - 1) / 365.0);

        return 0.322003
                - 22.971 * cos(t)
                - 0.357898 * cos(2*t)
                - 0.14398 * cos(3*t)
                + 3.94638 * sin(t)
                + 0.019334 * sin(2*t)
                + 0.05928 * sin(3*t);

My questions are: which value is iJulianDate, I think that you must
use the variable JulianDay, and more important (I think) is the fact
that your formula did not take into account the location variables, it
looks like the formula is costumized for a specific place.

I found a page that shows clear formulas for apparent!! declination
and right ascension of sun:
i-. Definitions 

Julian day number = number of days elapsed since 12h 
Julian date = Julian day number followed by the fraction of a day
elapsed since the preceding noon.

ii-. Variables 
          D = number of days from 1900.0 (Julian date 2415020.0)
          T = number of Julian centuries from 1900.0 = D/36525
          L = mean longitude of sun in degrees
          E = equation of time (in seconds of time)
    epsilon = obliquity of the ecliptic
      alpha = right ascension of sun (apparent) in degrees
      delta = declination of sun (apparent) in degrees
        GHA = Greenwich hour angle of sun (in degrees)
         UT = Universal time which is equivalent to GMT in this
context (in degrees)

iii-. Formulae 

     D = Julian date - 2415020
     T = D/36525
     L = 279.697 + 36000.769T
     E = - (93.0 + 14.23T - 0.0144T2)sin L - (432.5 - 3.71T - 0.2063T2)cos L
         + (596.9 - 0.81T - 0.0096T2)sin2L - (1.4 + 0.28T)cos 2L
         + (3.8 + 0.60T)sin3L + (19.5 - 0.21T - 0.0103T2)cos 3L -
(12.8 - 0.03T)sin 4L
tan epsilon = 0.43382 - 0.00027T
      alpha = L - E/240
  tan delta = tan(epsilon) sin(alpha)
        GHA = UT + E/240 + 180

iv Worked Example 
          Find GHA and declination of the sun on 1976 August 8 at 6h UT 
          D = 27978.75
          T = 0.7660164
          L = 136.877?
          E = -335.4 in seconds of time (= -1.397?)
  tan epsilon = 0.433613
      alpha = 138.274? = 138? 16.4'
      delta = 16.098? = 16? 5.9'
        GHA = 268.603? = 268? 36.2'                                  "

From "WMO Report No. 6 - Appendix F":

I hope that this helps you. Let me know if you still have queries that
i can help you to answer.

Subject: Re: I need an equation or C or C++ function to know the position of the sun
From: asha_gator-ga on 08 Apr 2005 21:10 PDT
  Here is an IDL function that returns the position of the sun in
Right Ascension (RA( and Declination(DEC) for a gived Julian Date. RA
and DEC are the co-ordinates usually used by astronomers to describe
the location of a celestial body.

Hope this helps. I'll be happy to provide any more clarifications if necessary

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy