[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
What's wrong with this?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 145
Thread images: 19
File: average-two-ints-in-cee.png (22 KB, 763x167) Image search: [Google]
average-two-ints-in-cee.png
22 KB, 763x167
What's wrong with this?
>>
Rounding errors because it deals with ints not floats
>>
>>55038101
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
>>
>>55038097
nothing really. If you are working with integers, this is perfectly fine.

just remember that C always rounds down.

>>55038132
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?
>>
>>55038097
overflow
>>
You are using C
>>
>>55038097
>>55038101
>>55038132
>>55038138
>>55038150
>>55038159
>>55038172
you are not welcome here
>>>/a/
>>
>>55038172
Actually this is D
>>
you are not using java
>>
>>55038097
No OOP
>>
>>55038178
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
>>
>>55038097
Arrays exist for a reason, anon
>>
>>55038175
>>55038178
>>55038181
>>55038192
>>55038234
get out, weebs >>>/a/
>>
>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
>>
File: jf.png (12 KB, 927x352) Image search: [Google]
jf.png
12 KB, 927x352
>>55038251
?
>>
>>55038181
you're right!
template <class T>
class Average;

template <class T>
ostream& operator<<(ostream &, const Average<T> &);

template <class T>
class Average {
friend ostream& operator<< <T>(ostream &, const Average<T> &);
private:
T total;
int size;
public:
Average();
Average(T, int);
};

template <class T>
Average<T>::Average()
{
total = 0;
size = 0;
}

template<class T>
Average<T>::Average(T new_total, int new_size)
{
total = new_total;
size = new_size;
}

template <class T>
ostream& operator<<(ostream &out, const Average<T> &average)
{
if(average.size)
out << (average.total / average.size);
else
out << "cannot divide by zero";
return out;
}
>>
>>55038251
>
0.150000
>>
>>55038284
0.1 + 0.2
>>
>>55038284
Is copying a simple 4-line piece of code too much for you?
>>
File: js2.png (12 KB, 956x341) Image search: [Google]
js2.png
12 KB, 956x341
>>55038296
works for me!
>>
>>55038330
Lol
>>
>>55038330
> const avg = (a, b) => (a+b) / 2; avg(0.1, 0.2);
< 0.15000000000000002
>>
>>55038330
>javascript
he said programming language, m9
>>
>>55038353
JS is a programming language, idiot
>>
>>55038357
:D
>>
File: scratch.png (38 KB, 479x358) Image search: [Google]
scratch.png
38 KB, 479x358
>>55038353
>>
>>55038357
It is not though.
>>
File: average.png (4 KB, 419x239) Image search: [Google]
average.png
4 KB, 419x239
>>55038251
?
>>
Now this is /g/-tier programming!
>>
>>55038353
>>55038364
>>55038374
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?
>>
>>55038374
>>55038364
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.
>>
>>55038381
now do
average(0.1, 0.2) == 0.150000
>>
>>55038381
try with 17 precision, you have trailing zeroes
>>
File: 1395981350900.jpg (26 KB, 948x638) Image search: [Google]
1395981350900.jpg
26 KB, 948x638
>>55038398
>wikipedia
:^)
nice b8, m9
>>
File: scratch2.png (96 KB, 1186x729) Image search: [Google]
scratch2.png
96 KB, 1186x729
> 2016
> not programming in Scratch
>>
>>55038251
average a b = (a + b) / 2 :: Float
main = print (average 0.1 0.2)

0.15
>>
File: 1452029634190.jpg (53 KB, 464x464) Image search: [Google]
1452029634190.jpg
53 KB, 464x464
>>55038416
>>55038330
scratch confirmed for being better than js
>>
>>55038416
is this shit suppose to be easy for kids lmao
looks more complicated than haskell
>>
>>55038432
Code flowchart management is the essential step to learn programming
>>
>>55038432
>haskell
>hard
pick one
>>
>>55038398
>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.
>>
>>55038216

>int

You dun goofd
>>
>>55038097
Integer overflow
>>
>>55038251
λ 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
>>
>>55038510
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
>>
File: powershell.png (929 B, 170x27) Image search: [Google]
powershell.png
929 B, 170x27
Powershell confirmed as god among men.
>>
>>55038097
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;
}
>>
>>55038539
But that's incorrect

Read IEEE754

Unless you use base 10 exponents, but that's for faggots
>>
>>55038551
C is pass by value so that const only makes sense for pointers that you could dereference and then modify whatever they point at
>>
>>55038569
I know, but I will continue to do it anyway.
>>
>>55038556
Who knows what Powershell is using, maybe half precision floats.
>>
>>55038622
>Who knows what Powershell is using
Rounding on display, it's pretty standard practice
>>
google confirmed for best calculator
>>
>>55038634
It's also gay af
>>
>>55038669
why?
>>
>>55038398
they are just triggered because they could learn a better, faster and easier language over their autistic one
>>
>>55038097
> not returning "a/2 + b/2"
>>
>>55038251
> he can't round
wew lads, try 0.1 + 0.2 - 0.3
>>
>>55038906
>accepting only 2 arguments
>actually having an algorithm this simple as a function at all
>>
File: image.png (296 KB, 2048x1536) Image search: [Google]
image.png
296 KB, 2048x1536
My calculator is based af
>>
>>55038551
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?
>>
File: arrays.png (44 KB, 1492x965) Image search: [Google]
arrays.png
44 KB, 1492x965
>>
>>55038965
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;
}
>>
>>55038951
Interesting choice to use comma as a decimal mark.
>>
>>55038738
It makes people think float is okay to use without understanding it until it suddenly fucks you up
>>
>>55039272
People will always be shit programmers, whether you choose to bombard them with information or not
>>
>>55038097
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.
>>
>>55038248
Fucking kill yourself

Nobody posted anything related to anime here
>>
>>55038097
>using "/ 2" on an int instead of ">> 1"
kys
>>
>>55040448
>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.
>>
>>55040532
 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.
>>
>>55039088
it's european
>>
>>55038097
> int
Does the author even know about means?
>>
>>55038097

>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
>>
>>55040590
they're the same speed
>>
>>55040590
The compiler is smarter than you. Division or multiplication by a power of two will be optimized to a shift.

>>55040922
The shift is much faster, but an optimizing compiler will fix it.
>>
>>55038251
Your mind will be blown if you are a first year CS fag.
>>
>>55040922

Any modern, optimising compiler will rewrite that to a bitshift.
>>
>>55041020
>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.
>>
>>55041020
>The shift is much faster
It really isn't
>>
How do you write a Method that takes average of user selected quantity of numbers?
>>
File: N8IKxd6.jpg (163 KB, 583x599) Image search: [Google]
N8IKxd6.jpg
163 KB, 583x599
>>55038251
> 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?
>>
>>55041251
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
>>
>>55041295
Can you bitshift divide/mutiply with numbers other than 2?
>>
>>55041295
Those are two different functions though
>>
>>55041341
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.

>>55041361
Then please post a correct example, I'm curious about this.
>>
>>55041377
>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.
>>
>>55041377
Can you bitshift multiply with 7 for example, instead of 2?
>>
>>55041391
Is 7 a power of 2?
>>
>>55041395
Nope, that's why i'm asking
>>
>>55041407
Then there's your answer
>>
>>55038097
It will overflow at the (a+b) if it goes over the range for int
>>
>>55041386
Well, yes, I see that. The difference in this case is not really clear to me though.
>>
>>55041438
half_div(-3) = -2
half_shift(-3) = -3

Not exactly what I'd call half of -3
>>
>>55038463
Not really. Run-time "types" are properly called "tags".
>>
>>55041455
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.
>>
>>55041391
You could multiply by 8, and then subtract one of the other operands.
>>
>>55041524
And this is why you never try to outsmart the compiler
>>
>>55041529
Is it faster than just regular multiplication?
>>
>>55041529
That's slower than just multiplying by 7 on most platforms

>>55041538
Nope

https://stackoverflow.com/questions/6357038/is-multiplication-and-division-using-shift-operators-in-c-actually-faster/6357747#6357747

https://stackoverflow.com/questions/10681375/which-is-better-option-to-use-for-dividing-an-integer-number-by-2

Look, every programmer goes through this phase. We all want to think we're clever and that we're writing efficient code, when in reality all you're doing is producing unmaintainable garbage.

Give it a few years and you'll grow out of it.
>>
>>55041538
Only if you're doing something silly like working in a tarpit VM
>>
http://www.google.com/patents?id=eAIYAAAAEBAJ&dq=6007232
>>
>>55038097
IDE, font and color theme please?
>>
>>55038302
Seeing he is using Javascript, yes it is
>>
what about a+b, .. > INT_MAX

avg(2147483645, 42); 
>>
>>55041529
>>55041538
>>55041560
>That's slower than just multiplying by 7 on most platforms
Ironically, that's exactly what most compilers do, though.
>>
>>55041560
>>55041571

Alright, thanks!
>>
>>55040602
Java silently overflows just like C
>>
File: Screenshot_2016-06-12-08-44-23.png (130 KB, 1080x1920) Image search: [Google]
Screenshot_2016-06-12-08-44-23.png
130 KB, 1080x1920
>>55040891
It's not Sublime
>>
>>55041231
signed bit shifts are undefined behavior
>>
>>55038178
But that is legit Java code as well.
>>
>>55038640
>/3
you wot m80
>>
>>55038251
Nobody said floats are perfectly precise but it is more precise than using ints in this situation.
>>
>>55038596
Stop being so obstinate for no reason and learn from your mistakes mate.
>>
>>55039088
Interesting choice to use dot as a decimal mark.
>>
>>55039088
Back to burger and stay back to burger
>>
>>55042376
How is it a mistake? In the end it's more or less a stylistic choice.
>>
>>55038216
Fuck your ints.

Also, if you are going to take the average from a list of n numbers then you should just pass an array of floats to the function (and the size if you are working in C).
>>
>>55042437
You don't have to use const when you don't use pointers. This is like extern for functions, you don't have to specify it as all functions are extern by default.
>>
File: doesn't work la.webm (19 KB, 643x416) Image search: [Google]
doesn't work la.webm
19 KB, 643x416
>>55038251
>>
>>55038097
>not taking into account rounding errors and slow function call and math operations.

inline int average(int a, int b)
{
int c = a+b;
c += c>0? 1:-1;
return c>>1;
}
>>
>>55038372
This shit is less intuitive than actual programming.
>>
>>55043043
> AIDS positive
>>
File: 2016-06-12-18:50:03-scrot.png (15 KB, 581x490) Image search: [Google]
2016-06-12-18:50:03-scrot.png
15 KB, 581x490
>>55038251
There you go. It's in emacs lisp.
>>
https://bitbucket.org/snippets/Tetsumi/qKnke/average-c
>>
>>55038861
It's neither faster, better or easier than Rust.
>>
>>55038138

>int
>rounding error
>5/2==2

>float
>rounding error
>smaller than the width of a proton in meters
>>
braces go on the next line
>>
>>55038138
>If you are working with integers, this is perfectly fine.
average(1, MAX_INT)

FAIL
>>
>>55043149
Vim does it better
>>
>>55038097
Not written in a decent language.
(define (avg . n)
(/ (apply + n) (length n)))
>>
average(INT_MAX, INT_MAX)
>>
>>55043757
how many people will fall for these bait threads and reply what 100 posters before him already posted before the bait posters get bored?
>>
File: 2016-06-12-19:48:04-scrot.png (66 KB, 1280x800) Image search: [Google]
2016-06-12-19:48:04-scrot.png
66 KB, 1280x800
>>55043149
That's interesting.
>>
File: 2016-05-11 00.23.44.png (400 KB, 1883x995) Image search: [Google]
2016-05-11 00.23.44.png
400 KB, 1883x995
>>55043877
Wouldn't want to miscalculate your shekels
>>
>>55038138
>even floating point numbers have rounding errors. It depends on what you're doing really
That's very misleading, and you probably have no idea what you're talking about.
IEEE 754 floating-point always give a perfectly correct result to the nearest epsilon.
Whereas OP's code is just wrong.
>>
>>55043903
kek
>>
>>55038097
If you are not changing the values you should take it as constant reference.
>>
my god, what have i done to this thread
i thought you guys would at least knew what bit shifting was, people dont even know how it works, jesus christ guys...

>>55040532
if you cant make out that ">> 1" is the same as "/ 2" when reading code, perhaps you shouldnt be touching said code

>mfw there are people in this thread that dont know that checking if a uint number is a even number can be done with
isEven = ! (number & 1)

>mfw this will probably derail the thread even more
>ishygddt
>>
>>55038097
You didn't intent the closing bracket you FUCKING BARBARIAN.
>>
>>55043877
>using kike software
Thread replies: 145
Thread images: 19

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.