What's wrong with this?
What's wrong with this?
Rounding errors because it deals with ints not floats
That and the obvious one it can only find the average of 2 numbers. Ideally the function would take any amount of inputs and provide the average
nothing really. If you are working with integers, this is perfectly fine.
just remember that C always rounds down.
even floating point numbers have rounding errors. It depends on what you're doing really
>What's wrong with this?
Well, would your mother approve?
overflow
You are using C
you are not welcome here
Actually this is D
you are not using java
No OOP
The main difference is that you need some public class Boilerplate{ and some public static in front of every int
int average(int a, int b) {
return (a + b) / 2;
}
int average(int a, int b, int c) {
return (a + b + c) / 3;
}
int average(int a, int b, int c, int d) {
return (a + b + c + d) / 4;
}
int average(int a, int b, int c, int d, int e) {
return (a + b + c + d + e) / 5;
}
Everything
Arrays exist for a reason, user
get out, weebs
>People thinking floating points are precise
float average(float a, float b) {
return (a + b) / 2;
}
average(0.1, 0.2)
Post your result in your favorite language, your mind will be blown
it might overflow for big a, b
?
you're right!
template
class Average;
template
ostream& operator
>0.150000
0.1 + 0.2
Is copying a simple 4-line piece of code too much for you?
works for me!
Lol
> const avg = (a, b) => (a+b) / 2; avg(0.1, 0.2);
< 0.15000000000000002
>javascript
he said programming language, m9
JS is a programming language, idiot
:D
...
It is not though.
?
Now this is Sup Forums-tier programming!
quoted from wikipedia
JavaScript (/ˈdʒɑːvəˌskrJpt/[5]) is a high-level, dynamic, untyped, and interpreted programming language.[6]
Why it isn't a programming language?
It's Turing complete and intended for programming, so it's definitely a programming language. It's even pretty decent as far as languages go. You probably think you're hardcore fizzbuzzing in C instead.
now do
average(0.1, 0.2) == 0.150000
try with 17 precision, you have trailing zeroes
>wikipedia
:^)
nice b8, m9
> 2016
> not programming in Scratch
average a b = (a + b) / 2 :: Float
main = print (average 0.1 0.2)
0.15
scratch confirmed for being better than js
is this shit suppose to be easy for kids lmao
looks more complicated than haskell
Code flowchart management is the essential step to learn programming
>haskell
>hard
pick one
>untyped
That's actually a mistake on Wikipedia's part - untyped would mean there's no type associated with any data at runtime or compile time at all, most importantly meaning no specified handling of cases where different types are used with the same operation. JS has explicit type coercion rules, and it throws errors if coercion is not possible, so it's definitely a type-aware language.
>int
You dun goofd
Integer overflow
λ let average a b = (a + b) / 2
λ average 0.1 0.2 :: Rational
3 % 20
λ average 0.1 0.2 :: CReal
0.15
λ average 0.1 0.2 :: Fixed E9
0.150000000
λ average 0.1 0.2 :: Double
0.15000000000000002
λ
Numbers sure are hard
Some more
λ average 0.1 0.2 :: Expr
(0.1 + 0.2) / 2
λ average 0.1 0.2 == (0.15 :: CReal)
True
λ average 0.1 0.2 == (0.15 :: Rational)
True
λ average 0.1 0.2 == (0.15 :: Fixed E9)
True
λ average 0.1 0.2 == (0.15 :: Double)
False
Powershell confirmed as god among men.
Overflow obviously. If you wanted to average 1 500 000 000 and 2 000 000 000 (which both fit in ints on most systems), you would get some negative number, even though the average fits into an integer as well. The function that does that looks like
int average ( const int a, const int b )
{
return a / 2 + b / 2 + ( a % 2 + b % 2 ) / 2;
}
But that's incorrect
Read IEEE754
Unless you use base 10 exponents, but that's for faggots
C is pass by value so that const only makes sense for pointers that you could dereference and then modify whatever they point at
I know, but I will continue to do it anyway.
Who knows what Powershell is using, maybe half precision floats.
>Who knows what Powershell is using
Rounding on display, it's pretty standard practice
google confirmed for best calculator
It's also gay af
why?
they are just triggered because they could learn a better, faster and easier language over their autistic one
> not returning "a/2 + b/2"
> he can't round
wew lads, try 0.1 + 0.2 - 0.3
>accepting only 2 arguments
>actually having an algorithm this simple as a function at all
My calculator is based af
On a 64 bit platform, I could also write this, right?
int average(int a, int b)
{
return (long)a / 2 + (long)b / 2;
}
Which one is faster, this or yours?
...
What you mean is
int average ( const int a, const int b )
{
return ( ( long ) a + ( long ) b ) / 2;
}
And yes, it's a lot faster. The version above is meant for situations where this option doesn't exist (when you want to average int64s for example). It also scales up for multiple integers like
int average ( const int *numbers, const int N )
{
int value = numbers[0] / N;
int error = numbers[0] % N;
for ( int i = 1; i < N; ++i )
{
value += numbers[i] / N;
error += numbers[i] % N;
}
return value + error / N;
}
Interesting choice to use comma as a decimal mark.
It makes people think float is okay to use without understanding it until it suddenly fucks you up
People will always be shit programmers, whether you choose to bombard them with information or not
int average ( int a, int b )
{
return (int)((a + b) / 2. + 0.5);
}
you can do it like that, although the op code will run faster.
Fucking kill yourself
Nobody posted anything related to anime here
>using "/ 2" on an int instead of ">> 1"
kys
>using ">> 1" instead of "/ 2"
I fucking hate your kind.
The kind that thinks they're being "clever" and "efficient". I hope you never get involved in any software project that I will ever look at the code of or use.
As a matter of fact, just eat shit and die.
x >> 1 is much much faster than x / 2 though.
Use a better language like java then you don't have to worry about it overflowing or whatever.
C and C++ will let the program compile even if it's completely fucked.
it's european
> int
Does the author even know about means?
>only takes two inputs
>(a + b)/2 instead of (a + b) / 2 or (a+b)/2
>returns int so averages will always be rounded down
>potential for overflow
>sublime text
they're the same speed
The compiler is smarter than you. Division or multiplication by a power of two will be optimized to a shift.
The shift is much faster, but an optimizing compiler will fix it.
Your mind will be blown if you are a first year CS fag.
Any modern, optimising compiler will rewrite that to a bitshift.
>The compiler is smarter than you. Division or multiplication by a power of two will be optimized to a shift.
Unfortunately, gcc does not do that. It does that for unsigned types, but not for signed ones. God knows why exactly.
>The shift is much faster
It really isn't
How do you write a Method that takes average of user selected quantity of numbers?
> Intentionally writing shit code as a trap to fuck with people that don't have a firm grasp of the topic
Am I back in college? Are you a TA?
It really is.
C:
int half_div ( int x )
{
return x / 2;
}
int half_shift ( int x )
{
return x >> 1;
}
Assembly:
half_div:
movl %edi, %eax
shrl $31, %eax
addl %edi, %eax
sarl %eax
ret
half_shift:
movl %edi, %eax
sarl %eax
ret
Can you bitshift divide/mutiply with numbers other than 2?
Those are two different functions though
All powers of 2 are possible. It's the equivalent of shifting the decimal point, i.e. to multiply 3821 by 10, you just need to add a zero 38210. That way you can multiply by 10, 100, 1000 etc.
Then please post a correct example, I'm curious about this.
>Then please post a correct example, I'm curious about this.
Correct example of what?
Your two functions both compile down to bit shifts, one is signed and the other is unsigned.
For unsigned ints they would produce the same code, but for signed bits GCC has to include the extra instruction to prevent it from breaking on negative numbers.
Which, given that we were writing an average function that explicitly permits negative integers in its signature, is a pretty big deal.
Can you bitshift multiply with 7 for example, instead of 2?
Is 7 a power of 2?
Nope, that's why i'm asking
Then there's your answer
It will overflow at the (a+b) if it goes over the range for int
Well, yes, I see that. The difference in this case is not really clear to me though.
half_div(-3) = -2
half_shift(-3) = -3
Not exactly what I'd call half of -3
Not really. Run-time "types" are properly called "tags".
Ah, I get it. It's off by one for negative numbers. Love how the compiler solves the problem. Didn't understand at first. Fucking smart.