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
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}]
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.
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.
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.
Donate some dollars to get the actual working code.
Schelte
--
set Reply-To [string map {nospam schelte} $header(From)]
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.