We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for
Did you mean:
Visitor
422 Views
Registered: ‎07-05-2019

How to get absolute value

I want to extract the absolute value, I use the following code, but it gives me an error.

puts "Hello World!"
set x -2
set y 3
puts \$x
puts \$y
puts abs(\$x)
puts c [expr abs(\$x)+\$y]

Error message:

```\$tclsh main.tclHello World!
-2
3
abs(-2)
can not find channel named "c"
while executing
"puts c [expr abs(\$x)+\$y]"
(file "main.tcl" line 7)```
1 Solution

Accepted Solutions
Moderator
301 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Hi @tcl_balaji ,

You can't just type: puts abs\$x

or even: puts abs \$x which would make more sense syntax wise as tcl has no idea what abs means.

This is why you have to give it context using the ::tcl::mathfunc:: to let tcl know you are using a maths function.

Have a look at the link I provided in the first response.

Then you can use the following to show the absolute value of x

puts [::tcl::mathfunc::abs \$x]

Best Regards
Aidan

------------------------------------------------------------------------------------------------------------------
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
6 Replies
Moderator
352 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Hi @tcl_balaji ,

Try using:

::tcl::mathfunc::abs \$x

and in the last line

Set c ...

puts \$c

You can check out this site:

https://www.tcl.tk/man/tcl8.7/TclCmd/mathfunc.htm#M6

Best Regards
Aidan

------------------------------------------------------------------------------------------------------------------
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
Visitor
324 Views
Registered: ‎07-05-2019

Re: How to get absolute value

#!/usr/bin/tclsh

puts "Hello World!"
set x -2
set y 3
puts \$x
puts \$y
::tcl::mathfunc::abs \$x
set c [expr abs(\$x)+\$y]
puts \$c

I still can not print absolute "x"
Moderator
320 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Hi @tcl_balaji ,

You misunderstood the usage, try the following

puts "Hello World!"
set x -2
set y 3
puts \$x
puts \$y
set c [expr [::tcl::mathfunc::abs \$x]+\$y]
puts \$c

Best Regards
Aidan

------------------------------------------------------------------------------------------------------------------
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
Visitor
312 Views
Registered: ‎07-05-2019

Re: How to get absolute value

I want to print the absolute "x" value first.

like puts abs\$x, but this statement gives an error.

Moderator
302 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Hi @tcl_balaji ,

You can't just type: puts abs\$x

or even: puts abs \$x which would make more sense syntax wise as tcl has no idea what abs means.

This is why you have to give it context using the ::tcl::mathfunc:: to let tcl know you are using a maths function.

Have a look at the link I provided in the first response.

Then you can use the following to show the absolute value of x

puts [::tcl::mathfunc::abs \$x]

Best Regards
Aidan

------------------------------------------------------------------------------------------------------------------
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
Moderator
293 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Hi @tcl_balaji ,

you can also try this format

puts "Hello World!"
set x -2
set y 3
puts \$x
puts \$y
puts [expr abs( \$x)]
set c [expr [expr abs( \$x)]+\$y]
puts \$c

Best Regards
Aidan

------------------------------------------------------------------------------------------------------------------