Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 486 487 [488] 489 490 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 909092 times)

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7305 on: May 02, 2015, 05:24:13 pm »

Alright, thanks.

I guess like Orange Wizard said I'll go with a binary if I ever get this thing done.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7306 on: May 02, 2015, 05:33:13 pm »

managed to get it working. This whole time I didn't realise that '->' meant dereference, so i was getting the data pointed to rather than the pointer itself. This was fixed by simply putting the ampersand at the front of the assignment, although the complier complains about it.

I'd really like to look at your code, you shouldn't need to take the address with & if you have the original pointer. You're probably doing something in a roundabout manner, which is why it's giving you that compile warning.
« Last Edit: May 02, 2015, 05:45:45 pm by Reelya »
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7307 on: May 02, 2015, 10:02:40 pm »

Yeah, that does sound fishy.  My guess is that maybe you need to just use the "." operator instead of "->"?  If you can show us the code we can tell you for sure.
Logged
Through pain, I find wisdom.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7308 on: May 03, 2015, 04:40:04 am »

Maybe giga's getting element zero with "struct1->struct2[0]", then taking the address of that whole thing as "&struct1->struct2[0]"

But that's just equal to "struct1->struct2", since the brackets is dereferencing the pointer to the value, then the ampersand is taking the address of the value. An array name is just a pointer that points to the start of an allocated memory block. the square brackets operator works by adding the index to the pointer then dereferencing that pointer.
« Last Edit: May 03, 2015, 04:43:38 am by Reelya »
Logged

gigaraptor487

  • Bay Watcher
  • Escaped Lunatic now civilised
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7309 on: May 03, 2015, 07:14:03 pm »

ok I will show you the relevant code, ask if you want a little more since I was quite sparing since I wasn't sure exactly what you wanted to see


Spoiler (click to show/hide)
Logged
Hehe, you thought there would be an interesting sig here

I now run a program which brings old multiplayer games back to life : click

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7310 on: May 03, 2015, 08:27:24 pm »

That's interesting. I tried to engineer a situation like that where i needed the & and I couldn't get that to work.

Can you show me the declaration of objectActOn? If "forceArray" is defined as a "vectorData *" you shouldn't need to cast the return type all the time. You should only need to cast the pointer returned by malloc, since it's a void pointer. When you do the line:

holdForceArrayAddress = &objectActOn->forceArray;

forceArray should already be of type "vectorData *" so there should be no need to cast it again.

~~~

This code is as close as I could make it to your situation and it didn't need a &, and adding the & breaks it:

Spoiler (click to show/hide)

since struct1->data is already a pointer, taking the address of that with & makes it a "pointer-to-a-pointer", and causes an error.

What compiler are you using?
« Last Edit: May 03, 2015, 08:44:04 pm by Reelya »
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #7311 on: May 04, 2015, 08:39:48 am »

*sigh* Imagine you professionally produce web-based 'static' 'offline' HTML+Javascript software for enterprise clients (need to run in a web browser, but must be entirely self-contained and client-side), including banks. Imagine a lot of those banks still had IE6 as a minimum browser. Imagine all of those projects use a common core of code that needs to work on all the crazy browsers enterprise clients can come at you with.

Sure would love to be able to use some of this new ECMAScript 6 I keep hearing of (outside of what I can polyfill because using ECMAScript 6 Promise in IE6 feels better than doing all of the drugs at once at this point). Heck, ECMAScript 5 is only usable because of the sheer volume of polyfills out there.

Hmm, wonder if I can convince people at work to switch to Typescript...Already managed to get knockout.js, require.js and every polyfill I can find snuck into their day-to-day lives :)
Logged

gigaraptor487

  • Bay Watcher
  • Escaped Lunatic now civilised
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7312 on: May 04, 2015, 12:33:17 pm »

Definition of objectActOn(the structure it is anyway

Code: [Select]
typedef struct
{
float massTotal;
vectorData *forceArray; //this stores all of the forces acting on the object at any given time in newtons
char screenAppearance;
positionData position;
vectorData Acceleration, resultantForce;
int numberForces;
}objectData;

Declaration of objectActON

Code: [Select]
void initObjectData(objectData *objectActOn, float mass, float magnitudeAcceleration, float angleAcceleration, int xPos, int yPos)
{
objectActOn = malloc(sizeof(objectData));
objectActOn->massTotal = mass;
objectActOn->forceArray = (vectorData *) malloc(MAX_FORCES * sizeof(vectorData));
objectActOn->position.x = xPos;
objectActOn->position.y = yPos;
objectActOn->Acceleration.magnitudeArbitraryUnit = magnitudeAcceleration;
objectActOn->Acceleration.angleRadians = angleAcceleration;
objectActOn->numberForces = 0;
}

Logged
Hehe, you thought there would be an interesting sig here

I now run a program which brings old multiplayer games back to life : click

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #7313 on: May 04, 2015, 02:37:21 pm »

Code: [Select]
void initObjectData(objectData *objectActOn, float mass, float magnitudeAcceleration, float angleAcceleration, int xPos, int yPos)
{
objectActOn = malloc(sizeof(objectData));
objectActOn->massTotal = mass;
objectActOn->forceArray = (vectorData *) malloc(MAX_FORCES * sizeof(vectorData));
objectActOn->position.x = xPos;
objectActOn->position.y = yPos;
objectActOn->Acceleration.magnitudeArbitraryUnit = magnitudeAcceleration;
objectActOn->Acceleration.angleRadians = angleAcceleration;
objectActOn->numberForces = 0;
}


You probably want to create the pointer in the function and then return it. As it is, that just assigns the value of the pointer to the address being allocated and then does nothing with it, a memory leak and whatever pointer you pass in to the first parameter remains unmutated.

So
Code: [Select]
objectData* someObjectData = 0;
initObjectData(someObjectData, 0.0, 0.0, 0.0, 0, 0);
assert(someObjectData != 0); // Here will fail, as someObjectData will still be a nullptr.

In C, you'd either need to pass a pointer *to* the pointer as the first parameter or (more logically) return the created object data and have a matching "freeObjectData" function that takes the pointer and frees the memory.
« Last Edit: May 04, 2015, 02:40:35 pm by MorleyDev »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7314 on: May 04, 2015, 03:07:01 pm »

BTW @giga, swap around the meaning of declaration and definition and you're sweet.

A "declaration" just declares "this object exists".

A definition, defines the object: "this is what the object is/does".

A code statement can be both declaration and definition, if it's a class that's all inline for example. But if there are missing parts (e.g. you just name a function in a .h file), then the name part is the declaration, and the body of the function is the definition.
« Last Edit: May 04, 2015, 03:10:10 pm by Reelya »
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #7315 on: May 04, 2015, 03:48:34 pm »

Pointer syntax gets dumb sometimes, especially when using an iterator for a container holding pointers.
Code: [Select]
(*iterator)->member
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7316 on: May 04, 2015, 04:03:00 pm »

You have to consider that the language needs to work for all cases, not just your case. And each operator needs consistent rules about whether it's applied before or after any other operator.

Consider (3 + 2) * 2. Order of operators in maths is completely arbitrary. Is it stupid to need brackets for that? I can show you that -> having higher precedence than * actually makes logical sense. So it's less dumb than needing brackets to make an addition happen before a multiply.

*thing1->thing2

is ambiguous. It could be (1) "get thing2 from thing1, then dereference the result" or it could (2) be "dereference thing1, then get thing2 from that".

Are we dereferencing thing1 or thing1->thing2?

But, if you think about it, dereferencing the pointer then taking a pointer member with -> would not make any sense unless "thing1" is a pointer-pointer. So, the scenario you've outlined is the uncommon one, not the common one.

You often find that the "order of operators" is geared to the most likely scenarios. Just like maths, if you want to change operator order, you use brackets.
« Last Edit: May 04, 2015, 04:15:02 pm by Reelya »
Logged

gigaraptor487

  • Bay Watcher
  • Escaped Lunatic now civilised
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7317 on: May 04, 2015, 05:08:24 pm »

Code: [Select]
void initObjectData(objectData *objectActOn, float mass, float magnitudeAcceleration, float angleAcceleration, int xPos, int yPos)
{
objectActOn = malloc(sizeof(objectData));
objectActOn->massTotal = mass;
objectActOn->forceArray = (vectorData *) malloc(MAX_FORCES * sizeof(vectorData));
objectActOn->position.x = xPos;
objectActOn->position.y = yPos;
objectActOn->Acceleration.magnitudeArbitraryUnit = magnitudeAcceleration;
objectActOn->Acceleration.angleRadians = angleAcceleration;
objectActOn->numberForces = 0;
}


You probably want to create the pointer in the function and then return it. As it is, that just assigns the value of the pointer to the address being allocated and then does nothing with it, a memory leak and whatever pointer you pass in to the first parameter remains unmutated.

So
Code: [Select]
objectData* someObjectData = 0;
initObjectData(someObjectData, 0.0, 0.0, 0.0, 0, 0);
assert(someObjectData != 0); // Here will fail, as someObjectData will still be a nullptr.

In C, you'd either need to pass a pointer *to* the pointer as the first parameter or (more logically) return the created object data and have a matching "freeObjectData" function that takes the pointer and frees the memory.

Can not thank you enough, I have given up on maybe half a dozen projects because this causes errors. Wasn't aware of that.

Thanks a lot to everyone, sorry if I have wasted your time, pointers are tricky and I guess I didn't have a good grasp on them, will fix the code based on your reccomendations.
Logged
Hehe, you thought there would be an interesting sig here

I now run a program which brings old multiplayer games back to life : click

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #7318 on: May 04, 2015, 08:18:13 pm »

You have to consider that the language needs to work for all cases, not just your case. And each operator needs consistent rules about whether it's applied before or after any other operator.
Yeah, I understand why it has to be that way. It just looks convoluted to someone who doesn't.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

ed boy

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7319 on: May 05, 2015, 06:10:29 am »

So I have a problem with inverting matrices.
 
I'm trying to invert a 3x3 matrix via gaussian elimination, and use this to solve a system of linear equations. I have a python script to do so, that's called as part of a class. I've tried using arbitrary precision via the mpmath module, but the problem persists. The matrix that I want to invery is:
Code: [Select]
14.0,2746627.0,170.6155733706259951532047125510871410369873046875
2746627.0,538854462769.0,33472668.1214660549849622128704140777699649333953857421875
170.6155733706259951532047125510871410369873046875,33472668.1214660549849622128704140777699649333953857421875,2079.2624245838425583639446197230982400395888676326095937481

And my code is:
Code: [Select]
    def invertmat(self,locstring):
        """Here we will invert the matrix that we have found using RRE"""
        if usempf==1:
            self.invmat=[[mp.mpf(0) for i in range(veclen)] for j in range(veclen)]
            for i in range(veclen):
                self.invmat[i][i]=mp.mpf(1)
        else:
            self.invmat=[[0.0 for i in range(veclen)] for j in range(veclen)]
            for i in range(veclen):
                self.invmat[i][i]=1
        if declarestages==1: print "Inverting matrix..."
        #we will use RRE, so we will put the first column in RRE form, then the second, etc
        #this means that we will iterate over the column and then the row
        for j in range(veclen):
            for i in range(veclen):
                #first we have to check whether or not the term on the leading diagonal is zero
                #if it is, we zero out the row and column
                if self.mat[j][j]==0:
                    if usempf==1:
                        self.mat[i][j]=mp.mpf(0.0)
                        self.invmat[i][j]=mp.mpf(0.0)
                        self.vec[j]=mp.mpf(0.0)
                        self.badvec[j]=mp.mpf(0.0)
                    else:
                        self.mat[i][j]=float(0.0)
                        self.invmat[i][j]=float(0.0)
                        self.vec[j]=float(0.0)
                        self.badvec[j]=float(0.0)
                else:
                    if i==j:
                        #in this case, we are on the leading diagonal
                        #so we will make the leading diagonal zero
                        self.vec[i]=self.vec[i]/self.mat[i][i]
                        self.badvec[i]=self.badvec[i]/self.mat[i][i]
                        print "Dividing by "+str(self.mat[i][i])
                        for k in range(veclen):
                            if k!=j:
                                self.invmat[i][k]=self.invmat[i][k]/self.mat[i][i]
                                self.mat[i][k]=self.mat[i][k]/self.mat[i][i]
                        self.invmat[i][i]=self.invmat[i][i]/self.mat[i][i]
                        self.mat[i][i]=self.mat[i][i]/self.mat[i][i]
                    if i!=j:
                        #in this case, we are not on the leading diagonal
                        #so we want this element to be zero
                        #we will use the fact that the leading diagonal is nonzero
                        #we only care about rounding to zero on the leading diagonal, as that's the only thing we divide by
                        mult=self.mat[i][j]/self.mat[j][j]
                        self.vec[i]-=self.vec[j]*mult
                        self.badvec[i]-=self.badvec[j]*mult
                        for k in range(veclen):
                            if k!=j:
                                self.mat[i][k]-=self.mat[j][k]*mult
                                self.invmat[i][k]-=self.invmat[j][k]*mult
                        self.mat[i][j]-=self.mat[j][j]*mult
                        self.invmat[i][j]-=self.invmat[j][j]*mult
            if writeprogresstofile>0:
                f.write("vec"+str(j)+":\n")
                for i in self.vec:
                    f.write(str(i)+",")
                f.write("\nmat"+str(j)+":\n")
                for i in self.mat:
                    for k in i:
                        f.write(str(k)+",")
                    f.write("\n")
                f.write("invmat"+str(j)+":\n")
                for i in self.invmat:
                    for k in i:
                        f.write(str(k)+",")
                    f.write("\n")
the relevant variables are that veclen is 3, and f is a file that I write progress to.
 
When I invert this, however, I get:
 
Code: [Select]
271635475736322.3449137725807845490330514652153613387246069,123760956.39505916619461152377801645592270193200911064532913,-24281616049723.701885662857417315428214747782727699841050359
123760956.39505916619461152377801645592270193200911064830143,56.387244784681245388662458884703294053440414360396328263187,-11063047.066704691682385396698842928335615664887677997376554
-24281616049723.701885662857417315428214747782727699841098208,-11063047.06670469168238539669884292833561566488767799713266,2170544465289.5403113558612668273593392823016506598570495833

which is not correct, as multiplying this by the original gives:
Code: [Select]
-4,1.3113E-05,0.25
393216,4.25,-81920
-8,0.000190735,-1

What is the source of my problem?
Logged
Pages: 1 ... 486 487 [488] 489 490 ... 796