yes I checked the modulo operator but taught is was the wrong function. maybe I've misunderstood the operator ** in my code, it is by calculations I've figured out.
int n = 9;
int d = 2;
int r = roundedDivision(n, d);
int roundedDivision(int n, int d)
{
float r1 = (float)n / (float)d;
int r2 = n / d;
r1 -= r2;
if(r1 >= 0.5)
r2++;
return r2;
}
return 367 * y - 7 * (y + (m + 9) \ 12) \ 4 + 275 * m\ 9 + d - 730531.5 + h / 24;
The "" operator is used in .NET to force an integer division. In C/C++ it depends on the type of the arguments. If both sides are int's the compiler will use int math rather than float.
A rewrite could be as follows:
return 367 * y - 7 * lround(y + lround(m + 9) / 12) / 4 +lround( 275 * m)/ 9 + d - 730531.5 + h / 24;
Edit:
This would not agree with what you wrote in your first post ...
9/2 = 4.5
9\2 = 5
as 9\2 would be 4 - which I would assume is what you want.
The code looks like an extract from date calculations and I trust you will find plenty of alternative sources for the same functionality in "proper" C if you google for it (the Arduino timer library may be a good start).
The issues you face here are also the underlying reasons why Microsoft decided on separate operators in .NET for int "" and float "/" division. The choice of int vs. float is explicitly defined and easier to debug.
okey, lesson learned! (I almost started thinking that the arduino was tired...turned out to be me)
the problem with my function still remains, it doesn't work.
I have values on everything so it shouldn't be that hard to figure out how the math should be.
float FNday(int year,int month,int day,float hour){
return 367 * year - 7 * (year + (month + 9) \ 12) \ 4 + 275 * month\ 9 + day - 730531.5 + hour / 24;
}
int year = 2001;
int month = 3;
int day = 4;
float hour = 15;
int mins = 30;
hour= hour + mins/60.0; // hour = 15.5
d = FNday(year, month, day, hour); //right value d = 428.14853
I'm wondering where this came from:
Quote:
A long is an integer
I said that a float was bigger than an int.
No you didn't.
Re-read reply #11.
It is quite dangerous to say that an "int" is bigger than a "float"; it is processor-dependant. An "int" in C++ on your PC is probably the same width as a "float".
Actually, the problems are not at all the same. The integer multiplication overflowed. The float multiplication did not.
The integer multiplication returned an integer. The float multiplication returned a float. A float is bigger than an integer.
I agree that the size of an int is processor dependent. However, this forum concerns itself primarily with a single family of processors, and on those processors, a float IS larger than an int.
Anyway, to be technically accurate, what I meant to say was the the range of values that can be stored in a float on an Arduino Duemilanove (had to go look at the board to figure out how to spell that) is larger than the range of values that can be stored in an int.
A float on the A/D has a range from -3.4028235E+38 to 3.4028235E+38. An int has a range from -32,768 to 32,767.
OP's multiplication example (367 * 2001) computed a value of 734367 which overflowed the int.
Sorry, maybe I was being a little flippant, but it is well worth remembering about type sizes, particularly if you're mixing Processing (where an "int" is 32 bits and a "long" is 64 bits) and the Arduino's Wiring (where an "int" is 16 bits and a "long" is 32 bits).
A calculation prototyped in Processing may well work and yet cause much head-scratching when ported onto an Arduino.