int main(void) { printf("%d\n", pgcd(1071,1029) ); return 0; }
int pgcd( unsigned a, unsigned b) { return a ? pgcd(b%a, a) : b; }
void sofwareCorrection(){ if(timeProt.correction.nbCorrection!=0){ // printf("nb corr : %d",timeProt.correction.nbCorrection); sendHMI(" Software Correction "); //interval of time Clock averageTime; Clock averageOffset; Clock sumOffsetl; float fcorr=0; int indice=timeProt.correction.indiceFull; sumOffsetl=sumOffset; //sign change if(timeProt.correction.valueCor!=sumOffsetl.sign){//if the sum of the offset change of sign error ze reset the software corection indice=0; timeProt.correction.indiceMoySoftCor=0; } float second=(float)timeProt.correction.sumTime.second/timeProt.correction.nbCorrection; int secondi=(int)second; //calcul average time //printf("second : %d,%d",secondi,(int)((second-secondi)*1000.0)); averageTime.second=secondi; averageTime.halfmillis=(timeProt.correction.sumTime.halfmillis/timeProt.correction.nbCorrection)+RTC_FREQ*1.0*((float)(second-secondi)); //unsigned long int averageTimeli=(averageTime.second*2000)+(averageTime.halfmillis); //calcul average offset averageOffset.second=sumOffset.second/timeProt.correction.nbCorrection; averageOffset.halfmillis=sumOffset.halfmillis/timeProt.correction.nbCorrection; averageOffset.sign=sumOffset.sign; //display average /*printf("averageTime: "); printfClock(averageTime); printf("averageOffseet: "); printfClock(averageOffset);*/ //calcul of average time in hms unsigned long int diffhms=averageTime.second*RTC_FREQ+averageTime.halfmillis; //offset in hms long int offset=averageOffset.second*RTC_FREQ+averageOffset.halfmillis; if (averageOffset.sign==false) { offset=-offset; } //printf("offset : %lu\r\n",offset); //printf("diffhms : %lu\r\n",diffhms); float correctionSoft=0.0; float correctionTime=0.0; int corr; int corrTime; int nbwait; corr=valueCor; nbwait=timeCorrection; corrTime=nbwait; if(offset!=0){ //fcorr=(float)(10*offset)/(diffhms); //printf("correction need %d(%d)\r\n",(int)fcorr,nbwait); if(fcorr<0){ fcorr=-fcorr; } while((nbwait<1000)&&(fcorr<0.5)){ fcorr=(float)((float)VALUE_TIMER*nbwait*offset*1.0)/(diffhms); nbwait++; } //fcorr=fcorr*RTC_FREQ; // printf("correction need %d/10(%d)\r\n",(int)fcorr*10,nbwait); int i; timeProt.correction.moySoftCor[timeProt.correction.indiceMoySoftCor][0]=fcorr; timeProt.correction.moySoftCor[timeProt.correction.indiceMoySoftCor][1]=nbwait; timeProt.correction.indiceMoySoftCor++; if(indice<MOYCORLENGTH){//if ze have enougth space to sqve the value indice++; } if(timeProt.correction.indiceMoySoftCor>indice){ timeProt.correction.indiceMoySoftCor=0; } for(i=0;i<indice;i++){//go to the maximum value save // printf("i : %d, value : %d\r\n",i,(int)timeProt.correction.moySoftCor[i]); correctionSoft+=timeProt.correction.moySoftCor[i][0]; correctionTime+=timeProt.correction.moySoftCor[i][1]; } // printf("sumCorr : %d",(int)correctionSoft); correctionSoft=(float)(1.0*correctionSoft)/(indice*1.0);//calcul the average value of the software correction correctionTime=(float)(1.0*correctionTime)/(indice*1.0);//calcul the average value of the software correction corr=(int)correctionSoft;//cast to int corrTime=(int)correctionTime;//cast to int Disable_global_interrupt(); corr=valueCor+Coef_Software_Correction*(corr-valueCor); Enable_global_interrupt(); if(corr<0){ if(correctionSoft-corr<-0.5){//calculate int the most close to the real value corr--; } // printf("corr<0"); } else{ if(correctionSoft-corr>0.5){//calculate int the most close to the real value corr++; } // printf("corr>0"); } if(correctionTime-corrTime>0.5){//calculate int the most close to the real value corrTime++; } // printf("moy : %d",(int)correctionSoft*1000); // printf("corr: %u",corr); int pgcdi =pgcd(corrTime,corr); corrTime=corrTime/pgcdi; corr=corr/pgcdi; Disable_global_interrupt(); valueCor=valueCor+Coef_Software_Correction*(corr-valueCor); timeCorrection=corrTime; timeCorr=corrTime; Enable_global_interrupt(); timeProt.correction.indiceFull=indice; } resetSofftwareCorrection(); sprintf(messageHMICor," update time corr: %d(%d) sign : %d with %d sample",(int)valueCor,corrTime,averageOffset.sign,indice); sendHMI(messageHMICor); sendHMI(" end Software correction"); } }
void reduire(rationnel *a) { int pg = pgcd(a->p, a->q); a->p = a->p / pg; a->q = a->q / pg; }
gen _pgcd(const gen & args){ if ( (args.type!=_VECT) || (args._VECTptr->size()!=2)) setsizeerr(); vecteur &v=*args._VECTptr; return pgcd(v[0],v[1]); }
int main() { printf("%d\n", pgcd(17, 3)); return (0); }