x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 465

# median of 2 numbers. how to?

I have 2 numbers and trying to find median of those two eithout taking average manually but with built in functions. Any functions in javascript or prolog?

REGARDS.
0
jazzIIIlove
4 Solutions

Commented:
I don't think there is a built in median function in JavaScript. If you just want the median of two numbers, then it is the same as the mean (average), just (num1+num2)/2

Here is a function for median in JavaScript
http://rosettacode.org/wiki/Averages/Median#JavaScript
And one for Prolog on the same page
http://rosettacode.org/wiki/Averages/Median#Prolog
0

Commented:
The reason that it isn't usually a built in function is that it requires sorting the data to find the median. The most efficient sorts are "in-place" (i.e. they move the data around in the original array) so you would need to copy the array if you wanted to find the median without rearranging everything.
0

Author Commented:
Please assume they are sorted and only 2 numbers.

Regards.
0

Commented:
``````function median(a, b) {
return (a+b)/2;
}
``````

Or if they are in an array
``````function median_array_of_2(ary) {
return (ary[0]+ary[1])/2;
}
``````
0

Commented:
the definition of median function for an even number of values may be arbitrary. sometimes, it is required that the median value matches at least one of the values of the set. if so, for an even count you may have two medians (low and high) which are the two middle values of the sorted set.

Sara
0

Commented:
A Median is the mid-point of a "sample" but when the "sample" is an even number no true mid-point exists. The most typical way to treat this is to then take the mean (average) of the 2 points being before and after the mid-point. Some approaches differ and will choose either the 'before' value or the 'after' value instead of using the mean of those 2 points.

The statistical significance of a sample that is only of 2 values is so low that you really have not much choice except to use the average as any other technique applied would just produce an absurd bias.

http://en.wikipedia.org/wiki/Median
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.