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

Pages: 1-

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: Alexander Dubček 2015-03-15 6:38

An example from the Plan 9 manual:

Print the first ten values of n!
[la1+dsa*pla10>y]sy
0sa1
lyx


http://man.cat-v.org/plan_9/1/dc

Name: Alexander Dubček 2015-03-15 16:59

>>3

There's an idea.

Here's RANDU
[65539 * 2 31^ %]su

Lol:
1
luxp
65539
luxp
393225
luxp
1769499
luxp
7077969
luxp
26542323
luxp
95552217
luxp
334432395
luxp
1146624417
luxp
1722371299
luxp
14608041

Name: Alexander Dubček 2015-03-17 3:06

>>3

Now I got it working. Thanks for the idea!

It took a while to figure out because setting the scale factor k fucked up the integer operations for RANDU.

Also, I'm quite pleased with myself for coming up with the pseudocomment string at the top.

[
Registers:
u - routine : execute i if sum of squares less than 1
i - routine : increment register x
z - routine : iterator - execute u while n > m++
r - routine : RANDU PRNG
m - variable: number of samples
x - variable: number of samples inside circle
s - variable: seed for r
k - variable: scale for division
n - variable: number of iterations (user input)
]c
[lrx 2^ lrx 2^ + 1>i]su
[lx 1+ sx]si
[lu x lm 1+ d sm ln>z]sz
[0k ls 65539 * 2 31^ % d ss lkk 2 31 ^ /]sr
? sn
5dksk
1 ss
lzx
lx lm / 4*
p

Name: Alexander Dubček 2015-03-17 3:08

>>7

$ echo 10000 | dc pi.dc
3.13640


Currently, the seed is hardcoded to 1. It would be a one-character change to read it from the input as well.

Name: Alexander Dubček 2015-03-17 3:32

[
Registers:
f: routine - fibonacci step function
z: routine - driver
a: variable - first number
b: variable - second number
i: variable - iterator
n: variable - number of steps (user input)
]c

[sa sb la lb + lb]sf
[p lfx li 1+ d si ln>z]sz
1 1
?sn
lzx


$ echo 10 | dc fib.dc
1
1
2
3
5
8
13
21
34
55

Name: Alexander Dubček 2015-03-17 4:19

[
f: routine - print fizz
b: routine - print buzz
u: routine - print fizzbuzz
c: routine - increment i
m: routine - call c to set i for 3, 5, 15
p: routine - print fizz or buzz
o: routine - just print the variable
z: routine - iterator; call m, then print for i=0,1,2 divisors
x: variable - garbage disposal
i: variable - 3, 5, 15
n: variable - number of iterations
]c

[[Fizz]psx]sf
[[Buzz]psx]sb
[[FizzBuzz]psx]su
[li 1 + si]sc
[0si d 3 % 0=c d 5 % 0=c]sm
[d 3 % 0=f d 5 % 0=b]sp
[p]so
[lmx li 2=u li 1=p li 0=o 1+ d ln >z]sz

?1+sn
1
lzx


$ echo 16 | dc fizzbuzz.dc
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16

Name: Alexander Dubček 2015-03-17 4:20

$ cat dubs.dc
[[Check 'em!]p]sd
? 100 % 11 % 0 =d
$ echo 11 | dc dubs.dc
Check 'em!

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].

Name: Alexander Dubček 2015-04-03 2:33

>>12,13

Wow, very nice!

########------- -------########
########----- ################################# -----########
######----- ##########-------------------------########## -----####
###---- #######------- -------####### ----#
---- ######------ ################# ------###### --
-- ######---- ########---------------######## ----######
#####---- #####-------- --------##### ----#####
#####---- #####----- ############# -----##### ----#####
####---- ####---- ####-------------------#### ----#### ----###
####--- ###---- ###------ ------### ----### ---##
##---- ###---- ##---- ####-----------#### ----## ----### ----
#--- ###--- ##---- ###------ ------### ----## ---### --
#--- ###--- ##---- ##---- ###---------### ----## ----## ---### --
--- ###--- ##--- ##--- ###-- ####### --### ---## ---## ---### -
--- ###--- ##--- ##--- ##-- ###- -### --## ---## ---## ---### -
--- ###--- ##--- ##--- ##-- ##- # # -## --## ---## ---## ---### -
--- ###--- ##--- ##--- ##-- ###- -### --## ---## ---## ---### -
--- ###--- ##--- ##--- ###-- ####### --### ---## ---## ---### -
#--- ###--- ##---- ##---- ###---------### ----## ----## ---### --
#--- ###--- ##---- ###------ ------### ----## ---### --
##---- ###---- ##---- ####-----------#### ----## ----### ----
####--- ###---- ###------ ------### ----### ---##
####---- ####---- ####-------------------#### ----#### ----###
#####---- #####----- ############# -----##### ----#####
#####---- #####-------- --------##### ----#####
-- ######---- ########---------------######## ----######
---- ######------ ################# ------###### --
###---- #######------- -------####### ----#
######----- ##########-------------------------########## -----####
########----- ################################# -----########

Name: Anonymous 2015-04-03 11:08

Any of you who like doing "programs" with dc or any other stack based language should try IBNIZ http://pelulamu.net/ibniz/

Real time graphics&audio programming with language similar to dc (but with commands better suited for the task IMO). Very fun to fool around. Download&compile for your own system, the Javascript implementation is buggy (at least for me).

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