### Author Topic: How to re-asign a variable value inside of a while ?  (Read 4683 times)

0 Members and 1 Guest are viewing this topic.

#### LE

• Guest ##### How to re-asign a variable value inside of a while ?
« on: November 26, 2005, 12:16:15 AM »
Hi,
Don't know if the subject is even the appropriate terminology.... but here is what I'm trying to do:

I have to recalculate the variable "x" inside of a while until it is equal to the same "x"

I am including the code in autolisp and the similar in c++, for reference.

Why is not giving me the same resultant as I get it on my function in autolisp?

Thanks.

Code: [Select]
`//(while (not (equal x//      (setq x//     (*//       x//       (1+ (/ (- (* x y) (sin x)) (- (* x (cos x)) (sin x))))))//      0.00001)))// here is what I want to verify and if my code is right ?while (!equal_real(x, x=(x * ((((x * y) - sin(x)) / ((x * cos(x)) - sin(x))) + 1)), 0.00001)) //(setq r   (/ larc (* 2 (abs x)))// fle (- r (sqrt (- (expt r 2) (expt (/ lc 2) 2))))) r = ((2 * fabs(x)) / larc); fle = (r - sqrt((pow(r, 2) - pow((lc / 2), 2))));`
« Last Edit: November 26, 2005, 10:25:34 AM by LE »

#### John Kaul (Se7en) ##### Re: How to re-asign a variable value inside of a while ?
« Reply #1 on: November 26, 2005, 10:00:57 AM »
I thik its because of scoping. (OBTW, it is very early in the morn for me, i havent even opened my eyes fully so beware of this post.) I think you have to use a pointer.

O&BTW, that code really hurt my head this early in the morn.
Common sense is not so common. ~Voltaire

--> Donate to TheSwamp.org <--

#### John Kaul (Se7en) ##### Re: How to re-asign a variable value inside of a while ?
« Reply #2 on: November 26, 2005, 10:32:01 AM »
w00t! Coffee is done and i had a half a cup now! (I can try to answer you in a more intelligent way now. --*Pttthhht, me ``intelligent'', I don't care who you are, that's funny!)

When you pass a var to a procedure you are passing by value. So when you attempt to alter that variable in you main procedure by means of another proced, you need to use either use a pointer or a reference in your support proced to alter that var.

My method for programing is use a more general support proced format. I use procedures to "test" and never "alter" data if i can. HOWEVER, because of the way lisp handles variable scoping, the altering of vars outside the main proced can be a nice and handy trick sometimes.
Common sense is not so common. ~Voltaire

--> Donate to TheSwamp.org <--

#### LE

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #3 on: November 26, 2005, 10:38:43 AM »
w00t! Coffee is done and i had a half a cup now! (I can try to answer you in a more intelligent way now. --*Pttthhht, me ``intelligent'', I don't care who you are, that's funny!)

When you pass a var to a procedure you are passing by value. So when you attempt to alter that variable in you main procedure by means of another proced, you need to use either use a pointer or a reference in your support proced to alter that var.

My method for programing is use a more general support proced format. I use procedures to "test" and never "alter" data if i can. HOWEVER, because of the way lisp handles variable scoping, the altering of vars outside the main proced can be a nice and handy trick sometimes. I have a look into the code, and posted back the results...

#### Chuck Gabriel

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #4 on: November 26, 2005, 11:31:45 AM »
Based on the results of the following code snippet, I think what is happening is that the assignment is taking place before the comparison is made, therefore the condition is always true.  It makes logical sense that this would be the case if you think about it.  The assignment (x = x * y) has to take place before anything can be compared to its result.

Code: [Select]
`#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]) {  float x = 1.0;  float y = 0.1;  while(x != (x = x * y)) {    y += 0.1;  }  cout << "x == " << x << endl;  system("PAUSE"); return EXIT_SUCCESS;}`

#### LE

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #5 on: November 26, 2005, 11:41:34 AM »
Based on the results of the following code snippet, I think what is happening is that the assignment is taking place before the comparison is made, therefore the condition is always true.  It makes logical sense that this would be the case if you think about it.  The assignment (x = x * y) has to take place before anything can be compared to its result.

Code: [Select]
`#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]) {  float x = 1.0;  float y = 0.1;  while(x != (x = x * y)) {    y += 0.1;  }  cout << "x == " << x << endl;  system("PAUSE"); return EXIT_SUCCESS;}`

Gracias Chuck,

Yes... I tried a similar approach last night.... but couldn't finished.... let me see if know I can make it work.

I forgot to mention, the code I posted is the Newton iteration method to find (x).... part of his book Principia Mathematica 1687

#### Chuck Gabriel

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #6 on: November 26, 2005, 11:55:13 AM »
If you restructure your code something like this, it should work.

Code: [Select]
`#include <cstdlib>#include <iostream>#include <cmath>using namespace std;int main(int argc, char *argv[]) {  float x = 1.0;  float y = 0.1;  for( ; ; ) {    float xPrime = x * y;    if(fabs(x - xPrime) < 0.00001)      break;    x = xPrime;    y += 0.1;  }  cout << "x == " << x << endl;  system("PAUSE"); return EXIT_SUCCESS;}`

#### LE

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #7 on: November 26, 2005, 12:42:28 PM »
Here is the function equal_real

Code: [Select]
` static bool equal_real(ads_real i1, ads_real i2, ads_real ftol) { double nom,denom,error;   nom   = fabs(i1-i2); denom = fabs(i1)+fabs(i2); if (denom != 0) { error = 2*nom/denom; return (error <= ftol); } return TRUE; }`
I need the iteration to find the value of (x)

#### LE

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #8 on: November 26, 2005, 08:15:29 PM »
Got the code working, here is what I end up using:

Code: [Select]
`ads_real xPrime;  for( ; ; ) {    xPrime = (x * ((((x * y) - sin(x)) / ((x * cos(x)) - sin(x))) + 1));    if(fabs(x - xPrime) < 0.00001)      break;    x=xPrime;  }//(setq r   (/ larc (* 2 (abs x)))// fle (- r (sqrt (- (expt r 2) (expt (/ lc 2) 2))))) r = (larc / (2 * fabs(x))); fle = r - sqrt((pow(r,2) - pow(lc/2,2)));// print value - test only//acdbRToS(fle, 2, 3, fmtval);//acutPrintf("FLE value is %s\n", fmtval);`
Also there was a mistake I was making on the following line:

Code: [Select]
`r = ((2 * fabs(x)) / larc);`
And should be like this:
Code: [Select]
`r = (larc / (2 * fabs(x)));`
A minor calculation's and was end up giving me the -1.#IND00000000000 values for the X and Y.... bad

Thank you Chuck, for the point to use for

Luis

#### Chuck Gabriel

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #9 on: November 26, 2005, 10:33:07 PM »
You're welcome.  I'm glad you figured it out.

#### LE

• Guest ##### Re: How to re-asign a variable value inside of a while ?
« Reply #10 on: November 26, 2005, 10:51:35 PM »
You're welcome.  I'm glad you figured it out. I still, have the parenthesis tic.... and keep using them on the numerical calculation's, in the lastest code I am avoid them...

i.e.

larc/r*180

instead of [even they can be used  no problema]
((larc / r) * 180)
« Last Edit: November 26, 2005, 10:59:46 PM by LE »