+ 6
<limits>, long long doubles and precision
Did you ever work with very little / very large numbers? How can you output a 123! or to be sure your approximation of pi to 57-th digit is not mispelled by the compiler? Feel free to post your (also not-C++) codes.
4 Réponses
+ 2
One solution is to split the long number over several smaller numbers, like for example here:
https://code.sololearn.com/cV3o3NWxVZ11/?ref=app
And about correct decimals: Do it like a kid at school, (int) digit by digit:
https://code.sololearn.com/cnCk4wRLTkS6/?ref=app
Problem with both approaches: Although the output may be right, they are not number types like you know them anymore; you'd have to write a complete math around them.
+ 3
HonFu all right, but in your code you're not generous with comments and samples!
+ 3
That's right... I'm just now starting to get into the habit of writing comments... for now I can offer you to ask questions. :-)
But samples? There are samples! Run the factorial code and see all these tapeworm numbers!
And rooty gives you square root of 2; for other numbers you just have to change the argument.
+ 3
I can try to comment a bit after the fact.
You remember how we multiply larger numbers in school, right?
123*5
First we take the 3*5. If it's larger than ten, we just take the last digit and keep the other digit by adding it to the next digits' result.
So 15 -> 5 and we keep 1 and add it to the 10 (2*5), keep the 1...
After we are through, we got the complete result.
In programming languages, you get the last digit by %10; then you can calculate 15/10 (// in Python) and get 1 (what you keep).
Now imagine we don't do this with %10, but with %1'000'000'000. You'd get bigger chunks, but actually it works quite the same (I needed a little help from a friend who can actually do maths for this).
Largest number you could get would be 999'999'999*999'999'999 which barely fits into an unsigned long long int.
Now if you do this 'digit' by 'digit', you can store each of them in a list (vector in this case). Then you basically just have to put them out and it looks like one large number.