Members Online
Total Online: 24 Web Spiders: 14
Guests Online: 24
Members Online: 0
Registered Members: 70170 Newest Member: bahmx
|
View Thread
| Author |
c++ large numbers |
p4plus2
Member
Posts: 167
Location:
Joined: 31.03.08 Rank: God |
|
The following program I am trying to create is supposed to find the answer to 2^1000. The problem is that I get a massively wrong answer that is about 100 numbers longer than it should be. My attempt at the problem was by far not the most straight forward method, but at the time seemed like it would be interesting to try. My point in posting is to ask if anybody can see a mistake that I made or whether I should simply scrap this and start over at square one.
My program:
See working code a few posts down.
See working code a few posts down.
#include <iostream>
int numb[500] = {0};
int sum = 0;
int main(int argc, char **argv)
{
numb[499] = 1; //set to 1 so we actually get an answer
for(int i = 0; i < 1000; i++){ //loop for number of squaring interations
for(int j = 499; j >= 0; j--){ //number of elements to scan through
if(numb[j] >= 5){ //check if we will have to carry
if(numb[j-1] != 9){ //check if there if double carry(not loop)
numb[j-1]++; //increment the place above the carry if no double carry
}else{ //start double carry code
numb[j-1] = 0; //set 9 to 0 for the carry
for(int k = j-2; k >=0; k--){ //loop through untill there is no more double carry
if(numb[k] != 9){ //check for more double carry(loop)
numb[k]++; //increment k if no more double carry(finishing the j-1)
break; //break out of the loop
}else{ //more double carry
numb[k] = 0; //set k to zero
} //end double carry check(loop)
} //end double carry loop
} //end double carry check(not loop)
numb[j] *= 2; //double j
numb[j] -= 10; //subtract 10 to remove all carry over
}else{ //start no carry
numb[j] *= 2; //simply double
} //end of all carry checking
} //end loop for doubling each digit
} //end all 1000 iterations.
for(int i = 0; i < 500; i++){
std::cout<<numb[i];
}
std::cout<<std::endl;
return 0;
}
Something odd about my output is that all numbers are even or zero as if the carry is handled wrong(which it probably is). But I commented it heavy so figuring out what I was trying to do shouldn't be hard.
My program's output:
24406404260220488888248284264884886088080862828400802642082240068684624686206464042080628066684800486044644288864262284286404820828822664288862248868640800828004206420404042464624208602806484204020426480460608622044222806240024228444862666028680406044888286242000688884082040088602446260622042080460266266060264026240820624608246046264268040668664282202422264600068620200400088486686046222280628640222266826068022460684202006204446
The correct output should be:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
EDIT: I just realized what went wrong. I will post the code here as soon as I come up with a work around.(go figure I realize the problem just after posting.)
"You can't be something your not,
Be yourself by yourself
Stay away from me" ~Walk, Pantera
"Playing an acoustic guitar is like having sex with your clothes on" ~Dave Mustaine
Edited by p4plus2 on 17-12-09 02:37 |
|
| Author |
RE: c++ large numbers |
stdio
Member
Posts: 375
Location: omnipresent
Joined: 06.04.08 Rank: God |
|
Holy shit, I know I dont comment near enough in my code, but wow... overdone much?
I'm sorry, I cant hear you over the sound of how awesome I am! |
|
| Author |
RE: c++ large numbers |
SET
Member
Posts: 380
Location: 0
Joined: 22.02.07 Rank: Hacker Level 1 |
|
This reminds me of a great artical i came across a month ago
Why Computers Suck at Math
http://www.techradar.com/news/computing/why-computers-suck-at-maths-644771
Its really a great read for both advanced and beginners
Edited by SET on 16-12-09 08:12 |
|
| Author |
RE: c++ large numbers |
COM
Banned

Posts: 800
Location:
Joined: 31.08.07 Rank: God |
|
Fix'd.
If you want to compare code, send me a PM as I kinda just rewrote it.
K'aem'nhi kh'rn, K'aem'nhi kh'r, K'aem'nhi kh'rmnu.
I'a Y'gs-Othoth! |
|
| Author |
RE: c++ large numbers |
stealth-
Member

Posts: 995
Location: Eh?
Joined: 10.04.09 Rank: God |
|
|
stdio wrote:
Holy shit, I know I dont comment near enough in my code, but wow... overdone much?
Some people comment alot when they first start out in a programming language. I did that with python, my first language. Even print statements had comments for me back then XD
The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealth-x.com |
|
| Author |
RE: c++ large numbers |
p4plus2
Member
Posts: 167
Location:
Joined: 31.03.08 Rank: God |
|
|
stdio wrote:
Holy shit, I know I dont comment near enough in my code, but wow... overdone much?
I actually wrote this with no comments, but I commented this before posting so my logic behind what I was doing was clear.
@com As for the rewritten code I found my problem, I was adding the carry at the wrong spot being dumb. My problem was that I added the carry before the multiplication when it should be after, I should have paid more attention to something like that. After I get it working I will PM you so that I can compare my code for slowdowns and such.
@moshbat I need the full number not just the notation version.
EDIT:
working code
#include <iostream>
int numb[500] = {0};
int carry, sum = 0;
int main(int argc, char **argv)
{
numb[499] = 1;
for(int i = 0; i < 1000; i++){
for(int j = 499; j >= 0; j--){
numb[j] <<= 1;
numb[j] += carry;
carry = 0;
if(numb[j] >= 10){
numb[j] -= 10;
carry = 1;
}
}
}
for(int i = 0; i < 500; i++){
std::cout<<numb[i];
}
std::cout <<std::endl;
return 0;
}
"You can't be something your not,
Be yourself by yourself
Stay away from me" ~Walk, Pantera
"Playing an acoustic guitar is like having sex with your clothes on" ~Dave Mustaine
Edited by p4plus2 on 17-12-09 02:44 |
|
|
|
|