Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

manipulating user input in dc

17 views
Skip to first unread message

Mike Rhodes

unread,
Oct 29, 2012, 12:47:51 PM10/29/12
to
Hi all,

I'm trying to understand how to manipulate user input in dc. I have a
script for calculating monthly payments on a mortgage:

---
#!/usr/bin/dc

9k # set precision value

[Enter interest rate: ]P?Sili12/si # get i
[Enter loan term in years: ]P?Snln12*sn # get n
[Enter present value: ]P?Sp # get p

li1+ln^li*li1+ln^1-/lp* # compute payment
p # print result
---

Is there no shorter way to read input, manipulate it, and store it in a
register than "?Sili12/si"? I tried "?12/Si" but that didn't work.

Any help would be greatly appreciated.

Regards,
Mike

Ed Morton

unread,
Oct 29, 2012, 2:59:00 PM10/29/12
to
Wouldn't it make more sense to do it in awk or similar? I don't read "dc" so I
don't know what your algorithm says but here's an awk script to calculate
something - apply your own algorithm:

$ cat tst.sh
#!/usr/bin/awk -f
BEGIN {
printf "[Enter interest rate: ] "
getline rate

printf "[Enter loan term in years: ] "
getline term

printf "[Enter present value: ] "
getline principal

interest = principal * (rate/100) * term

payment = ( (principal + interest) / term ) / 12

print payment
}

$ ./tst.sh
[Enter interest rate: ] 4.5
[Enter loan term in years: ] 30
[Enter present value: ] 200000
1305.56

Ed.

Alan Curry

unread,
Oct 29, 2012, 4:01:48 PM10/29/12
to
In article <508eb339$0$7645$c3e8da3$f626...@news.astraweb.com>,
Mike Rhodes <M8R-1...@mailinator.com> wrote:
>
>Is there no shorter way to read input, manipulate it, and store it in a
>register than "?Sili12/si"? I tried "?12/Si" but that didn't work.

Why do you use the S command if you don't want to do an L later? The oldest
versions of dc didn't have the S and L commands, only s and l, so maybe
that's the problem.

?12/Si should work with GNU dc at least, but ?12/si would work on all of
them.

--
Alan Curry

Mike Rhodes

unread,
Oct 29, 2012, 4:05:14 PM10/29/12
to
On 10/29/12 2:59 PM, Ed Morton wrote:
> On 10/29/2012 11:47 AM, Mike Rhodes wrote:
>> Hi all,
>>
>> I'm trying to understand how to manipulate user input in dc. I have a
>> script for calculating monthly payments on a mortgage:
>>
>> [...]
>>
>> Is there no shorter way to read input, manipulate it, and store it in a
>> register than "?Sili12/si"? I tried "?12/Si" but that didn't work.
>
> Wouldn't it make more sense to do it in awk or similar?
>
> Ed.

Yes, it would. Just an exercise on my part, in an effort to understand
dc a little better. AWK is definitely the more straightforward solution:

#!/usr/bin/awk -f

BEGIN {
printf "Enter interest rate: "
getline i
printf "Enter loan term in years: "
getline n
printf "Enter present value: "
getline p

i = i / 100 / 12
n = n * 12

pmt = p * (i * (1 + i) ^ n) / ((1 + i) ^ n - 1)
print
print "Monthly payment: $" pmt
}

--
Mike

Ben Bacarisse

unread,
Oct 29, 2012, 4:13:04 PM10/29/12
to
Mike Rhodes <M8R-1...@mailinator.com> writes:

> I'm trying to understand how to manipulate user input in dc. I have a
> script for calculating monthly payments on a mortgage:
>
> ---
> #!/usr/bin/dc
>
> 9k # set precision value
>
> [Enter interest rate: ]P?Sili12/si # get i
> [Enter loan term in years: ]P?Snln12*sn # get n
> [Enter present value: ]P?Sp # get p
>
> li1+ln^li*li1+ln^1-/lp* # compute payment
> p # print result
> ---
>
> Is there no shorter way to read input, manipulate it, and store it in a
> register than "?Sili12/si"? I tried "?12/Si" but that didn't work.

I tried it and it did, so maybe it was something else? If you post the
code that didn't work, maybe someone can say why.

BTW, I tried ?12/si as well as ?12/Si -- I'd use s rather S unless you
need the stacking behaviour.

--
Ben.

Mike Rhodes

unread,
Oct 29, 2012, 4:21:07 PM10/29/12
to
I get a "stack empty" error unless I use S. I can't make it work without
the uppercase S for some reason. I'm using OpenBSD dc, which supports S
and L.

Mike

Ben Bacarisse

unread,
Oct 29, 2012, 4:33:58 PM10/29/12
to
In case it helps, here:

$ dc -V
dc (GNU bc 1.06.95) 1.3.95
...

's' works in place of 'S' as does your shortened version:
$ printf "1\n2\n100\n" | dc dco
Enter interest rate: Enter loan term in years: Enter present value: 9.763223400
$ printf "1\n2\n100\n" | dc dcs
Enter interest rate: Enter loan term in years: Enter present value: 9.763223400
$ printf "1\n2\n100\n" | dc dc
Enter interest rate: Enter loan term in years: Enter present value: 9.763223400

'dco' is your version.

$ diff dco dcs
3,5c3,5
< [Enter interest rate: ]P?Sili12/si # get i
< [Enter loan term in years: ]P?Snln12*sn # get n
< [Enter present value: ]P?Sp # get p
---
> [Enter interest rate: ]P?sili12/si # get i
> [Enter loan term in years: ]P?snln12*sn # get n
> [Enter present value: ]P?sp # get p
$ diff dco dc
3,4c3,4
< [Enter interest rate: ]P?Sili12/si # get i
< [Enter loan term in years: ]P?Snln12*sn # get n
---
> [Enter interest rate: ]P?12/si # get i
> [Enter loan term in years: ]P?12*sn # get n

--
Ben.
0 new messages