arduino / arduino/ArduinoCore-API

Use of macros for math functions causes unexpected results when argument has side effect

Aperta
#85 6 commenti 2 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
C++
Stelle
306
Fork
150
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Inizia individuando le definizioni dell’API Arduino per abs, constrain, round e bitWrite, quindi esamina le segnalazioni correlate per il comportamento previsto e i problemi di compatibilità. Sostituisci il comportamento problematico della macro con l’approccio basato sulla funzione proposto e verifica che gli argomenti con effetti collaterali vengano valutati una sola volta senza modificare l’API pubblica.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp
Ambito
api, embedded-iot
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.