Skip to content

max() not accepting #defined values #6623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
palmerr23 opened this issue Apr 25, 2022 · 9 comments · Fixed by #6726
Closed
1 task done

max() not accepting #defined values #6623

palmerr23 opened this issue Apr 25, 2022 · 9 comments · Fixed by #6726
Assignees
Labels
Milestone

Comments

@palmerr23
Copy link

Board

DevkitC ESP32 WROOM

Device Description

None

Hardware Configuration

No

Version

latest development Release Candidate (RC-X)

IDE Name

Arduino 1.8.13

Operating System

Win 11

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

921600

Description

Max is not accepting #defined values

The second call to max() fails to compile in ESP32, nut OK in AVR.

Issue occurs in both 2.0.2 and 2.0.3_RC1

Sketch

#define MULT 0.7
void setup() {
  float a = 1.1, b = 2.2, c;  
  c = max(a, b);
  c = max(a, MULT);
}

void loop() {
}

Debug Message

C:\Users\xyzzy.DESKTOP-J4ESB3N\Documents\dad\_microcontroller\esp\esp32\max_test\max_test.ino: In function 'void setup()':
max_test:5:17: error: no matching function for call to 'max(float&, double)'
   c = max(a,MULT);
                 ^
In file included from c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\algorithm:62,
                 from C:\Users\xyzzy.DESKTOP-J4ESB3N\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.3-RC1\cores\esp32/Arduino.h:155,
                 from sketch\max_test.ino.cpp:1:
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3468:5: note: candidate: 'template<class _Tp, class _Compare> _Tp std::max(std::initializer_list<_Tp>, _Compare)'
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
C:\Users\xyzzy.DESKTOP-J4ESB3N\Documents\dad\_microcontroller\esp\esp32\max_test\max_test.ino:5:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'float'
   c = max(a,MULT);
                 ^
In file included from c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\algorithm:62,
                 from C:\Users\xyzzy.DESKTOP-J4ESB3N\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.3-RC1\cores\esp32/Arduino.h:155,
                 from sketch\max_test.ino.cpp:1:
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3462:5: note: candidate: 'template<class _Tp> _Tp std::max(std::initializer_list<_Tp>)'
     max(initializer_list<_Tp> __l)
     ^~~
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
C:\Users\xyzzy.DESKTOP-J4ESB3N\Documents\dad\_microcontroller\esp\esp32\max_test\max_test.ino:5:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'float'
   c = max(a,MULT);
                 ^
In file included from c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\algorithm:61,
                 from C:\Users\xyzzy.DESKTOP-J4ESB3N\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.3-RC1\cores\esp32/Arduino.h:155,
                 from sketch\max_test.ino.cpp:1:
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:265:5: note: candidate: 'template<class _Tp, class _Compare> const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
C:\Users\xyzzy.DESKTOP-J4ESB3N\Documents\dad\_microcontroller\esp\esp32\max_test\max_test.ino:5:17: note:   deduced conflicting types for parameter 'const _Tp' ('float' and 'double')
   c = max(a,MULT);
                 ^
In file included from c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\algorithm:61,
                 from C:\Users\xyzzy.DESKTOP-J4ESB3N\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.3-RC1\cores\esp32/Arduino.h:155,
                 from sketch\max_test.ino.cpp:1:
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:219:5: note: candidate: 'template<class _Tp> const _Tp& std::max(const _Tp&, const _Tp&)'
     max(const _Tp& __a, const _Tp& __b)
     ^~~
c:\users\xyzzy.desktop-j4esb3n\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\bits\stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
C:\Users\xyzzy.DESKTOP-J4ESB3N\Documents\dad\_microcontroller\esp\esp32\max_test\max_test.ino:5:17: note:   deduced conflicting types for parameter 'const _Tp' ('float' and 'double')
   c = max(a,MULT);

Other Steps to Reproduce

Nothing else, simple compile issue.

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@palmerr23 palmerr23 added the Status: Awaiting triage Issue is waiting for triage label Apr 25, 2022
@palmerr23
Copy link
Author

Sorry, Arduino IDE is 1.8.19

@lbernstone
Copy link
Contributor

lbernstone commented Apr 25, 2022

If you want to ensure it is a float, cast it as a float:
#define MULT (float)0.7

@palmerr23
Copy link
Author

Thanks for the tip, however I'm already aware of the workarounds. BTW, one should not need to cast a clearly floating point number as float, despite the fact that it works!
I was merely pointing out that this compiler's response to preprocessor insertions is different from "Arduino standard."

@lbernstone
Copy link
Contributor

Yes, the authors of gcc do not seem to care much about the "Arduino standard"

@SuGlider
Copy link
Collaborator

Arduino Standard defines a macro for max()
#define max(a,b) ((a)>(b)?(a):(b))
https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Arduino.h#L93

ESP32 Arduino doesn't define such macro...
it has something else instead:
#define _max(a,b) ((a)>(b)?(a):(b))
https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/Arduino.h#L207

@palmerr23 - does your sketch behave the same as Arduino Standard if you use _max() instead of max()?

@SuGlider SuGlider self-assigned this Apr 27, 2022
@SuGlider
Copy link
Collaborator

A workaround for now is to add this code to the first line of your sketch:

#define max(a,b) _max(a,b)

I'll verify if we can add a patch to solve it for good.

@palmerr23
Copy link
Author

Yes, the workaround does work as you describe.

It would be useful to have _max() and max() doing the same thing, preferably the same as Arduino "standard".

Thanks for your efforts so far on this.

@VojtechBartoska VojtechBartoska moved this to Under investigation in Arduino ESP32 Core Project Roadmap Apr 28, 2022
@SuGlider SuGlider added Status: To be implemented Selected for Development Status: In Progress ⚠️ Issue is in progress and removed Status: Awaiting triage Issue is waiting for triage labels May 3, 2022
@VojtechBartoska VojtechBartoska added this to the 2.0.4 milestone May 4, 2022
@VojtechBartoska VojtechBartoska moved this from Under investigation to In Progress in Arduino ESP32 Core Project Roadmap May 9, 2022
@SuGlider SuGlider moved this from In Progress to Done in Arduino ESP32 Core Project Roadmap May 10, 2022
@SuGlider
Copy link
Collaborator

@palmerr23 - There is no way to add max()/min() to the Arduino.h because it conflicts with functions with the same name used in the C++ environment of the compiler.

Therefore, the work around proposed is the only way to go.

I added a few other functions to Arduino.h in the PR #6726

@palmerr23
Copy link
Author

Thanks for trying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging a pull request may close this issue.

4 participants