Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

dc

Name: Alexander Dubček 2015-03-15 6:33

Write a program in dc!

"monte carlo estimator for pi":
[? 2^ ? 2^ + 1>i]su
[lx 1+ sx]si
[lu x lm 1+ d sm ln>z]sz

5k
?sn
lzx
lx ln / 4* p
q


It needs a stream of floats:
/* frand.c */
#include <u.h>
#include <libc.h>

void
main(void)
{
srand(time(0));

for(;;)
print("%f\n", frand());
}


Driver:
#!/bin/rc
# runpi <number of samples>

{ echo $1; frand } | dc pi.dc


Example:
% runpi 10000
3.14840

Name: Anonymous 2015-04-02 17:42

I made a demo. Needs sleep-program to work.

**demo.dc
[ ]1:p[.]2:p[,]3:p[-]4:p[+]5:p[¤]6:p[#]7:p[%]8:p[$]9:p
[p0lwxsn
!sleep 0.1 && clear

1+d100>q]sq
[[lkx;pn1+d79>e]se0lexsn
[
]n1+d30>w]sw
[sxsystlx40-d*ly15-3*d*+v13*vlt*13/9%1+saltlylxla]sk

1lqx


$ dc <demo.dc

Name: Anonymous 2015-04-02 22:55

Oh, and basically register k contains the kernel-macro that selects the value for each "pixel", so it would be easy to modify the code to generate different kind of demo.

Stack before k is x y t, and after k it must be a x y t, where x is x-coordinate, y is y-coordinate, t is time, and a is the value of the pixel [1-9].

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List