Compile time error

I'm getting an error when i'm compiling this function. Its supposed to take a date and turn it into a julian day. I just used the formula provided to do it and its not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
long convertJulian(int m, int d, int y)
{
    long intRes1, intRes2, intRes3;
    long julianDay;
    
    if( y <= 1582 && m < 10)
             intRes1 = 0;
    if(y == 1582 && m == 10 && d <= 15)  
                 intRes1 = 0;
    else 
         intRes1 = 2 - y / 100 + y / 400;
    
    intRes2 = int(365.25*y);
    intRes3 = int(30.6001*(m + 1); // <--this line is giving error
    
    long(julianDay = intRes1 + intRes2 + intRes3 + d + 1720994.5);
    
    return julianDay;
}
Count your parens
I see it. fiixed.

But now, line 16 is giving me a problem :(
Ok think i got it. I was mixing longs with doubles. but i thought it would automatically convert it to whatever type i want it to.. long(double) = long
FYI, if you are trying to cast stuff, follow this:

1
2
3
4
5
someLong = long(myOtherType);
//or
someLong = (long)myOtherType;
//or if you are using C++
someLong = static_cast<long> (myOtherType);
@firedraco

I was using the first example but i was getting the error. I ended up being lazy and changing everything to doubles and it compiled. But from your explanation should it have looked like julianDay = long(julianDay = intRes1 + intRes2 + intRes3 + d + 1720994.5);?
I got it working with the type casting with the first code too. I'm not sure what happened. I think it went wrong in the prototype or the call itself. Thanks for the help!
It would be

 
julianDay = long( intRes1 + ... + 1720994.5);


or in C++ it should be written

 
julianDay = static_cast<long>( intRes1 + ... + 1720994.5);


However... I'm a little confused. intRes1, intRes2, intRes3, and d are all integers,
and julianDay is an integer (long). This means that the 0.5 in your constant is
always going to be truncated when the conversion back to integral type is
done. At that point, if that is truly what you want your code to do, just drop
the 0.5 making the last term an integer literal and then you don't need the cast
at all.



Topic archived. No new replies allowed.