Go Down

Topic: Real maths (Read 1 time) previous topic - next topic

PaulS

Quote
I didn't know about the sticking (float) in front of a variable converted it.

It doesn't convert it. The variable remains the same type.

What it does is cast it. That is it treats the value as though it was the type being cast to. So, in this case, it treats y, m, and d as though they had been defined as floats.

dxw00d

Also note the addition of '.0' to all the literals, to make them floats too.

cowasaki


Also note the addition of '.0' to all the literals, to make them floats too.


Thanks, much appreciated.  I've not had to resort to floats in this language yet!

cowasaki

I've checked and double checked the maths but Excel and the Arduino don't quite come out with the same answer.

This is the current sketch.....

Quote


// Variables for return by function "siderealcalculations"

float sr_whole;
float sr_frac;
float sr_days;
float gmst;
float gmst360;
float gmstHrs;
int gmst_H;
int gmst_M;
int gmst_S;

// Variables for return by function "polestarlocate"

float polaris_posH;
float polaris_posDeg;



void siderealcalculations(int y,int m,int d,int h,int mn,int s){

  sr_whole=367.0*(float)y - 7.0*((float)y+int(((float)m+9.0)/12.0))/4.0 +int(275.0*(float)m/9.0)+(float)d-730531.5;
  sr_frac = ((float)h + (float)mn/60.0 + (float)s/3600.0)/24.0;
  sr_days = sr_whole + sr_frac;
  gmst = 280.46061837 + (360.98564736629 * sr_days);
  gmst360 = fmod(gmst,360.0);
  gmstHrs = gmst360/360.0*24.0;
  gmst_H = int(gmstHrs);
  gmst_M = int(gmst360*1.0/360.0*24.0*60.0)-gmst_H*60;
  gmst_S = int(gmst360*1.0/360.0*24.0*3600.0)-(gmst_H*3600)-(gmst_M*60);



// Note polestarlocate() relies on the function siderealcalculations having been called first

void polestarlocate(){
  polaris_posH = fmod(((24.0-gmstHrs)/2.0)+1.25,12);
  polaris_posDeg = 30.0*polaris_posH; 
}

void setup()
{
  Serial.begin(19200);
 
  siderealcalculations(2012,2,13,14,27,5);
  polestarlocate();

  Serial.println(sr_whole);
  Serial.println(sr_frac);
  Serial.println(sr_days);
  Serial.println(gmst);
  Serial.println(gmst360);
  Serial.println(gmstHrs);
  Serial.println(polaris_posH);
  Serial.println(polaris_posDeg);
  Serial.println(gmst_H);
  Serial.println(gmst_M);
  Serial.println(gmst_S);
}

void loop()
{
}



and I posted the Excel test spreadsheet earlier.

Everything works until the SECONDS part of the calculation

Excel gets 13 seconds
Arduino gets 30 seconds

I PRESUME this may well be down to precision but looking at it I wouldn't have thought that it would be that far out, unless I have messed something up and couldn't see it.

Being out by 17 seconds wouldn't be the end of the world for it's current application but I could do with it accurate if I can get it so as I will then add it to my library and can forget about it....


I really do appreciate the help.  I normally program in Pascal (delphi) and assembly but have had to venture into C and JAVA recently.  I am getting on with it but some syntax etc has confused me.

Thanks

PaulS

Quote
730531.5

Time to read up on precision.

Quote
360.98564736629

Better go back and read it again.

A float on the Arduino is good for 6 to 7 digits of precision. The first value is pushing the limits. The 2nd is way over the edge.

cowasaki


Quote
730531.5

Time to read up on precision.

Quote
360.98564736629

Better go back and read it again.

A float on the Arduino is good for 6 to 7 digits of precision. The first value is pushing the limits. The 2nd is way over the edge.


Is there any way of getting more precision than with float ?

PaulS

Quote
Is there any way of getting more precision than with float ?

Use different hardware.

cowasaki


Quote
Is there any way of getting more precision than with float ?

Use different hardware.


OK time to use assembly head.........


Go Up