Tcl 9.1/Tk9.1 Documentation > Tcl Commands, version 9.1b0 > modf

Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API

NAME

modf — Split a floating-point number into integer and fraction parts

SYNOPSIS

package require tcl 9.1
modf value

DESCRIPTION

The modf command takes a floating-point number, value, and returns a list of two values. The first element of the result is the whole integer part (as a floating-point number) of value, and the second element of the result is the fractional part of value. The signs of the two elements will match the sign of value.

EXAMPLE

It should be noted that the fractional part may have some digits that appear to be undetermined by the input. This is due to the nature of approximation inherent in floating-point arithmetic; the digits will apparently disappear if the two parts are added back together.

set value 123.456
lassign [modf $value] whole part
set sum [expr {$whole + $part}]
puts "$value -> $whole $part -> $sum"
#       123.456 -> 123.0 0.45600000000000307 -> 123.456

SEE ALSO

expr, frexp, mathfunc

KEYWORDS

floating point
Copyright © 2025 Donal Fellows