且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

TCL脚本中的浮点异常

更新时间:1970-01-01 08:00:00

除非NS2"这个东西重新定义了 Tcl 的 global 命令,脚本应该是这样的:

Unless that "NS2" thing redefines the Tcl's global command, the script should supposedly read something like this:

#Create a simulator object

set ns [new Simulator]

set rate02 2.0Mb 
set rate12 2.0Mb
set rate23 1.1Mb
set rate32 1.2Mb

# ...

#Create links between the nodes
$ns duplex-link $n0 $n2 $rate02 10ms RED
$ns duplex-link $n1 $n2 $rate12 10ms DropTail
$ns duplex-link $n2 $n3 $rate23 20ms DropTail
$ns duplex-link $n3 $n2 $rate32 10ms DropTail

# ...

换句话说,你的代码有两个问题:

In other words, there are two problems with your code:

  • Tcl 的global 命令的语义定义为global varname ?varname ...?,也就是说,它只是声明names 传递给它作为引用全局变量.因此调用 global set rate02 2.0Mb 只声明名称set"、rate02"和2.0Mb"来表示全局变量.很可能不是您想要的.
  • duplex-link 子命令可能需要一个值作为它的rate"参数,而你要传递给它一个变量的名称.em> 看起来,一些 NS2 代码然后试图将这样的字符串解释为浮点值,但由于明显的原因而失败.因此,当您想将该变量的值传递给命令时,您必须取消引用该变量以获取其值.这是使用 set varname 命令或使用 $ 语法的速记符号完成的:$varname (更多信息.
  • The Tcl's global command has the semantics defined as global varname ?varname ...?, that is, it just declares the names which are passed to it as referring to global variables. Hence a call global set rate02 2.0Mb just declares the names "set", "rate02" and "2.0Mb" to denote global variables. Most probably not what you wanted.
  • the duplex-link subcommand probably expects a value as its "rate" argument, and you're passing it the name of a variable. Seemingly, some NS2 code then attempts to interpret such a string as a floating-point value, which fails for obvious reasons. Hence you have to dereference a variable to get its value when you want to pass that variable's value to a command. This is done using the set varname command or the shorthand notation using the $ syntax: $varname (more info about this).

看起来您应该从Tcl 教程开始处理这些基础知识.

Look like you should start with the Tcl tutorial which deals with these basics.