UPGRADE YOUR BROWSER

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 
Search instead for 
Did you mean: 
Highlighted
Visitor tcl_balaji
Visitor
304 Views
Registered: ‎07-05-2019

How to get absolute value

Jump to solution

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.tcl
Hello World! -2 3 abs(-2) can not find channel named "c" while executing "puts c [expr abs($x)+$y]" (file "main.tcl" line 7)
0 Kudos
1 Solution

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

Re: How to get absolute value

Jump to solution

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

 

------------------------------------------------------------------------------------------------------------------
Please mark the Answer as "Accept as solution" if this answered your question
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
6 Replies
Moderator
Moderator
234 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Jump to solution

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

 

 

------------------------------------------------------------------------------------------------------------------
Please mark the Answer as "Accept as solution" if this answered your question
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
0 Kudos
Visitor tcl_balaji
Visitor
206 Views
Registered: ‎07-05-2019

Re: How to get absolute value

Jump to solution
#!/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"
0 Kudos
Moderator
Moderator
202 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Jump to solution

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

 

------------------------------------------------------------------------------------------------------------------
Please mark the Answer as "Accept as solution" if this answered your question
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
0 Kudos
Visitor tcl_balaji
Visitor
194 Views
Registered: ‎07-05-2019

Re: How to get absolute value

Jump to solution

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

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

0 Kudos
Moderator
Moderator
184 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Jump to solution

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

 

------------------------------------------------------------------------------------------------------------------
Please mark the Answer as "Accept as solution" if this answered your question
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
Moderator
Moderator
175 Views
Registered: ‎04-24-2013

Re: How to get absolute value

Jump to solution

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

 

------------------------------------------------------------------------------------------------------------------
Please mark the Answer as "Accept as solution" if this answered your question
Give Kudos to a post which you think is helpful and may help other users
------------------------------------------------------------------------------------------------------------------
0 Kudos