Add div by zero check back into WMath::map (#4853)
* Add div by zero check back into WMath::map * include esp32-hal-log.h Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
This commit is contained in:
parent
5d00b6eb16
commit
3fe7c2e8cd
@ -27,6 +27,7 @@ extern "C" {
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
}
|
}
|
||||||
|
#include "esp32-hal-log.h"
|
||||||
|
|
||||||
void randomSeed(unsigned long seed)
|
void randomSeed(unsigned long seed)
|
||||||
{
|
{
|
||||||
@ -69,7 +70,10 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) {
|
|||||||
const long dividend = out_max - out_min;
|
const long dividend = out_max - out_min;
|
||||||
const long divisor = in_max - in_min;
|
const long divisor = in_max - in_min;
|
||||||
const long delta = x - in_min;
|
const long delta = x - in_min;
|
||||||
|
if(divisor == 0){
|
||||||
|
log_e("Invalid map input range, min == max");
|
||||||
|
return -1; //AVR returns -1, SAM returns 0
|
||||||
|
}
|
||||||
return (delta * dividend + (divisor / 2)) / divisor + out_min;
|
return (delta * dividend + (divisor / 2)) / divisor + out_min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user