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

Q: Long "expr" Evaluation

14 views
Skip to first unread message

United Memories

unread,
Aug 21, 1996, 3:00:00 AM8/21/96
to

Hello,

I am using tcl7.4 under Linux 1.2.13.

I am writing some procedures to implement capacitance equations from the
following paper...
"Multilevel Metal Capacitance Models for CAD Design Synthesis Systems",
Chern, J-H, et al, IEEE Electron Device Letters, Vol 13, No 1,
January 1992.

My problem is that when I simply return the entire expression using
return (with expr), I get a different result than my hand calculations.
By breaking the expression into smaller pieces, each piece checks with
hand calculations. When I return *that* result, everything is alright.

My question is, why doesn't expr evaluate this expression correctly?
My work-around is to split the equation up but I have written other
procedures that are as complex but not checked as carefully. I wonder.

Included below is the procedure I am using with various experiments.
The procedure that I would like to use would have only the final return
statement (uncommented, of course). Also included is a sample tclsh
session. All stdout values are correct except result & return.
The "correct" answer is 1.4059e-16F/um.

Thanks for your time.

file: chern_proc.tcl
---BEGIN TCL PROCEDURE---
# c1g is line-to-ground capacitance per unit length over one ground plane.
# w is line width, s is line spacing, h is dielectric thickness between
# conductor and ground, t is line thickness, dw is delta width per side
# of the line.
proc c1g {w s h t dw {eox 3.45e-17}} {
set flat [expr ($w + 2 * $dw)/$h]
puts stdout "flat plate = $flat"

set sidewall [expr pow($t/($t + 2 * $h), 0.023)]
puts stdout "sidewall = $sidewall"

set coupling [expr pow($s/($s + 2 * $h), 1.16)]
puts stdout "coupling = $coupling"

set fringe [expr 3.8 * $sidewall * $coupling]
puts stdout "fringe = $fringe"

set normalized [expr $flat + $fringe]
puts stdout "flat plate + fringe = $normalized"

#set result [expr $eox * $normalized]
set result [expr $eox * ( ($w + 2 * $dw)/$h + \
3.28 * pow($t/($t + 2 * $h), 0.023) * pow($s/($s + 2 * $h), 1.16) )]
puts stdout "result = $result"
return $result
#return [expr $eox * ( ($w + 2 * $dw)/$h + \
#3.28 * pow($t/($t + 2 * $h), 0.023) * pow($s/($s + 2 * $h), 1.16) )]
}
---END TCL PROCEDURE---

---BEGIN TCL SESSION---
barra% tclch
% source chern_proc.tcl
% set value [c1g 2.5 5.0 1.342 0.5 0.0]
flat plate = 1.86289
sidewall = 0.958314
coupling = 0.607469
fringe = 2.21215
flat plate + fringe = 4.07504
result = 1.30145e-16
1.30145e-16
% exit
---END TCL SESSION---

John Tiede
United Memories, Inc. | Tel: (719) 594-4238 |A committee is an alley that
4815 List Drive, Suite 109| Fax: (719) 594-4939 |ideas are led down then
Colo Spgs, CO, USA 80919 | E-Mail: jo...@unimem.com|strangled. George Will

Tom Lane

unread,
Aug 22, 1996, 3:00:00 AM8/22/96
to

u...@earth.usa.net (United Memories) writes:
> My problem is that when I simply return the entire expression using
> return (with expr), I get a different result than my hand calculations.
> By breaking the expression into smaller pieces, each piece checks with
> hand calculations. When I return *that* result, everything is alright.

Offhand I'd bet that the long-expr code is more accurate than your hand
calculations. The calculations internal to a single expr call are done in
double precision, but expr's result has to be represented as a string,
and it will format the string with a limited number of decimal places
(determined by I-forget-which builtin variable ... but by default it's
only about six places).

Thus, breaking the calculation into multiple expr commands introduces
roundoff error into some of the intermediate answers; error which does
not occur if you do it in one humongous statement.

Tcl isn't really intended for serious numerical calculation, of course.
If you find that the extra roundoff error is a problem then you should
be using some other language.

regards, tom lane

Neil Walker

unread,
Aug 22, 1996, 3:00:00 AM8/22/96
to

Hi

the query was about precision in expressions:

p58 of John Ousterhout's book goes into details - but basically

* integers are stored as C ints
* real numbers as C doubles
* expr _by_default_ converts to a string with reals having 6 significant
digits
* you can set a global variable to up the number of significant digits:
% set tcl_precision x
where "x" > 6
* John Ousterhout recommends that:

"if you set tcl_precision to 17 on a machine that uses IEEE
floating point, you will guarantee that string conversions
do not lose information ..."

Cheers
Neil


Matthew Rice

unread,
Aug 22, 1996, 3:00:00 AM8/22/96
to

Tom Lane wrote:
> double precision, but expr's result has to be represented as a string,
> and it will format the string with a limited number of decimal places
> (determined by I-forget-which builtin variable ... but by default it's
> only about six places).

to change the precision in strings use:

set tcl_precision xxx


--
Matthew Rice e-mail: matthe...@ftlsol.com

United Memories

unread,
Aug 22, 1996, 3:00:00 AM8/22/96
to

Hello,

I had posted the question in this thread but it was my error.
The error occurred in the constant 3.8. In the long expression,
it was 3.28.

I am really embarrassed and sorry for the time anyone wasted on this.

Roger E. Critchlow Jr.

unread,
Aug 24, 1996, 3:00:00 AM8/24/96
to

u...@earth.usa.net (United Memories) writes:

I am using tcl7.4 under Linux 1.2.13.

I am writing some procedures to implement capacitance equations from the
following paper...
"Multilevel Metal Capacitance Models for CAD Design Synthesis Systems",
Chern, J-H, et al, IEEE Electron Device Letters, Vol 13, No 1,
January 1992.

My problem is that when I simply return the entire expression using


return (with expr), I get a different result than my hand calculations.
By breaking the expression into smaller pieces, each piece checks with
hand calculations. When I return *that* result, everything is alright.

Using tcl7.5p1 under Linux 1.2.13 I get exactly the same result
whether the computation is done by pieces or done by a single expr
call.

-- rec --

Larry W. Virden

unread,
Aug 26, 1996, 3:00:00 AM8/26/96
to

According to Tom Lane <t...@netcom.com>:
:Tcl isn't really intended for serious numerical calculation, of course.


:If you find that the extra roundoff error is a problem then you should
:be using some other language.

Two comments:


1. Perhaps someone out there has actually written a Tcl extension for
performing serious numerical calculation? If not, then if you know of a
C package, one could use the Tcl SWIG package to create a Tcl extension based
on the C package.

2. If the answers to the above two questions (is there a specialized Tcl
extension for serious numerical calculation and is there a C extension) are
no, then what language should a person use? Perhaps there is a way to use
SWIG or one of the other Tcl packages to interface the calculations in that
language to Tcl.

--
:s Larry W. Virden INET: lvi...@cas.org
:s <URL:http://www.teraform.com/%7Elvirden/> <*> O- "We are all Kosh."
:s Unless explicitly stated to the contrary, nothing in this posting should
:s be construed as representing my employer's opinions.

David M. Beazley

unread,
Aug 27, 1996, 3:00:00 AM8/27/96
to

Larry W. Virden wrote:
>
>
> Two comments:
>
> 1. Perhaps someone out there has actually written a Tcl extension for
> performing serious numerical calculation? If not, then if you know of a
> C package, one could use the Tcl SWIG package to create a Tcl extension based
> on the C package.
>
> 2. If the answers to the above two questions (is there a specialized Tcl
> extension for serious numerical calculation and is there a C extension) are
> no, then what language should a person use? Perhaps there is a way to use
> SWIG or one of the other Tcl packages to interface the calculations in that
> language to Tcl.
>

One of the examples in the SWIG distribution is a Tcl interface
to MATLAB so it's certainly possible to interface with numerical
packages. The only thing I would add, however, is if you're doing
lots of numerical calculations in Tcl, you probably want to put this
in your Tcl script :

set tcl_precision 17

This significantly improves accuracy and eliminates alot of the
round off error problems.

Cheers,

Dave Beazley

Don Libes

unread,
Aug 27, 1996, 3:00:00 AM8/27/96
to

In article <4vs2dv$j...@srv13s4.cas.org> lw...@cas.org (Larry W. Virden) writes:
1. Perhaps someone out there has actually written a Tcl extension for
performing serious numerical calculation? If not, then if you know of a
C package, one could use the Tcl SWIG package to create a Tcl extension based
on the C package.

2. If the answers to the above two questions (is there a specialized Tcl
extension for serious numerical calculation and is there a C extension) are
no, then what language should a person use? Perhaps there is a way to use
SWIG or one of the other Tcl packages to interface the calculations in that
language to Tcl.

Many people have told me that they use Expect to talk to bc (an
arbitrary-precision calculator) and dataplot (high-quality free
statistics package). These are excellent fast-and-cheap solution for
serious calculations.

Don


0 new messages