arduino / arduino/ArduinoCore-API
Use of macros for math functions causes unexpected results when argument has side effect
- Vorherrschende Sprache
- C++
- Sterne
- 306
- Forks
- 150
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Beginne damit, die Arduino-API-Definitionen für abs, constrain, round und bitWrite zu finden, und prüfe anschließend die zugehörigen Berichte auf erwartetes Verhalten und Kompatibilitätsaspekte. Ersetze das problematische Makroverhalten durch den vorgeschlagenen Funktionsansatz und verifiziere, dass Argumente mit Seiteneffekten einmal ausgewertet werden, ohne die öffentliche API zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- cpp
- Bereich
- api, embedded-iot
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100