# findMaximum bug - ends up finding minimum

#1

This is in juce_MathsFunctions.h. To start off, here’s findMinimum:

[code] /** Scans an array of values, returning the minimum value that it contains. /
template
const Type findMinimum (const Type
data, int numValues)
{
if (numValues <= 0)
return Type();

`````` Type result (*data++);

while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
{
const Type& v = *data++;
if (v < result)  result = v;
}

return result;
``````

}[/code]

Makes sense. Now here’s findMaximum():

[code]/** Scans an array of values, returning the minimum value that it contains. /
template
const Type findMaximum (const Type
values, int numValues)
{
if (numValues <= 0)
return Type();

`````` Type result (*values++);

while (--numValues > 0) // (> 0 rather than >= 0 because we've already taken the first sample)
{
const Type& v = *values++;
if (result > v)  result = v;
}

return result;
``````

}[/code]

You can see the comment at the top for findMaximum still says it finds the minimum, but that’s not the problem. The problem is that findMinimum is doing this:

``````     if (v < result)  result = v;
``````

and findMaximum is doing this

``````     if (result > v)  result = v;
``````

which are the same thing.

#2

Bah, I failed the test to report bugs correctly. OK, the code I’m referring to is right here

http://juce.git.sourceforge.net/git/gitweb.cgi?p=juce/juce;a=blob;f=modules/juce_core/maths/juce_MathsFunctions.h

I believe that this is the latest revision, but I’m a bit new to this newfangled web-based git thing. I noticed the problem on an older version of JUCE that I have (v1.54.27) that I’ve been holding off on updating because I need to finish debugging the code I’m working on before doing something like that. However, I checked and it does appear to still be a bug in even the latest revision on git, so I do believe that this is a valid bug to report.

As per the usual bug reporting protocol, I’m on Mac OS X Snow Leopard.

#3

Looks like I totally buggered that one up, didn’t I!

Thanks for letting me know, I’ll get it fixed imminently…