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

How to express exponential function in TCL

464 views
Skip to first unread message

howie

unread,
Jul 26, 2007, 9:09:39 AM7/26/07
to
Dear friends:

Could anyone give me some tips about how to express exponential
funcition in tcl? For example, I want to express the function " y =
-1.32*x^4 + 1.24*x^2 + 1.21 " in tcl. So far all I know is such as
"6.032e-15" means 6.032*10^(-15). But I have no idea about how to
express x^4 in tcl.

Any help will be very appreciated.

Howie

Larry W. Virden

unread,
Jul 26, 2007, 9:27:05 AM7/26/07
to
On Jul 26, 9:09 am, howie <allke0...@gmail.com> wrote:
> Dear friends:
>
> Could anyone give me some tips about how to express exponential
> funcition in tcl? For example, I want to express the function " y =
> -1.32*x^4 + 1.24*x^2 + 1.21 " in tcl. So far all I know is such as
> "6.032e-15" means 6.032*10^(-15). But I have no idea about how to
> express x^4 in tcl.

According to expr(n) (the expr reference page, section n (for internal
commands of Tcl, Tk, etc.) one would say:

set y [expr {-1.32*(x**4) + 1.24*(x**2) + 1.21}]

Erik Leunissen

unread,
Jul 26, 2007, 9:28:56 AM7/26/07
to
howie wrote:
>
> Could anyone give me some tips about how to express exponential
> funcition in tcl? For example, I want to express the function " y =
> -1.32*x^4 + 1.24*x^2 + 1.21 " in tcl. So far all I know is such as
> "6.032e-15" means 6.032*10^(-15). But I have no idea about how to
> express x^4 in tcl.
>

Tcl 8.4 and older have an exponentiation function (only): pow(). Tcl
version 8.5 also understands ** as the exponentiation operator. See also
the manual page for the [expr] command.

E.g. 8.4:
% set x 2
2
% expr {pow($x,4)}
16.0


E.g. 8.5:
% set x 2
2
% expr {$x**4}
16

Can't explain the difference w.r.t. floating point in the results.

HTH,

Erik
--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Erik Leunissen

unread,
Jul 26, 2007, 9:38:32 AM7/26/07
to
As an aside:

Don't be tempted to try the ^ operator, which is used for exponentiation
in many mathematical packages. In Tcl, the ^ operator is used for a
bit-wise exlusive OR operation.

Cameron Laird

unread,
Jul 26, 2007, 11:48:51 AM7/26/07
to
In article <46a8a198$0$738$3a62...@textreader.nntp.hccnet.nl>,

Erik Leunissen <lo...@the.footer.invalid> wrote:
>howie wrote:
>>
>> Could anyone give me some tips about how to express exponential
>> funcition in tcl? For example, I want to express the function " y =
>> -1.32*x^4 + 1.24*x^2 + 1.21 " in tcl. So far all I know is such as
>> "6.032e-15" means 6.032*10^(-15). But I have no idea about how to
>> express x^4 in tcl.
>>
>
>Tcl 8.4 and older have an exponentiation function (only): pow(). Tcl
>version 8.5 also understands ** as the exponentiation operator. See also
>the manual page for the [expr] command.
>
>E.g. 8.4:
>% set x 2
>2
>% expr {pow($x,4)}
>16.0
>
>
>E.g. 8.5:
>% set x 2
>2
>% expr {$x**4}
>16
>
>Can't explain the difference w.r.t. floating point in the results.
.
.
.
Roughly speaking, pow() always returns a float (although that
has the potential to be implementation-dependent), while **
respects the types of its operands, in the sense of trying to
use integer arithmetic when it can.

I have a tiny bit of concern that we might be misinterpreting
howie; I suspect that at least one of mathematics and English
are not mother tongues for him. He *might* prefer the compu-
tation

set x_square [expr {x * x}]
set y [expr {-1.32 * x_square * x_square + 1.24 * x_square + 1.21}]

to any of the alternatives already mentioned, depending on the
details of his relationship to precision and performance. Or
maybe he even deserves an introduction to critcl at this point.

Schelte Bron

unread,
Jul 26, 2007, 3:26:25 PM7/26/07
to
Cameron Laird wrote:
> set x_square [expr {x * x}]
> set y [expr {-1.32 * x_square * x_square + 1.24 * x_square +
> 1.21}]
>

Donate some dollars to get the actual working code.


Schelte
--
set Reply-To [string map {nospam schelte} $header(From)]

Donal K. Fellows

unread,
Jul 27, 2007, 8:28:09 AM7/27/07
to
howie wrote:
> Could anyone give me some tips about how to express exponential
> funcition in tcl? For example, I want to express the function " y =
> -1.32*x^4 + 1.24*x^2 + 1.21 " in tcl. So far all I know is such as
> "6.032e-15" means 6.032*10^(-15). But I have no idea about how to
> express x^4 in tcl.

In 8.4 and before, you need the pow() function:

set x4 [expr {pow($x,4)}]

But in 8.5 you can *also* do this (using the new exponentiation operator):

set x4 [expr {$x**4}]

Note that the ^ operator is a bitwise XOR in Tcl (as in many other
programming languages).

Donal.

0 new messages