arduino / arduino/ArduinoCore-API

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

Đang mở
#85 6 bình luận 2 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
C++
Star
306
Fork
150
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu bằng cách xác định các định nghĩa Arduino API cho abs, constrain, round và bitWrite, sau đó kiểm tra các báo cáo liên quan để biết hành vi mong đợi và các vấn đề về tính tương thích. Thay thế hành vi macro có vấn đề bằng cách tiếp cận dùng hàm được đề xuất và xác minh rằng các đối số có tác dụng phụ được đánh giá đúng một lần mà không thay đổi public API.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
cpp
Lĩnh vực
api, embedded-iot
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.