# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2009-05-20 18:06:30

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### median - returns wrong value when using an odd number of arguments

Brief Description:            median - returns wrong value when using an odd number of arguments
GM Versions:                   GM6  @Fixed in GM7
References:                     <none>
Example File:                   <none>
Bug Confirmed:                Yes
Hardware Dependent:     No

Main Info:

When using the median function, if an odd number of arguments is given GM will return the next value to the median. For example when using:

median(1,3,5);      //1 is returned instead of 3
median(1,2,5,7,8);    //2 is returned instead of 5
median(3,3,4,5,5,6,7);  //4 is returned instead of 5

Workaround:

This bug is fixed in GM7 but if you need a work-around for GM6 you can write your own median function. Doing so you will need to specify the number of arguments given unlike with the in-built function.

//median(arg num,arg0,arg1,arg2,arg3,arg4...)
if (argument0 < 0) {show_error("Wrong argument number specified.",false); return 0;}
var i,list;
list = ds_list_create();
for (i = 1; i <= argument0; i += 1)
{
}
ds_list_sort(list,true);
if (argument0 == 0) {return 0;}
if (argument0 & 1)
{
return ds_list_find_value(list,(argument0+1)/2);
}
else
{
return (ds_list_find_value(list,argument0/2) + ds_list_find_value(list,argument0/2+1))/2;
}

Last edited by flexaplex (2009-09-04 12:47:55)

Offline

## #2 2009-05-28 17:41:27

Smarty
Member
Registered: 2008-01-23
Posts: 15

### Re: median - returns wrong value when using an odd number of arguments

The problem with median in GM6 was that it didn't work if there were 3 parameters. I remember this because I originally submitted the bug.

By the way, what's the point of keeping track of bugs in previous versions? If it's for backtracking purposes, the help file contains a list of fixed bugs with regard to the previous version, so you'll find all of them there...

Offline

## #3 2009-05-28 17:49:18

xot
Registered: 2007-08-18
Posts: 1,201

### Re: median - returns wrong value when using an odd number of arguments

Smarty wrote:

By the way, what's the point of keeping track of bugs in previous versions? If it's for backtracking purposes, the help file contains a list of fixed bugs with regard to the previous version, so you'll find all of them there...

But what you won't find is a list of bugs that haven't been fixed or bugs that have been broken in bold and exciting new ways. Since a lot of people still use GM5 and GM6 (including myself), I'd like to track these bugs as well.

Abusing forum power since 1986.

Offline

## #4 2009-05-28 18:04:15

Smarty
Member
Registered: 2008-01-23
Posts: 15

### Re: median - returns wrong value when using an odd number of arguments

Agreed, but then you should be able to get a head start here by copying in the list of fixes mentioned in the GM7 help file.

Offline

## #5 2009-05-29 15:48:46

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### Re: median - returns wrong value when using an odd number of arguments

I posted this one because it could be especially confusing, likely to be more common and was easy to follow without testing. I agree the bug list should be used to post all the GM6 bugs here however I cannot do so myself being on Vista. I am doubtful anyone will actually do it however considering the effort required.

You said the problem is if there were 3 parameters, other sources say it was any odd number of parameters?

Offline

## #6 2009-05-29 16:49:46

xot
Registered: 2007-08-18
Posts: 1,201

### Re: median - returns wrong value when using an odd number of arguments

The fact that this bug exists blows my mind. Wouldn't this come up in the very first test of the function? How can something like this be missed? I guess it must be a div 2  resulting in an off-by-one index, but still, no testing? Did Mark ever give you any insight into the cause of the problem, Smarty?

Oh yeah, since you asked, flex, I can officially confirm that the bug in GM6.1 is exactly as described at the top.

Abusing forum power since 1986.

Offline

## #7 2009-05-29 20:04:25

flexaplex
Member
Registered: 2008-12-11
Posts: 72

### Re: median - returns wrong value when using an odd number of arguments

xot wrote:

The fact that this bug exists blows my mind. Wouldn't this come up in the very first test of the function? How can something like this be missed? I guess it must be a div 2  resulting in an off-by-one index, but still, no testing? Did Mark ever give you any insight into the cause of the problem, Smarty?

Perhaps the function is so simple he didn't bother testing it. Or he only tested even number of parameters.

There's a bug in GM7 which Icuurd told me.

http://gmcfaq.forumdes.com/-*known-game … -t121.html
3rd one on the list.

The outside view event literally doesn't work at all with instances with a sprite. Strange how this was missed in testing.

Offline

## #8 2009-05-31 04:24:33

Smarty
Member
Registered: 2008-01-23
Posts: 15

### Re: median - returns wrong value when using an odd number of arguments

xot wrote:

The fact that this bug exists blows my mind. Wouldn't this come up in the very first test of the function? How can something like this be missed? I guess it must be a div 2  resulting in an off-by-one index, but still, no testing? Did Mark ever give you any insight into the cause of the problem, Smarty?

No, I never got confirmation or explanation of bugs I reported, but I assume nobody does.

Strange, I remember testing in GM6.1 whether more parameters work correctly. My test must have been flawed.

xot, I sent you a message about this - why am I not allowed to create new topics in this forum? Is it supposed to be managed by a few members only? I couldn't find any topic about this.

Offline

## #9 2009-05-31 14:20:52

xot
Registered: 2007-08-18
Posts: 1,201

### Re: median - returns wrong value when using an odd number of arguments

No, I never got confirmation or explanation of bugs I reported, but I assume nobody does.

That's probably true most of the time. I exchanged several emails with Mark when I reported GM7 bugs during its beta. I thought maybe a GM user of your stature might have as well.

why am I not allowed to create new topics in this forum? Is it supposed to be managed by a few members only? I couldn't find any topic about this.

At the moment, flexaplex and I are the only ones who can post new topics in this forum. I haven't made an official announcement about this forum yet, it's sort of a work in progress as flex and I hammer out the details. The idea is that this forum is for bugs that have been confirmed, can be reliably demonstrated with an example, have a viable workaround (when possible), and are presented using a standard template.

To report a new bug, use the GML Reference forum. Those bug reports will be moved or copied to this forum as they are verified. This may change. I'm still trying figure out the best way to do this because PunBB is does not have very good topic moderation tools compared to IPB. I may have to roll my own or just open up the forum completely and add the template by editing the topic starter's first post.

Abusing forum power since 1986.

Offline

## #10 2009-06-05 17:12:23

Smarty
Member
Registered: 2008-01-23
Posts: 15

### Re: median - returns wrong value when using an odd number of arguments

xot wrote:

I exchanged several emails with Mark when I reported GM7 bugs during its beta. I thought maybe a GM user of your stature might have as well.

Not about bugs. I have usually been pretty accurate in reporting bugs - a 'bare necessity' source to reproduce, a little investigation into the scope etc. I don't think there was need for further communication in all cases because there was nothing left to ask. Fortunately I've seen all bugs that I reported fixed in subsequent versions.

I'll try and use your template when reporting bugs.

Offline

## #11 2009-06-05 18:26:41

xot
Registered: 2007-08-18
Posts: 1,201

### Re: median - returns wrong value when using an odd number of arguments

Oh, so you're saying the only reason he emailed is because my bug reports stink, eh?

I think the impetus of the exchange was probably because I gave him the clue he needed to solve the precision problem introduced in GM6. I'm not sure if it is possible to jump for joy in an email, but I think he may have.

Abusing forum power since 1986.

Offline

## #12 2009-06-06 09:55:49

Smarty
Member
Registered: 2008-01-23
Posts: 15

### Re: median - returns wrong value when using an odd number of arguments

xot wrote:

Oh, so you're saying the only reason he emailed is because my bug reports stink, eh?

Or unable to reproduce.

I think the impetus of the exchange was probably because I gave him the clue he needed to solve the precision problem introduced in GM6. I'm not sure if it is possible to jump for joy in an email, but I think he may have.

Yes, I know - 3D initialisation in DirectX sets the FPU to single precision for reasons of optimisation. That's why it first occurred in GM6, as GM6 was the first edition with 3D routines.

You know, the bug was almost reintroduced in the C++ engine, for which I also tested the first alphas. This is probably because others took over development. Fortunately I was able to point them to an MS knowledge base about it.

Offline

## #13 2010-02-25 18:32:31

brac37
Member
Registered: 2010-02-12
Posts: 18

### Re: median - returns wrong value when using an odd number of arguments

As yourself pointed, the median function is useful for bounding a value to an interval: median(lower_bound, value, upper_bound). I guess median(lower_bound, value, upper_bound, upper_bound) works for all versions of game maker.

Sorting an array is too much work to obtain the median or any other "smallest but k" value. Lazy quicksort suffices and takes linear time approximately instead of an extra logarithmic factor. But probably, there is no way to do lazy quicksort without using a slow GML.

There is a faster solution: add max_dbl as an argument.

Offline

## #14 2010-02-25 21:09:59

xot
Registered: 2007-08-18
Posts: 1,201

### Re: median - returns wrong value when using an odd number of arguments

As yourself pointed, the median function is useful for bounding a value to an interval: median(lower_bound, value, upper_bound).

I'm finding myself using this more and more instead of my usual min(max(lower_bound, value), upper_bound). For some reason I still don't like using median this way, which is odd because it's perfectly logical, is faster, and coding it is less error prone (GM bugs aside).

Abusing forum power since 1986.

Offline