| Author |
How to express exponential function in TCL
|
|
|
| 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 2007-07-26, 7:11 pm |
| 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 2007-07-26, 7:11 pm |
| 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 2007-07-26, 7:11 pm |
| 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.
Erik
--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.
| |
| Cameron Laird 2007-07-26, 7:11 pm |
| In article <46a8a198$0$738$3a628fcd@textreader.nntp.hccnet.nl>,
Erik Leunissen <look@the.footer.invalid> wrote:
>howie wrote:
>
>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.
| |
| Schelte Bron 2007-07-26, 10:08 pm |
| 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 2007-07-27, 8:09 am |
| 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.
|
|
|
|