+ 2
Code results in negative numbers for no reason
So I made a code on an unsolvable maths question and somehow every time I go beyond a specific input, the value becomes negative for no reason.
27 Respuestas
+ 5
"There's supposed to be no limit on an int"
Where did that info come from?
There is a max and min limit for each primitive numeric types.
https://en.cppreference.com/w/cpp/types/numeric_limits
+ 3
while (int i == 1) //wrong statement
Right statement
while (i == 1) {
}
But it will never satisfy condition if i assigned with 0
So It should be
while (i >= 0)
+ 3
WaterMelon
But are the values correct? Maybe perfectly should not be used here.
You can argue all you want.
But limits exists and you should really try to understand and realize the dangers of overflow errors. They can introduce bugs to your code if you're not careful.
//this is the biggest value you can assign to int
int x = 2147483647;
cout<<x<<endl;
//try to make sense of this:
x = x*2+2;
cout<<x<<endl;
//this is not int
cout<<10000000000000<<endl;
//this is int. it will raise an overflow error but the program will compile and run anyway. This is the danger of overflow. You are now using incorrect values in your code.
x = 10000000000000;
//this is what the program will use for x
cout<<x<<endl;
+ 2
code?
+ 2
what language are you coding in? also, aside from the messed up syntax, the code is incomplete...
+ 2
maybe you are inputting very big numbers. when you overflow the maximum of int, you get negative values. Try this:
#include <iostream>
using namespace std;
int main() {
int x{2147483647};//max value for int
cout<<x<<endl; //ok
x++; //just add 1
cout<<x<<endl; //overflow
}
so if your x calculation exceeds 2147483647, you get a negative number.
so if the result of either
x = x /2
or
x = x*3+1
results in x > 2147483647, you will get a negative value.
+ 2
WaterMelon
It is doing exactly that. It overflowed the max at x*3.
#include <iostream>
using namespace std;
int main() {
//your input:
int x=888888889;
cout<<x<<endl;
//step by step:
cout<<(x*3)<<endl;
cout<<(x*3+1)<<endl;
//what your program did:
x = x*3+1;
cout<<x<<endl;
}
+ 2
Think of overflow as the odometer in your car. Drive it far enough and it starts again at 0. That doesn't mean you now have a new car...
+ 1
WaterMelon ,
Add a tag for the language.
Share a link to your code so we can see it in one click.
Get to the point. Nobody wants (at least I don't want) to read 15 messages in a row of you thinking out loud.
You're obviously getting garbage because the result doesn't fit in the width of your variable, but the fix depends on the language.
<curmudgeon mode off>
+ 1
V
0
It's basically a code to keep running an input to break till it prints "1", but with an even and odd multiplier. There's no reason it should go negative.
0
while (int n == 1)
If (int x % 2 == 0)
x = x /2
Else
x = x*3+1
0
And then it's supposed to break at 1
0
I only took positive numbers because 1 cannot be achieved in negative numbers, but somehow the script gets triggered at a random positive integer
0
C++. Also just add the regular identifiers. I gave up the logic in the code rest all is just cin cout include namespace and shit
0
Also we can use input inside a loop, it doesn't necessarily has to be outside, but I just put it in here to give you an idea of what the code logic looks like
0
There's supposed to be no limit on an int. I input 888888888 and it worked fine. The moment I input 888888889, it broke the code?
0
Where's the logic?
0
Let's say there's a limit. Why does 888888888 result in 444444444 but 888888889 result in a negative absurd number but my code still stops at 1?
0
-1628300628 is what comes when I input 888888889. Why is that? Why doesn't it multiply by 3 and add a 1 instead?