0

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.

george24
  • 153
  • casting to int? –  Jul 30 '15 at 08:44
  • 2
    Mathematics has the floor (and ceiling) operations, using "hooked" parentheses. See e.g. https://en.wikipedia.org/wiki/Floor_and_ceiling_functions . You'll need to take into account whether your average is positive or negative to use those operations. –  Jul 30 '15 at 08:45
  • I am looking for a "mathematical expression", describing that only the "integer part" of the result is being considered. – george24 Jul 30 '15 at 08:49
  • But the wikipedia link I gave shows how you can define truncation as well; applying that to average, you'd get something like \sgn(1/n \Sigma x_i) \lfloor | 1/n Sigma x_i | \rfloor . –  Jul 30 '15 at 08:56
  • It's not clear what you want. The floor and ceiling functions cause a number to become an integer, but you aren't liking that answer. You can say floor(8/3)=2, but you can't say 8/3 ∈ N, because that's false. ∈ N is a statement, not a conversion of one number to another. – Teepeemm Jul 30 '15 at 12:58
  • I'm voting to close this question as off-topic because it is not about programming. –  Jul 30 '15 at 16:07

4 Answers4

1

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);
1

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]
insidesin
  • 111
1

$$ \text{sgn} \left( \frac{1}{n} \Sigma x_i \right) \left\lfloor \left\lvert \frac{1}{n} \Sigma x_i \right\rvert \right\rfloor $$

-1

Mathematically, Avg(n1,n2,...nm) = p/q for lowest values (no more divisible) for all p belongs to natural number and q =1.

AKS
  • 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/3 is lowest values, with p=8 and q=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 intelligent person understand... –  Jul 30 '15 at 13:09