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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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., 2022: 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