Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
How function can returns floating-point values

Posted by triclosan (triclosan), 20 July 2005
For example:
Code:
proc funct {} {
return [expr 10/3]      
}


Function 'funct' returns '3'. How i can get floating-point value?

Posted by admin (Graham Ellis), 20 July 2005
By doing a floating point operation rather than an integer one.

For example

expr 10./3

(note the extra "." after the 10)

Posted by triclosan (triclosan), 20 July 2005
Thnx for answer!

But it's so inconveniently. For example I write plugin for some software on Tcl/Tk and it software application sends to Tcl value '10'. How I do convert it to '10.'  ?


Posted by admin (Graham Ellis), 21 July 2005
In most circumstances, the way Tcl does it is very clever and elegant - you do a division using floating point numbers and it does a floating point division ... give it integers and it knows to do an integer division.

If you want to divide an integer by an integer using a floating point division, you need to convert at least one of them. Personally, I often multiply by 1.0 or add 0.0 before dividing as that will work no matter what type of number the input variables are.

Code:
set tom 10
set fred 3
set result [expr 1.0 * $tom / $fred]


If you can be certain that one of your input variables is an integer, you can just add a "." in the code:

Code:
set tom 10
set fred 3
set result [expr $tom / $fred.]


Posted by triclosan (triclosan), 21 July 2005
on 07/21/05 at 06:09:56, Graham Ellis wrote:
I often multiply by 1.0 or add 0.0 before dividing







This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho