I'm too stupid to program

Aug 10, 2001
10,420
2
0
The following program gives a rational approximation for an irrational number (pi,e,sqrt(2), etc.). How do I tell maple only to print out the current p/q if it is closer to r than all the previous p/q's? Currently I'm just printing out thousands of numbers. :eek:

ratapprox:=proc(r,N)
local rr,p,q,z;

rr:=evalf(r);
p := 1; q := 1;

while N > q do
z:=(p/q);

if evalf(z)<rr then
p:=p+1;
print(z);

else
q:=q+1;
print(z);

end if;

end do;

end proc;
 

habib89

Diamond Member
Jan 17, 2001
3,599
0
0
yah, you musta forgot a ; somewhere.. or maybe too many :

that's why i did not want to be a programmer.. one semi-colon out of place, and you're screwed and looking for your problem for hours on end
 
Aug 10, 2001
10,420
2
0
Originally posted by: habib89
yah, you musta forgot a ; somewhere.. or maybe too many :

that's why i did not want to be a programmer.. one semi-colon out of place, and you're screwed and looking for your problem for hours on end

basically I need to add something that says that if the current error=abs(p/q-r) is less than ALL the previous errors, print out the current p/q

I only know how to tell Maple to print out the current p/q if it's error is smaller than the previous p/q's error
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Use an if statement to perform a test prior to outputing.
After outputing, store the variable that triggered the condition and test against it.

Also try the S/W forums
 
Aug 10, 2001
10,420
2
0
Originally posted by: EagleKeeper
Use an if statement to perform a test prior to outputing.
After outputing, store the variable that triggered the condition and test against it.

Also try the S/W forums

Thanks for the help. It's now only priting about 10 numbers. Unfortunately now I think the numbers are just slighly off.
 

mitmot

Golden Member
Aug 11, 2005
1,852
1
0
Originally posted by: Random Variable
Originally posted by: DaFinn
You are also too stupid to post IN THE RIGHT FORUM!!!


:p

In the "right forum" I probably wouldn't get any responses.

that's because they spend all their time looking for the ; they are missing ;)
 

artikk

Diamond Member
Dec 24, 2004
4,172
1
71
Originally posted by: Random Variable
Originally posted by: DaFinn
You are also too stupid to post IN THE RIGHT FORUM!!!


:p

In the "right forum" I probably wouldn't get any responses.

The problem is that people use that excuse and that is partly the reason that those forums aren't visited as much because people post these threads here and not there.
 
Aug 10, 2001
10,420
2
0
New code:

ratapprox:=proc(r,N)
local rr,p,q,err,z;

rr:=evalf(r);
p := 1; q := 1;
err:=evalf(r+1);

while N > q do
z:=(p/q);

if evalf(z)<rr then
p:=p+1;

if evalf(abs(p/q-r))<err then
print(p/q);
err:=evalf(abs(p/q-r));

end if;

else
q:=q+1;
if evalf(abs(p/q-r))<err then
print(p/q);
err:=evalf(abs(p/q-r));

end if;

end if;

end do;

end proc;
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: habib89
yah, you musta forgot a ; somewhere.. or maybe too many :

that's why i did not want to be a programmer.. one semi-colon out of place, and you're screwed and looking for your problem for hours on end

Programming tools these days take care of syntax problems for you.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Random Variable
It works now. :)

Was it the extra if test and then setting/updating the test variable?

 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Random Variable
Originally posted by: DaFinn
You are also too stupid to post IN THE RIGHT FORUM!!!


:p

In the "right forum" I probably wouldn't get any responses.

A response will not come as quickly, but they will usually come.

/HINT and if the games were removed, then better quality would also exist

And a lot less flaming and sarcasm will exist for posting in the wrong forum.

There are quality S/W people that do frequent the S/W forum.