How do I calculate the average of two numbers?

how do I calculate the average of two numbers?

Other urls found in this thread:

ams.org/journals/proc/1999-127-06/S0002-9939-99-05167-9/S0002-9939-99-05167-9.pdf
twitter.com/NSFWRedditVideo

(a + b) / 2

>inb4 i meant integers
>inb4 i meant integers in C

What kind of average

7

If we knew how to do that we would already have colonized the universe

I meant integers in C

(a + b) >> 1

Jesus christ this is 4th grade math how fucking stupid are you?

what kind of brainlet question is that?

Then you fucked up.

a/2 + b/2 + (a%2 + b%2)/2
prevents integer overflow on addition

>t. brainlets who don't know what an overflow is

Doesn't C have some build in library for doing this?

Why not just do:
int avg(int a, int b)
{
return (((long long int) a) + ((long long int b)) / 2;
}

looks expensive

it's not, computers are optimized for this sort of thing

can I optimize my computer for different kind of things? ;-)

install gentoo

Why is it a problem if you do it like this with an integer?
Is it because you don't get decimals?

of course you can ;^(

you do get decimals, it''s just that this fact isn't represented by the integers

Wait, what?
So if I do let's say 1/4 the computer still treats it as 0.25 it just won't display it as such?
So if I fed my integer into a double afterwards it would come out right as 0.25 and not as 0?

That's not how integer division works, m9.
It's some kind of magic trick and in the end you get 0

Integer division, the way I understand it, divides normally but then quite literally just cuts off the decimals instead of rounding them or anything. So 1/4 would just end up as a 0 on your screen.
Now this guy is saying something about an integer still having its decimals but them just not being displayed or some shit and I was asking what exactly he means by that.

if (a + b != 0)
return((a + b) / 2)

That if statement is completely redundant.

lmao

yes, the remainder will be shown after casting it to a floating point value

>avoiding divide by 0
>redundant

he's obviously checking for division by 0 idiot

0/2 is perfectly fine, you rube.
Not only that it takes up less resources than throwing in an if check for every single run as well.

int A, B;
avg = A + B;
while ( | |avg - A| - |avg - B| | > 1 ) {
avg = avg - 1;
}
if ( | |avg - A| - |avg - B| | = 1 ) {
avg = avg - 0.5;
}
return avg;

if (a != -b)
return (a + b) / 2

lol

0/2 is undefined. there is no integer x such that 0x = 2

if ((a != b) == true)
return (a + b) / 2

0/2 is 0 you dumbass.
2/0 is undefined.

holy shit is this guy serious

being this stupid

Name a single language or IDE where 0/2 doesn't just return null instantly.
This shit is very clearly defined in any even somewhat modern environment.

What?
0x = 2 is the same as x = 2/0, not 0/2.

Why would you check for that?
The average of 'n' and '-n' is in fact 0, that is correct.

computer science is dumb

Google.

lmao
2/0 = 0. How many times does 0 go into 2? 0 times, because no matter how many times you add, you can never get to 2
0/2 is undefined, because you can't put something that's bigger than 0 into 0

>his language overflows

okay sure but first prove that 0 exists

thanks. you made me laff irl.

You can't be serious, did you skip high school?

long average(long a, long b) {
a += b;
for (long c = 0; c >= b/2; c--) {
a--;
}
return a;

You still need to take into account the fact when the average is a decimal number:
long average(long a, long b) {
a += b;
for (long c = 0; c >= b/2; c--) {
a--;
}
if (c > (a + b) / 2)
return average(c, (a + b) / 2);
return a;

(double) (a + b) / 2.0

(int)

So this is the power of Sup Forums
I'm out.

...

low + ((high - low) / 2)

This avoids any potential overflows from adding a and b together.

Gotta buy a $180 TI 84 CE for that complicated shit
t. CS grad student

>$180

>(((long long int)))
mfw when a jew takes the form of an integer variable

double f(double a, double b){
return (a + b) / 2
}


burp

kek

Integer division is simple as fuck.
10\3
Subtract 3 as many times as possible.
10-3=7-3=4-3=1.
You can subtract 3 3 times from 10 so 10\3 is 3. Remainder is 1.
Do you guys serously think it’s some shit where the computer calculates the floating point .33 and then chooses to forget it because it’s declared as an integer?

This is 3rd-4th grade math you bloody brainlet

he didnt go to school, his uncle Jose taught him how to deal with numbers

10/10 bait, holy shit

This is what happens when you give weebs a computer.

>scientists
This is why engineers are superior.

Point numbers on 1d graph and split it into half theb bring something to measure

here

ams.org/journals/proc/1999-127-06/S0002-9939-99-05167-9/S0002-9939-99-05167-9.pdf

Hey look it's divide and conquer.

>> is a signed binary shift. The sign flag in the CPU gets shifted into the most significant bit.

0/2 returns 0

Are you fucking retarded?

This is the best answer. Super simple, understandable and works in every language, no syntax bullshit 10/10.

const getAverage = (numberOne,numberTwo) => {
let average = (numberOne + numberTwo) / 2
return (average)
};

console.log(getAverage(2,4))
// Output: 3

I laughed so hard at this

Jesus christ this board is a fucking joke

Only high schoolers use that overpriced meme

New old = new Old();

#lang scheme
(define (average x y)
(/ (+ x y) 2))
easy

This is why /sci/ bullies us.

I think this might be worse than the guy who thought j*j*j*j*j*j*j was the same as j*7 on agdg

wrong

>0/2 is undefined

- American education

>ask Sup Forums to do 1 + 1 in their language of choice
>186 replies 52 images omitted
>some dumb shit trying to divide by zero with 10 replies
>weeb showing off their custom arch install
>diploma.png
>still no right answer

every time

math.average(a,b)

What do you return if they are equal?

1;

>Sup Forums

(a / 2) + (b / 2) + (a & b & 1);

enjoy you're overflow

>These are the people that post smug anime reaction images when you say you use windows.

int average(int a, int b)
{
return ((abs(a - b) / 2) + min(a, b));
}


fucktards

SCREECHING at the state of the american education system

see way less operations, same result

void average(int a, b) {
a = a / 2;
while(b > 1) {
a++;
b = b - 2;
}
while(b < -1) {
a--;
b = b + 2;
}
return a;
}

and how do you define high and low fucktard ?

int a and int b, now int high and int low

int mean (int a , int b) { return (a + (unsigned)b) >> 1; }

What do I win?

Just add an if statement.

What's wrong with weebs?
How do you know they are weebs?