Go Down

Topic: max() and min() functions not available (Read 8533 times) previous topic - next topic

lynchzilla

Hello Arduino Zero Forum.

In attempting to test an Adafruit 2.8" TFT with Capacitive Touchpad, I discovered that LadyAda's graphicstest.ino example would not compile. Seems that it can't see the min() and max() functions.
I am using Arduino IDE revision 1.6.5

I boiled it down to a simple example - adding some math to the standard "blink" example.


// the setup function runs once when you press reset or power the board
void setup() {
int    x,y,z;

// initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);

  x = 10;
  y = 20;
  z = min(x, y);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}


The error message is as follows:

Arduino: 1.6.5 (Windows 7), Board: "Arduino Zero (Programming Port)"

Build options changed, rebuilding all

sketch_jun19b.ino: In function 'void setup()':
sketch_jun19b:10: error: 'min' was not declared in this scope
'min' was not declared in this scope


The exact same code compiles and runs properly on an Arduino Uno.

Cheers,
Jim Lynch :smiley-confuse:

Denbo

Really strange.  Even redefining the max macro in the program did not work for me.  I tried the following:
Code: [Select]
#undef max
#define max(a,b) ((a)>(b)?(a):(b))
void setup()
{
  int z = max(10,5);
}

Still received an error that max was not defined.  I looked and it is defined in the wiring_constant.h file in the SAMD core directory.

It's a bug but I don't know why.   As a temp solution you could try the following:
Code: [Select]
inline int max(int a,int b) {return ((a)>(b)?(a):(b)); }
void setup()
{
  int z = max(10,5);
}

lynchzilla

Thanks Denbo for verifying my observation about the built-in min() and max() functions.

Your work-around works for the simple case, but to get LadyAda's graphics library to compile correctly would require modification of her graphics subroutines - it would be more prudent for me to wait.

Is posting to this forum sufficient to alert the Arduino Zero team about this bug?

Cheers, Jim Lynch


Denbo

By the way it will work for the Adafruit_GFX library but it must be in the library itself after the include to Arduino.h.  You can add in the last 3 lines of code as follows in Adafruit_GFX.h

Code: [Select]
#ifndef _ADAFRUIT_GFX_H
#define _ADAFRUIT_GFX_H

#if ARDUINO >= 100
 #include "Arduino.h"
 #include "Print.h"
#else
 #include "WProgram.h"
#endif

// add in the next 3 lines
#undef min
inline int min(int a, int b) { return ((a)<(b) ? (a) : (b)); }
inline double min(double a, double b) { return ((a)<(b) ? (a) : (b)); }

wholder


cmaglie

It's a really weird bug, it seems that if "max" or "min" are defined, simply including <stdint.h> will make them disappear (undefine in some way? I coulnd't find where this happens exactly in stdint.h....)

BTW I've opened a pull request with a fix here:

https://github.com/arduino/ArduinoCore-samd/pull/12

if someone could try it before merging that would be wonderful.
C.

wholder

#7
Jun 22, 2015, 07:09 pm Last Edit: Jun 22, 2015, 07:10 pm by wholder
if someone could try it before merging that would be wonderful.

I tried making the changes manually and they seem to fix the problem.  At least, statements like this now compile:

 int temp = min(1,2);
  int tmp2 = max(2,4);


Wayne

lynchzilla

May I ask when the fix for this issue will make it into the ARDUINO SOFTWARE
HOURLY BUILDS
in the download section?

cmaglie

We are setting up also the hourly build... you have to be a bit patient!
C.

lynchzilla

Not to worry - I raised two teenagers by myself. Patience is my best virtue.

Markus_L811


cmaglie

The nightly builds are now available!

Here the instructions:

https://github.com/arduino/ArduinoCore-samd#hourly-builds

enjoy!
C.

lynchzilla

OK, that worked - LadyAda's LCD displaytest nowruns properly for me.

Hope the fix gets into an official release soon.   :)

Go Up