[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Groff] pic: nasty little trap in function!
From: |
Ted Harding |
Subject: |
[Groff] pic: nasty little trap in function! |
Date: |
Mon, 02 Mar 2015 21:22:02 -0000 (GMT) |
Greetings All!
I was recently wanting to draw someline segments along radii
of a circle, at series of angles. Since I wanted to "think
in degrees", while the sin() and cos() functions work in
radians, I defined a function in pic according to:
.PS
pi = 4*atan2(1,1)
define d2r {$1*pi/180}
.PE
Then I wanted to plot the segments as follows:
.PS
r0 = 2 ; dr = 0.5
for i=1 to 5 do {
t = d2r(15.0+0.5*i)
line from (r0*cos(t),r0*sin(t)) to ((r0+dr)*cos(t),(r0+dr)*sin(t))
}
.PE
These came out in the wrong place! And I found out why. Look at:
.PS
pi = 4*atan2(1,1)
define d2r {$1*pi/180}
## Left-hand columne:
for i=1 to 5 do {
t = d2r(15.0+0.5*i)
sprintf("%g",t) at (0,1-i/5)
}
## Right-hand column:
for i=1 to 5 do {
t = d2r((15.0+0.5*i))
sprintf("%g",t) at (2,1-i/5)
}
.PE
which prints the values of d2r(15.0+0.5*i) in the left-hand column,
and the values of d2r((15.0+0.5*i)) in the right-hand column.
These are:
15.0087 0.270526
15.0175 0.279253
15.0262 0.287979
15.0349 0.296706
15.0436 0.305433
The right-hand column is correct (verified by independent software),
while the left-hand column has the values of 15+d2r(0.5*i) (again
similarly verified).
Thus when given the definition "define d2r {$1*pi/180}", pic evaluates
d2r(15.0+0.5*i) as 15 + d2r(0.5*i); in order to get it right I had
to bracket the expression "15.0+0.5*i" given as argument to d2r()
to make "(15.0+0.5*i)" be given as argument, which then came out right.
I can't think by what logic pic produces "15 + d2r(0.5*i)", but I'm
passing this on in case it is of use or interest to any of you!
Best wishes to all,
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Date: 02-Mar-2015 Time: 21:21:57
This message was sent by XFMail
-------------------------------------------------
- [Groff] pic: nasty little trap in function!,
Ted Harding <=