Skip to content

Instantly share code, notes, and snippets.

@dirkmueller
Created September 28, 2020 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkmueller/c1ec0aa8c124caccf181b21a45943657 to your computer and use it in GitHub Desktop.
Save dirkmueller/c1ec0aa8c124caccf181b21a45943657 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// esp8266 code size using double's: 223 bytes
// esp8266 code size using floats: 204 bytes
float datasheet_calc(int stemp) {
float temp = (stemp * 175.0f) / 65535.0f - 45.0f;
return temp;
}
// esp8266 code size 199 bytes
float simplified_calc(int stemp) {
stemp = ((4375 * stemp) >> 14) - 4500;
float temp = (float)stemp / 100.0f;
return temp;
}
// esp8266 code size 199 bytes
float improved_int_calc(int stemp) {
const int i_scale = 128;
stemp = (((i_scale/4 * 175) * stemp) >> 14) - (i_scale * 45);
float temp = (float)stemp / (float) i_scale;
return temp;
}
int main()
{
float maxdiff_simplified = 0.0f;
float maxdiff_improved = 0.0f;
for (int i = (-40 + 45) * 375; i < /*65536*/ (90+45) * 375; ++i) {
float ds_temp = datasheet_calc(i);
float im_temp = improved_int_calc(i);
float sc_temp = simplified_calc(i);
//printf("%d: comparing %f with %f = %f\n",
// i, ds_temp, sc_temp, fabsf(ds_temp - sc_temp));
if (fabs(ds_temp - sc_temp) > maxdiff_simplified) {
maxdiff_simplified = fabsf(ds_temp - sc_temp);
}
if (fabs(ds_temp - im_temp) > maxdiff_improved) {
maxdiff_improved = fabsf(ds_temp - im_temp);
}
}
printf("maxdiff_simplified = %f maxdiff_improved = %f\n", maxdiff_simplified, maxdiff_improved);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment