Tcl Library Source Code

Documentation
Login
EuroTcl/OpenACS 11 - 12 JULY 2024, VIENNA


[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]

NAME

math::decimal - General decimal arithmetic

Table Of Contents

SYNOPSIS

package require Tcl ?8.5 9?
package require math::decimal 1.0.3

::math::decimal::fromstr string
::math::decimal::tostr decimal
::math::decimal::setVariable variable setting
::math::decimal::add a b
::math::decimal::+ a b
::math::decimal::subtract a b
::math::decimal::- a b
::math::decimal::multiply a b
::math::decimal::* a b
::math::decimal::divide a b
::math::decimal::/ a b
::math::decimal::divideint a b
::math::decimal::remainder a b
::math::decimal::abs decimal
::math::decimal::compare a b
::math::decimal::max a b
::math::decimal::maxmag a b
::math::decimal::min a b
::math::decimal::minmag a b
::math::decimal::plus a
::math::decimal::minus a
::math::decimal::copynegate a
::math::decimal::copysign a b
::math::decimal::is-signed decimal
::math::decimal::is-zero decimal
::math::decimal::is-NaN decimal
::math::decimal::is-infinite decimal
::math::decimal::is-finite decimal
::math::decimal::fma a b c
::math::decimal::round_half_even decimal digits
::math::decimal::round_half_up decimal digits
::math::decimal::round_half_down decimal digits
::math::decimal::round_down decimal digits
::math::decimal::round_up decimal digits
::math::decimal::round_floor decimal digits
::math::decimal::round_ceiling decimal digits
::math::decimal::round_05up decimal digits

DESCRIPTION

The decimal package provides decimal arithmetic support for both limited precision floating point and arbitrary precision floating point. Additionally, integer arithmetic is supported.

More information and the specifications on which this package depends can be found on the general decimal arithmetic page at http://speleotrove.com/decimal This package provides for:

Numbers are converted to decimal format using the operation ::math::decimal::fromstr.

Numbers are converted back to string format using the operation ::math::decimal::tostr.

EXAMPLES

This section shows some simple examples. Since the purpose of this library is to perform decimal math operations, examples may be the simplest way to learn how to work with it and to see the difference between using this package and sticking with expr. Consult the API section of this man page for information about individual procedures.

package require math::decimal

# Various operations on two numbers.
# We first convert them to decimal format.
set a [::math::decimal::fromstr 8.2]
set b [::math::decimal::fromstr .2]

# Then we perform our operations. Here we add
set c [::math::decimal::+ $a $b]

# Finally we convert back to string format for presentation to the user.
puts [::math::decimal::tostr $c] ; # => will output 8.4

# Other examples
#
# Subtraction
set c [::math::decimal::- $a $b]
puts [::math::decimal::tostr $c] ; # => will output 8.0

# Why bother using this instead of simply expr?
puts [expr {8.2 + .2}] ; # => will output 8.399999999999999
puts [expr {8.2 - .2}] ; # => will output 7.999999999999999
# See http://speleotrove.com/decimal to learn more about why this happens.

API

Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category decimal of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.

KEYWORDS

decimal, math, tcl

CATEGORY

Mathematics

COPYRIGHT

Copyright © 2011 Mark Alston