Scenario: Suppose I have three numbers: 1, 3 and 4. The average of the three numbers are 2.67 (to 2 decimal places). It many real-world applications, there is the need to consider only the "whole number", that is, the integer part, hence the value 2 only.
Question: How do I define using mathematical expression this particular issue, that is, consider only the whole number result of the average expression?
Thank you.
- 153
4 Answers
You can define the integral part using Greatest integer function(represented as |_ _|).
So, greatest integer(2.67) = |_ 2.67 _| = 2.
And, in almost all of the programming languages there exists methods/functions defined in mathematical libraries to achieve the same.
In Java, you can achieve the same using double floor(double d) method.
Ex :- Math.floor(2.67) = 2.0 and then you can convert it into int using down-casting OR down promotion.
int res = (int)(Math.floor(2.67));
As mentioned by Evert, if you're performing operations with negative average, then you need to use smallest-integer function(ceil function).
int res = (int)(Math.ceil(2.67));
So, your code would be :-
if(avg>=0)
Math.floor(avg);
else
Math.ceil(avg);
-
-
-
For that OP can have an if-else condition. Wait, I'm editing that in my answer. – Jul 30 '15 at 08:48
-
-
Integers in Java don't use remainders, so if you were to add all values together and divide by N (the number of values), you should arrive at the greatest whole number.
This is in no way a highly accurate method of doing it, but it is definitely a fast solution that "works".
int a, b, c;
a = 1; b = 3; c = 4;
System.out.println((a + b + c) / 3); //Equals 2 [replace 3 with N]
- 111
-
-
-
Java, C, C++, and many others use integer division and this would work fine. Python (version 3+) and Javascript use floating point division. – Teepeemm Jul 30 '15 at 13:38
-
That's probably why I'm over-assuming, I'm not a scripting guy (yet) :P @Teepeemm – Jul 30 '15 at 13:40
$$ \text{sgn} \left( \frac{1}{n} \Sigma x_i \right) \left\lfloor \left\lvert \frac{1}{n} \Sigma x_i \right\rvert \right\rfloor $$
Mathematically,
Avg(n1,n2,...nm) = p/q for lowest values (no more divisible) for all p belongs to natural number and q =1.
- 101
- 1
-
Mathematically, that will only happen if the average is already an integer, making this question (whatever it is) moot. – Teepeemm Jul 30 '15 at 12:45
-
@Teepeee...Sorry but looks you don't aware about algos, it was just an psuedo. If you didn't understand, you may ask. – Jul 30 '15 at 12:51
-
@eorge24 , pls. accept answer if it solves your problem. Do you need programming solution , pseudo, algo. You may clarify if the answers are still acceptable. – Jul 30 '15 at 12:54
-
1
Avg(1,3,4) = 8/3is lowest values, withp=8andq=3≠1. What part of your algorithm am I misunderstanding? – Teepeemm Jul 30 '15 at 12:55 -
@Teepeemm, Oh that is not I am trying to explain. For "mathematical expression that is, consider only the whole number" , your 8/3 is not giving the whole number per the rule. To get and confirm if the whole number is achieved , the expression can be formulated. With the provided example, the output will not be fully whole number and we need a so called to trunk or floor or ceil the value. So it is a first step.if it fails/not fulfilled then user has confirmed that he need to use special functions (or he need alto) will be cleared after first step. – Jul 30 '15 at 13:03
-
@Teepeemm-It seems the author of this answer is unaware of all other answers posted here and himself written a crap and bewildering solution. You may better downvote and flag rather than make this
intelligentperson understand... – Jul 30 '15 at 13:09
floor(8/3)=2, but you can't say8/3 ∈ N, because that's false.∈ Nis a statement, not a conversion of one number to another. – Teepeemm Jul 30 '15 at 12:58