22#include " wled.h"
33#include " fcn_declare.h"
44
5+ // on esp8266, building with `-D WLED_USE_UNREAL_MATH` saves around 7Kb flash and 1KB RAM
6+ // warning: causes errors in sunset calculations, see #3400
7+ #if defined(WLED_USE_UNREAL_MATH)
8+ #define sinf sin_t
9+ #define asinf asin_t
10+ #define cosf cos_t
11+ #define acosf acos_t
12+ #define tanf tan_t
13+ #define atanf atan_t
14+ #define fmodf fmod_t
15+ #define floorf floor_t
16+ #else
17+ #include < math.h>
18+ #endif
19+
520/*
621 * Acquires time from NTP server
722 */
@@ -412,8 +427,8 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
412427 // 1. first calculate the day of the year
413428 float N1 = 275 * month / 9 ;
414429 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 ;
430+ float N3 = (1 . 0f + floorf ((year - 4 * floorf (year / 4 ) + 2 . 0f ) / 3 . 0f ));
431+ float N = N1 - (N2 * N3) + day - 30 . 0f ;
417432
418433 // 2. convert the longitude to hour value and calculate an approximate time
419434 float lngHour = lon / 15 .0f ;
@@ -423,37 +438,37 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
423438 float M = (0 .9856f * t) - 3 .289f ;
424439
425440 // 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 );
441+ float L = fmodf (M + (1 .916f * sinf (DEG_TO_RAD*M)) + (0 .02f * sinf (2 *DEG_TO_RAD*M)) + 282 .634f , 360 .0f );
427442
428443 // 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 );
444+ float RA = fmodf (RAD_TO_DEG*atanf (0 .91764f * tanf (DEG_TO_RAD*L)), 360 .0f );
430445
431446 // 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 ;
447+ float Lquadrant = floorf ( L/90 ) * 90 ;
448+ float RAquadrant = floorf (RA/90 ) * 90 ;
434449 RA = RA + (Lquadrant - RAquadrant);
435450
436451 // 5c. right ascension value needs to be converted into hours
437452 RA /= 15 .0f ;
438453
439454 // 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));
455+ float sinDec = 0 .39782f * sinf (DEG_TO_RAD*L);
456+ float cosDec = cosf ( asinf (sinDec));
442457
443458 // 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)
459+ float cosH = (sinf (DEG_TO_RAD*ZENITH) - (sinDec * sinf (DEG_TO_RAD*lat))) / (cosDec * cosf (DEG_TO_RAD*lat));
460+ if (( cosH > 1 . 0f ) && !sunset) return 0 ; // the sun never rises on this location (on the specified date)
461+ if (( cosH < -1 . 0f ) && sunset) return 0 ; // the sun never sets on this location (on the specified date)
447462
448463 // 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);
464+ float H = sunset ? RAD_TO_DEG*acosf (cosH) : 360 - RAD_TO_DEG*acosf (cosH);
450465 H /= 15 .0f ;
451466
452467 // 8. calculate local mean time of rising/setting
453468 float T = H + RA - (0 .06571f * t) - 6 .622f ;
454469
455470 // 9. adjust back to UTC
456- float UT = fmod_t (T - lngHour, 24 .0f );
471+ float UT = fmodf (T - lngHour, 24 .0f );
457472
458473 // return in minutes from midnight
459474 return UT*60 ;
0 commit comments