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))
Rounding Down in Tcl

Posted by eyarrow (eyarrow), 26 January 2004
Hi,

Is there any package I can call.... so that I can round down in Tcl as opposed to rounding up?

Many thanks

Elliot


Posted by admin (Graham Ellis), 26 January 2004
Hmm.  I thought Tcl rounded down ...

Code:
% expr 10/3
3
% expr 11/3
3
% expr 12/3
4
% expr 10./3
3.33333333333
% expr 11./3
3.66666666667
% expr -11/3
-4
%


All of these are rounding down except the 11./3 which has rounded up in about the 12th significant figure.

The -4 answer to the last test might be a suprise, but this is still rounding down.

Can you post a more specific example of the sort of behaviour that you want to change, and I'll answer further

Posted by eyarrow (eyarrow), 26 January 2004
Graham,

Many thanks.. what I am refering to is as follows:-

If we wish to round to 3 DP.... then normal rounding is as follows

1.64354          =>   1.644

If we were to round down to 3Dp... then it would be as follows

1.64354          =>  1.643

I believe, this is called ROUND_HALF_DOWN in Java.

Many thanks

Elliot

Posted by admin (Graham Ellis), 26 January 2004
Easiest way is to use the floor function, but that rounds down to the nearest whole number.   Here's a definition of a proc that does want you want (shifts your value, floors it, shifts it back)

Code:
proc round_down {val rounder} {
       set nval [expr floor($val*$rounder) /$rounder]
       return $nval
       }


Demo of that in use:

Code:
% source ndp.tcl
% round_down 123.456789 1000
123.456
%




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