Go Down

Topic: ardiuno unwanted rounding rounding (Read 78 times) previous topic - next topic

larrym1972

this is part of a program i been working i broken program in parts to work of the bugs.
i have is unwanted rounding which i do not know how to fix

here is and example of the problem
double example = 730532 + .49961;
Serial.print("example  ");
Serial.print(example,12);

serial prints out 730532.5
i should get this 730532.49961
 i have been getting this throught the calculation
i need exact number 


int xPOS, yPOS;
int yearA = 2018;
int monthA = 9;
int dayA = 19;
int t_zone=0;
int summer=0;
int hourA=5;
int minA=48;
int secA=0;
int lmst_hour, lmst_min, lmst_sec;
int last_hour, last_min, last_sec;
double lmst, last;
int RA_HH,RA_MM,RA_SS;

int long_DD = -78;
int long_MM = 45;
int long_SS = 45;

int lat_DD = 39;
int lat_MM = 39;
int lat_SS = 10;
int count;



void setup(){
     Serial.begin(9600);
 // while(!Serial);
 // gps.begin(9600);
  //gpsSerial.begin(9600);
 // delay(1000);

}
void loop(){
  /*
     while (gpsSerial.available()>0) {
   
    gps.encode(gpsSerial.read());
    if (gps.location.isUpdated()){
         Serial.print("Latitude= ");
         Serial.println(gps.location.lat(), 6);
         Serial.print(" Longitude= ");
         Serial.println(gps.location.lng(), 6);
      }       
      if (gps.encode(gpsSerial.read())) {
 
        Serial.print("Number os satellites in use = ");
         Serial.println(gps.satellites.value());
         hourA = gps.time.hour();
         minA = gps.time.minute();
         secA = gps.time.second();
      }
  }*/
   SiderealTime();/*
   LCD.clear();
   LCD.setCursor(0,1);
   LCD.print("DATE");
   LCD.setCursor(7,1);
   LCD.print(monthA);
   LCD.setCursor(9,1);
   LCD.print(dayA);
   LCD.setCursor(13,1);
   LCD.print(yearA);
   LCD.setCursor(0,1);
   LCD.print("TIME   HOUR MIN SEC");
   LCD.setCursor(0,2);
   LCD.print("DEC");
   LCD.setCursor(8,2);
   LCD.print(last_hour); //DEC Degrees
   LCD.setCursor(13,2);
   LCD.print(last_min);
   LCD.setCursor(14,2);
   LCD.print(last_sec);
   LCD.setCursor(0,3);
   LCD.print("RA");
   LCD.setCursor(8,3);
   LCD.print(hourA);
   LCD.setCursor(13,3);
   LCD.print(minA);
   LCD.setCursor(14,3);
   LCD.print(secA);*/

}
int SiderealTime(){
  t_zone = 0; //longitude / 15; //time zone
  double L_time = (hourA + (minA / 60.0000 ) + (secA / 3600.0000)); // decimal hours
  double GMT = (L_time + t_zone - summer) / 24.0000; // greenwich medredian time
  int YY =  int((monthA + 9) / 12);
  double AA = int(7*(yearA+YY));
  double BB = int(AA / 4);
  double CC = int(275.0000000 * monthA / 9.0000000);
  double XX= 367.0000000 * yearA;
  double SS = XX - BB + CC + dayA;
  double JD=SS - 730532 .49961 + GMT;
  Serial.print("JD ");
  Serial.println(JD,8);
  double j_cent = JD / 36525.0000000; // julian centuries to J2000
  Serial.print("j_cent ");
  Serial.println(j_cent,8);
  double EE = 360.98564736629 * JD;
  Serial.print("EE   ");
  Serial.println(EE,12);
  double FF = 280.46061837 + EE;
  Serial.print("FF   ");
  Serial.println(FF,12);
  double gmst=fmod((FF), 360); 
  Serial.print("gmst ");
  Serial.println(gmst,8);     //longitude of the ascending node of the Moon's mean orbit - the longitude when the Moon passes through the plane of the ecliptic
  double Om = fmod((280.46061837+360.98564736629*j_cent), 360);
  double Sun_long = fmod((280.4665+36000.7698*j_cent), 360); //mean longitude of the Sun
  double Moon_long = fmod((218.3165+481267.8813*j_cent), 360); //mean longitude of the Moon

        //change in the ecliptic longitude of a star due to the nutation (good to about 0.5 arcsec)
  double dp =-17.2*sin(Om)-1.32*sin(2*Sun_long)-0.23*sin(2*Moon_long)+0.21*sin(2*Om);
       //shift in angle between the ecliptic and equator (good to about 0.1 arcsec)
  double de =9.2*cos(Om)+0.57*cos(2*Sun_long)+0.1*cos(2*Moon_long)-0.09*cos(2*Om);
  double eps =23.43929111-46.815/60/60*j_cent; // Obiliquity of the Ecliptic

  double dT=(dp / PI *180)*cos(de+eps)/3600; //difference between Mean and Apparent Sidereal Times
  double gast=gmst+dT;// Greenwich Apparent Sidereal Time
  lmst=gmst+longitude; //local Mean sidereal time
  last=gast+longitude; //local Apparent sidereal time


  lmst_hour = int(lmst/15);
  lmst_min=int((lmst/15-lmst_hour)*60);
  lmst_sec=int(lmst/15-lmst_hour-lmst_min/60)*3600;

  last_hour = int(last/15);
  last_min=int((last/15-last_hour)*60);
  last_sec=(last/15-last_hour-lmst_min/60)*3600;

}

larrym1972


sterretje

#2
Sep 19, 2018, 06:33 pm Last Edit: Sep 19, 2018, 06:35 pm by sterretje
Floating point numbers (floats, doubles) are never accurate, sorry.

Work with integers and integer math if you need accuracy.

// Edit
PS
Double is equivalent to float on AVR based boards.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

gfvalvo

Double is equivalent to float on AVR based boards.
Right, both float and double are the same 32-bit IEEE format in AVR. You're getting as good as you're going to get with that format.

AWOL

#4
Sep 19, 2018, 06:42 pm Last Edit: Sep 19, 2018, 06:43 pm by AWOL
Code: [Select]
double example = 730532 + .49961;
Serial.print("example  ");
Serial.print(example,12);
Even of you were using a processor that supports 64 bit doubles, like the Arm on the Due, you'd be hard-pressed to get twelve places of decimals.

Please remember to use code tags when posting code.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up