algorithm

I am using jean meeus's astronomical algorithm's formula :

int = whole number.

if (month > 2) leave month and year unchanged
if (month == 1 or month == 2) then year - 1 and month + 12

a = INT (year / 100)
b = 2 - a + INT ( a / 4)

Julian day = INT (365.25 * ( year + 4716)) + INT (30.6001 * ( month + 1 )) + day + b - 1524.5


For example, October 4.81, 1957;
or y = 1957 m = 10 d = 1957

because m > 2 we leave y and m unchanged

so

a = INT (1957 / 100) = INT (19.57) = 19

b = 2 - 19 + INT (19 / 4) = 2 - 19 + 4 = -13

jd = INT (365.25 * 6637) + INT (30.6001 * 11) + 4.81 - 13 - 1524.5

jd = 2436116.31

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <iomanip>
#include <cmath>


using std::cout; 
using std::cin;
using std::setprecision;
using std::endl;

/* GLOBAL VARIABLES */
double y = 1957;
double m = 10;
double d = 4.81;

double a;
double b;

double jd;
int main ()
{
    if (m > 2) {
        y = y;
        m = m;
    }
        
        else if (m == 1 || m == 2) {
            y--;
            m+=12;
        }
    
    
    a = floor (y / 100.0);
    b = 2 - a + floor (a / 4);
    jd = floor (365.25 * (y + 4716)) + floor (30.001 * ( m + 1)) + d + b - 1524.5;
    
    cout << setprecision (16) << jd << endl;
    cin.get();
    return 0;
} 


So why is my output:

2436110.36


it should really be:

2436116.31


as posted above

I did this right. I obtained a copy of the book that this source is in and
I double checked it more than once and I did it right.


Last edited on
Computers do the best they can but you're definitely suffering from some rounding errors by the computer.
how do i fix that
BTW this algorithm is meant for computers which is weird.

Should I use something else than floor in cmath
Last edited on
What is this calculating?

I would think round() is what you want.
What is this calculating?


Jean Meeus's algorithm for converting Gregorian calander date to a Julian date


What header is round in? I have never heard of it. And I am Wanting to round down to the point to where there is no decimal. only a whole number
Your formula is off. 30.6001 is the constant, not 30.001. And floor() is correct in this case.
thanks for pointing that out.

I was about to use like 80 variables, split the formula into its parentheses go all modf (not sure if that would work)
By the way, round() is part of C99, so it's in math.h (cmath) for most compilers these days even though it's not part of the C++ standard (yet).
But C99 is not part of C++.

Here are some basic Rounding Algorithms
http://www.cplusplus.com/forum/articles/3638/
Topic archived. No new replies allowed.