Message from @Tero
Discord ID: 620834462728585277
`Although threads seem to be a small step from sequential computation, in fact, they represent a huge step. They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism.`
(this is probably where functional fans come in and tell everyone about how having any shared state is bad)
for some reason this program returns x as 25 million times its number
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open(){ char ch; cin>>ch; }
//for some reason, the square function returns 25 million
int square(int x)
{
x +=x;
cout << x;
}
int main()
{
int i = 0;
cout << "Enter a value.\n\n";
cin >> i;
cout << square(i);
}
seeing all these double >> around are you sure you're not bitshifting something?
i don't actually know C++ though
@Tero that's how you print in c++
And take input
In C++ the '>>' and '<<' indicator data flows with iostream
ok
why's it returning wrong results if not bitshifting then
Why use '+=' here @What Would Jack Conte Do? ?
I don't get the point of the cout in the function
Hmm, that could be something, nesting Couts in the return sequence
Why not ```return x;```?
He's already imported the cmath library, so why didn't he use the pow() function?
both good questions
so apparently the int-returning function square with no return statement results in undefined behaviour
Okay so definitely swap the function to
```int square(int x)
{
x *=x;
return x;
}```
TIL this is a thing https://www.onlinegdb.com/online_c++_compiler
Oh yeah that's super useful
Saves me the trouble of having to unpack Visual Studio's doodads
Saves me the trouble of SSHing
well its an excercise to replicate multiplication without multiplying, like what was done in ancient times
kinda stupid tbh
it squares a value entered
Ah, then you need some kind of if loop to iterate the value to it's square
Because right now
x += x; only works for squaring 2
😅
What I'm thinking of is something like the 2nd example on this page https://www.techiedelight.com/find-square-number-without-using-multiplication-division-operator/
```c
int findSquare(int num)
{
// convert number to positive if it is negative
num = abs(num);
// stores square of the number
int sq = num;
// repeatedly add num to the result
for (int i = 1; i < num; i++)
sq = sq + num;
return sq;
}```
the problem is that his `square(int x)` was not returning a value
the compiler should be giving a warning about this
i guess we figured that out already
yep
but why not square by just multiplying the numbers? its much more efficient than the iterative approaches
He added it's part of the coding prompt to do it without multiplying