Book Image

Tcl/Tk 8.5 Programming Cookbook

Book Image

Tcl/Tk 8.5 Programming Cookbook

Overview of this book

With Tcl/Tk, you can create full-featured cross-platform applications in a simple and easy-to-understand way without any expensive development package; the only tools required are a simple text editor and your imagination. This practical cookbook will help you to efficiently interact with editors, debuggers, and shell type interactive programs using Tcl/Tk 8. This cookbook will comprehensively guide you through practical implementation of Tcl/Tk 8.5 commands and tools. This book will take you through all the steps needed to become a productive programmer in Tcl/Tk 8. Right from guiding you through the basics to creating a stand-alone application, it provides complete explanation of all the steps along with handy tips and tricks. The book begins with an introduction to the Tcl shell, syntax, variables, and programming best practices in the language. It then explores procedures and the flow of events with control constructs followed by advanced error trapping and recovery. From Chapter 4, a detailed study of string expressions and handling enables you to handle various string functions and use lists to expand the string functionality. The book then discusses in-depth the Tcl Dictionary and how to utilize it to store and retrieve data. File operations and Tk GUI handling are covered extensively along with a developing a real-world address book application to practice the concepts learned.
Table of Contents (20 chapters)
Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface

Mathematical functions


Mathematical functions (such as sine and cosine) are replaced with a call to the Tcl mathfunc namespace. This does not require any additional syntax to access the namespace as it is called automatically. These are invoked by passing the Function followed by the value or values to evaluate to the expr command. Those functions that accept multiple arguments require that the arguments be comma delimited. The default Mathematical functions are listed below in alphabetical order. These functions require a specific syntax (for example expr {function(value,value)}) to be accessed, as described in the Computing mathematical expressions section that follows:

Function

Result

abs arg

Returns the absolute value of arg.

Numeric operators may be integer or floating-point. Value is returned in the same format.

acos arg

Returns the arc cosine of arg.

asin arg

Returns the arc sine of arg.

atan arg

Returns the Arc Tangent of x/y.

bool arg

Returns the Boolean value of arg where non-numeric values are true, otherwise the value is false.

ceil arg

Returns the smallest floating-point integer value not less than arg.

Any numeric value is acceptable.

cos arg

Returns the cosine of arg, measured in radians.

If the result produces an over-flow, an error is returned.

double arg

Converts arg to its floating-point value.

May return INF or INF when the numeric value is such that it exceeds the floating-point value.

entier arg

Converts arg to its integer value.

exp arg

Returns the exponential of arg.

If the result produces an over-flow, an error is returned.

floor arg

Returns the largest floating-point integer not greater than arg.

The argument may be any numeric value.

fmod x y

Returns the remainder of x/y as a floating-point integer.

If y is a zero (0), then an error is returned.

hypot x y

Returns the length of the hypotenuse of a right angled triangle.

int arg

Returns the low order bits of arg up to the machine word size.

isqrt arg

Returns the integer portion of the square root of arg.

Arg must be a positive value (integer or floating-point).

log arg

Returns the natural logarithm of arg.

arg must be a positive value.

log10 arg

Returns the base 10 logarithm of arg.

arg must be a positive value.

max arg

This function accepts one or more numeric values and returns the greatest.

min arg

This function accepts one or more numeric and returns the least one.

pow x y

Returns the value of x raised to the power y.

If x is zero (0), y must be an integer value.

rand

Returns a pseudo-random floating-point integer in the range of 0, 1.

round arg

Returns the rounded value of arg if arg is an integer value.

If arg is not an integer, it is converted to an integer by rounding and the converted value is returned.

sin arg

Returns the sine of arg as radians.

sinh arg

Returns the hyperbolic sin of arg.

If the result produces an over-flow, an error is returned.

sqrt arg

Returns the square root of arg.

Accepts any non-negative numeric value.

May return INF when the value is a numeric value that exceeds the square of the maximum value for the floating-point range.

srand arg

Resets the seed for the random number generator and returns a random number as described in rand.

tan arg

Returns the tangent of arg as radians.

tanh arg

Returns the hyperbolic tangent of arg.

wide arg

Returns the low order 64 bits of arg.

Accepts any numeric value.