Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit 166316e

Browse files
committed
fix for wled#3400
replace low_accuracy math functions (sint_t, cos_t, atan_t, ...) with standard libm functions that have higher accuracy.
1 parent 5eadbe7 commit 166316e

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

wled00/ntp.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
412412
//1. first calculate the day of the year
413413
float N1 = 275 * month / 9;
414414
float N2 = (month + 9) / 12;
415-
float N3 = (1 + floor_t((year - 4 * floor_t(year / 4) + 2) / 3));
416-
float N = N1 - (N2 * N3) + day - 30;
415+
float N3 = (1.0f + floorf((year - 4 * floorf(year / 4) + 2.0f) / 3.0f));
416+
float N = N1 - (N2 * N3) + day - 30.0f;
417417

418418
//2. convert the longitude to hour value and calculate an approximate time
419419
float lngHour = lon / 15.0f;
@@ -423,37 +423,37 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
423423
float M = (0.9856f * t) - 3.289f;
424424

425425
//4. calculate the Sun's true longitude
426-
float L = fmod_t(M + (1.916f * sin_t(DEG_TO_RAD*M)) + (0.02f * sin_t(2*DEG_TO_RAD*M)) + 282.634f, 360.0f);
426+
float L = fmodf(M + (1.916f * sinf(DEG_TO_RAD*M)) + (0.02f * sinf(2*DEG_TO_RAD*M)) + 282.634f, 360.0f);
427427

428428
//5a. calculate the Sun's right ascension
429-
float RA = fmod_t(RAD_TO_DEG*atan_t(0.91764f * tan_t(DEG_TO_RAD*L)), 360.0f);
429+
float RA = fmodf(RAD_TO_DEG*atan(0.91764f * tan(DEG_TO_RAD*L)), 360.0f);
430430

431431
//5b. right ascension value needs to be in the same quadrant as L
432-
float Lquadrant = floor_t( L/90) * 90;
433-
float RAquadrant = floor_t(RA/90) * 90;
432+
float Lquadrant = floorf( L/90) * 90;
433+
float RAquadrant = floorf(RA/90) * 90;
434434
RA = RA + (Lquadrant - RAquadrant);
435435

436436
//5c. right ascension value needs to be converted into hours
437437
RA /= 15.0f;
438438

439439
//6. calculate the Sun's declination
440-
float sinDec = 0.39782f * sin_t(DEG_TO_RAD*L);
441-
float cosDec = cos_t(asin_t(sinDec));
440+
float sinDec = 0.39782f * sinf(DEG_TO_RAD*L);
441+
float cosDec = cosf(asinf(sinDec));
442442

443443
//7a. calculate the Sun's local hour angle
444-
float cosH = (sin_t(DEG_TO_RAD*ZENITH) - (sinDec * sin_t(DEG_TO_RAD*lat))) / (cosDec * cos_t(DEG_TO_RAD*lat));
445-
if (cosH > 1 && !sunset) return 0; // the sun never rises on this location (on the specified date)
446-
if (cosH < -1 && sunset) return 0; // the sun never sets on this location (on the specified date)
444+
float cosH = (sinf(DEG_TO_RAD*ZENITH) - (sinDec * sinf(DEG_TO_RAD*lat))) / (cosDec * cosf(DEG_TO_RAD*lat));
445+
if ((cosH > 1.0f) && !sunset) return 0; // the sun never rises on this location (on the specified date)
446+
if ((cosH < -1.0f) && sunset) return 0; // the sun never sets on this location (on the specified date)
447447

448448
//7b. finish calculating H and convert into hours
449-
float H = sunset ? RAD_TO_DEG*acos_t(cosH) : 360 - RAD_TO_DEG*acos_t(cosH);
449+
float H = sunset ? RAD_TO_DEG*acosf(cosH) : 360 - RAD_TO_DEG*acosf(cosH);
450450
H /= 15.0f;
451451

452452
//8. calculate local mean time of rising/setting
453453
float T = H + RA - (0.06571f * t) - 6.622f;
454454

455455
//9. adjust back to UTC
456-
float UT = fmod_t(T - lngHour, 24.0f);
456+
float UT = fmodf(T - lngHour, 24.0f);
457457

458458
// return in minutes from midnight
459459
return UT*60;

0 commit comments

Comments
 (0)