# findMaximum bug - ends up finding minimum

**URL:** <https://forum.juce.com/t/findmaximum-bug-ends-up-finding-minimum/8207>\
**Category:** General JUCE discussion\
**Created:** [February 25, 2012, 4:47am UTC](https://forum.juce.com/t/findmaximum-bug-ends-up-finding-minimum/8207 "2012-02-25T04:47:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![battaglia01](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@battaglia01](https://forum.juce.com/u/battaglia01)\
**Post date:** [February 25, 2012, 4:47am UTC](https://forum.juce.com/t/findmaximum-bug-ends-up-finding-minimum/8207/1 "2012-02-25T04:47:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![battaglia01](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@battaglia01](https://forum.juce.com/u/battaglia01)\
**Post date:** [February 25, 2012, 4:51am UTC](https://forum.juce.com/t/findmaximum-bug-ends-up-finding-minimum/8207/2 "2012-02-25T04:51:59Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 25, 2012, 10:36am UTC](https://forum.juce.com/t/findmaximum-bug-ends-up-finding-minimum/8207/3 "2012-02-25T10:36:04Z")

</div>

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

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