mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2013-02-15, 21:34   #1
Andrew
 
Andrew's Avatar
 
Feb 2013

22×7 Posts
Default Code Help

Hi all, I'm having issues. I'm not "new" to C, but I've never practiced it or build great big things with it either. (I supposed I might as well be a noob, oh well.)

Down here I'm having trouble doing simple subtraction and that is annoying to me. It's on the powerM variable. It prints eff the value 21 for 83 - 21.

I'm just noobily speculating that maybe the print eff is on the fritz and that the real value plugs correctly through the rest of the function. In that case there is something else wrong with my program, in addition to the print eff, because my final product is wrong by one digit. (>.>)

'Case you're wondering, I'm building a manual square rooting program.


->> Some of those other variables were just things I made up to check what's wrong..."sub"ing in variables for direct digital numbers. I've made a bit of a mess.

thanks


Code:
int finish_root(int U,int P) {
    
    unsigned int place = (U%10)*2;
    place = (place + (U - U%10))*10;
    
    unsigned int powerM;
    unsigned int y=0;
    unsigned int S=0;
    unsigned int Remainder;
    //unsigned int save = D;
    //unsigned int sub = U*U;
    powerM = 83-81;
    printf("%u",powerM);


        for (y = 0; S==0; y++) {
            
            if (((place + y) * y) > ((powerM*100) + P)) {
                if (((place + (y-1)) * (y-1)) != ((powerM*100) + P)) {
                y = y - 1;
                }
                printf("%u\n",y);
                Remainder = (powerM*100 + P) - ((place+y)*y);
                
                S = 1;
            }
            
        }
    DENOM = place+y;
    printf("%u\n",DENOM);
    return(Remainder);
    
}
Andrew is offline   Reply With Quote
Old 2013-02-15, 21:48   #2
Mr. P-1
 
Mr. P-1's Avatar
 
Jun 2003

7×167 Posts
Default

Is it printing the value "21"? Or is it printing the value "2" and the next printf is printing the value "1"?

Try changing the first printf statement to

Code:
printf("%u\n",powerM);

Last fiddled with by Mr. P-1 on 2013-02-15 at 21:49
Mr. P-1 is offline   Reply With Quote
Old 2013-02-15, 23:33   #3
ewmayer
2ω=0
 
ewmayer's Avatar
 
Sep 2002
Rep├║blica de California

9,791 Posts
Default

Consider also gaining at least a small amount of proficiency in step-thru debugging ... if you used e.g. gdb to print the variable in question, you would quickly realize it's your linefeed-less printf which is the culprit here, as Herr Pay-Minus-Eins also notes above.
ewmayer is offline   Reply With Quote
Old 2013-02-16, 17:56   #4
Andrew
 
Andrew's Avatar
 
Feb 2013

22·7 Posts
Default

Thanks Both. ><, thanks mr.P-1.

Yeah, I'm not really getting what "debugging" is beyond the more human mistakes...things like "well it doesn't work" ...is it the machine or me, or a little of both?

I broke down and started using XCode for help a while ago after using text edit only, and discovered the "Analyze" function just now, so that should be a big plus for me, if it doesnt act as too much of a crutch. Maybe a learning tool.

The issue was elsewhere. I was going modular in order to find if a large number was square, using the old school arithmetic technique similar to long division, built up into computer terms. I ran into another problem realizing that the carry-over/remainder number used to do the operation eventually gets very very large itself.

I didn't realize that at first, and that was the real source of the whole problem. You wouldnt have seen it in that little snippet up there.

thanks again

Last fiddled with by Andrew on 2013-02-16 at 17:57
Andrew is offline   Reply With Quote
Old 2013-02-16, 18:03   #5
Andrew
 
Andrew's Avatar
 
Feb 2013

22·7 Posts
Default

One more question, without making a thread out of it:?

Suppose I wanted to store one number in a CSV or another file, as in, write into it once, and read out of it.

Say, 2^x or [y*(10^x)]. Roundabouts, how big could this CSV file get in the powers of 1000s, 1000000s, 1000000000? I mean in terms of hard drive space?

Size of a high res photograph? video?
Andrew is offline   Reply With Quote
Old 2013-02-16, 18:07   #6
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2·4,643 Posts
Default

Quote:
Originally Posted by Andrew View Post
Yeah, I'm not really getting what "debugging" is beyond the more human mistakes...things like "well it doesn't work" ...is it the machine or me, or a little of both?
Oh oh... Programmers who don't know how to use step-by-step debugging tools are doomed to failure once they go a little past "Hello World".

Quote:
Originally Posted by Andrew View Post
I broke down and started using XCode for help a while ago after using text edit only, and discovered the "Analyze" function just now, so that should be a big plus for me, if it doesnt act as too much of a crutch. Maybe a learning tool.
Maybe. But, IMO, IDEs just isolate the programmer from the "metal". Yes, they can expedite development, but often it's a bit like giving a chain-saw to someone not trained in their use....
chalsall is offline   Reply With Quote
Old 2013-02-16, 18:12   #7
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×4,643 Posts
Default

Quote:
Originally Posted by Andrew View Post
Say, 2^x or [y*(10^x)]. Roundabouts, how big could this CSV file get in the powers of 1000s, 1000000s, 1000000000?
Since you didn't give "x" nor "y", infinite.

For fun, try this under a Unix variant some time...

Code:
cat /dev/urandom > /tmp/fill_your_filesystem.csv
chalsall is offline   Reply With Quote
Old 2013-02-16, 18:14   #8
Andrew
 
Andrew's Avatar
 
Feb 2013

22·7 Posts
Default

Didn't even know there were step by step debugging tools. I'll google around, thanks.

No one ever taught me this stuff, I just started doing it, so forgive me. And I've never had a real use for real languages till I started following the primes searches.

I mean I used "E-views" if you've heard of that, and "R" to run through stat and econometric courses, but the end-goal there was 'major' material, not other-major material, and that doesn't really count as its still built on top of a UI.

XCode does make things nice and neat.
Andrew is offline   Reply With Quote
Old 2013-02-16, 18:18   #9
Andrew
 
Andrew's Avatar
 
Feb 2013

22×7 Posts
Default

Quote:
Originally Posted by chalsall View Post
Since you didn't give "x" nor "y", infinite.

For fun, try this under a Unix variant some time...

Code:
cat /dev/urandom > /tmp/fill_your_filesystem.csv


2^50,000,000 say.


And that looks dangerous
Andrew is offline   Reply With Quote
Old 2013-02-16, 19:47   #10
Andrew
 
Andrew's Avatar
 
Feb 2013

22·7 Posts
Default

Well I went ahead and ran a large one.

1,000,000,000 * 12 digits, plus a comma for every 3.

so ~ 12,004,000,000 characters.

1.6MB


So that's what my old photo class photo's were doing.


Sound right?
Andrew is offline   Reply With Quote
Old 2013-02-16, 19:55   #11
ewmayer
2ω=0
 
ewmayer's Avatar
 
Sep 2002
Rep├║blica de California

9,791 Posts
Default

Quote:
Originally Posted by chalsall View Post
IMO, IDEs just isolate the programmer from the "metal". Yes, they can expedite development, but often it's a bit like giving a chain-saw to someone not trained in their use....
When I bought my macbook "amateur" 4 years ago, I expected to do mainly 64-bit code-dev (SSE2 inline asm ported from 32-bit) on it, and also had high hopes that the Apple IDE might also prove useful. The very first C file using any inline ASM I attempted to build, I immediately hit the issue that the Apple IDE makes "special internal" use of the x86 *bx register in a way which is not transparent to the assembly coder, i.e. in order to get a working build I needed to plow through thousands of lines of inline asm and replace the *bx register with some other available general-purpose register. When it became clear that the resulting build was no faster than a simple gcc command-line build and that I was spending nearly as much time dicking around with the IDE as looking at code - to say nothing of the nightmarishly bug-riddled Xcode editor (which friends @Apple confirm is something of a running sad joke within the company, alas a sad joke with friends in high places, apparently), I stopped using it. Simple gcc/gdb serves me well. Meaning my next laptop will likely run Linux.

Last fiddled with by ewmayer on 2013-02-16 at 19:57
ewmayer is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rho code Happy5214 YAFU 3 2015-11-01 21:54
Please help me with my code daxmick Programming 15 2014-02-14 11:57
New Code JohnFullspeed Programming 20 2011-09-04 04:28
Code Primeinator Software 20 2009-06-11 22:22
A little help with VBS code IronBits No Prime Left Behind 6 2008-11-12 14:23

All times are UTC. The time now is 22:05.

Mon Oct 26 22:05:49 UTC 2020 up 46 days, 19:16, 0 users, load averages: 1.90, 1.82, 1.74

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.