+ 1
How to handle larger numbers?
so i used the given code (30+12) and it came out as 42. I then changed the 30 to 300 and when i ran it, it told me there was no output. Does something in the code need to be changed in regards to larger numbers?
7 Antworten
+ 4
All numbers do infact have a maximum value so large numbers can be problems. However, 300 is very small, if you aren't using a byte or char there should be no issue based on the size of the number. Keep in mind Codeplayground sometimes says no output if you have slow internet connection.
+ 4
There's a number of techniques for dealing with large numbers, such as factoring, manipulation of strings, binomial properties. Any way it can be reduced, works. There's usually some big integer libraries in most languages to help deal with the problems.
https://mattmccutchen.net/bigint/
+ 3
Here's a code to add any two numbers together regardless of size. The code may not be optimised though, just made it from scratch and I can get lazy :P. Hopefully you get the idea though.
Note* Written in Java.
https://code.sololearn.com/cfIjr137aWxd/#java
PS: The largest number a long int can handle is roughly 2.1 Billion. So larger than that you need to use a long long (for c++). Larger than that, and you needa handle it your own way or via a library. My code can handle any size.
You might need to use another IDE to use the code. It's fairly slow for the large numbers.
If it doesn't work due to your connection, take a look at the code you'll get the idea.
What did I do?
I stored every digit individually, from a STRING.
5123
becomes
5,1,2,3
Then I added the digits together
Example question: 5123+91
=
5,1,2+9,3+1
=
5,1,11,4
Then I check if anything is greater than or equal to 10, if it is, move the extra number over one
=
5,1+1,1,4
=
5,2,1,4
Final ans: 5214
This is a way to handle large numbers anywho.
+ 1
@Restoring faith That makes sense, I am on a free, local network. That now brings the question, however, of how would larger numbers be handled and how big do they need to be to require that they are handled differently?
+ 1
@Restoring faith Thank you, this has helped me understand variables much better. One thing I love about code playground is the ability to interact with anyone for things such as this.
+ 1
At this point I'm just curious as to how larger numbers are handled.
0
You should post the code.