arduino / arduino/ArduinoCore-API
Use of macros for math functions causes unexpected results when argument has side effect
- Lenguaje dominante
- C++
- Estrellas
- 306
- Forks
- 150
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
I find semantically wrong the fact that the following math functions are implemented as macros:
- `abs`
- `constrain`
- `round`
- ~~`min`~~
- ~~`max`~~
... and let me add `bitWrite` to the list.
It is well known that using these function the way the rest of the world does introduces faults, and therefore generates failures.
Writing something like `abs(cos(x) - 123 * sq(sin(x))` would introduce **redundant calculations**, just a crazy delay in this case...
Writing something like `abs(a += b)` or `abs(i++)` just introduces a **fault**... it does something different (wrong in fact).
I assume that using **template** and **inline** functions would solve the issue with no extra executable space taken.
### Additional context
#### Additional reports
- https://github.com/arduino/Arduino/issues/9571
- https://github.com/arduino/ArduinoCore-avr/issues/324
#### Related
- https://github.com/arduino/reference-en/pull/513
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Línea de trabajo
Comienza localizando las definiciones de la API de Arduino para abs, constrain, round y bitWrite; después, revisa los informes relacionados para conocer el comportamiento esperado y las consideraciones de compatibilidad. Sustituye el comportamiento problemático de la macro por el enfoque basado en funciones propuesto y verifica que los argumentos con efectos secundarios se evalúen una sola vez sin cambiar la API pública.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- cpp
- Área
- api, embedded-iot
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100