math::decimal(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– math::decimal β€” the decimal arithmetic glow-up πŸ’–

okay bestie, so this is math::decimal, a Tcllib package (the man page calls it math::decimal(3tcl)), and it's genuinely the moment decimal math stops being a tragedy. no cap.


✨ NAME ✨

math::decimal β€” general decimal arithmetic. that's it. that's the whole name. iconic.


🌸 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

look at all those procedures. she's got range. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so the decimal package gives you decimal arithmetic support for both limited precision floating point and arbitrary precision floating point. and if that wasn't enough, integer arithmetic is supported too. overachiever behavior, honestly. πŸŽ€

if you want the full lore and the specifications this package depends on, they live on the general decimal arithmetic page at http://speleotrove.com/decimal. this package provides for:

  • a new data type, decimal, which is represented as a list containing sign, mantissa and exponent. (yes bestie, a list. sign, mantissa, exponent. that's the whole vibe.)
  • arithmetic operations on those decimal numbers such as addition, subtraction, multiplication, etc...

numbers get converted to decimal format using the operation ::math::decimal::fromstr.

numbers get converted back to string format using the operation ::math::decimal::tostr.


πŸ”₯ EXAMPLES πŸ”₯

this section shows some simple examples. since the whole point of this library is to perform decimal math operations, examples may honestly be the easiest way to learn how to work with it, and to see the difference between using this package and just sticking with expr. for info about individual procedures, consult the API section of this man page. πŸ’œ

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.

see that 8.399999999999999? that's the villain origin story right there. this package is the redemption arc. 😭✨


πŸ‘‘ API πŸ‘‘

  • ::math::decimal::fromstr string β€” convert string into a decimal. the glow-up converter. πŸͺ„
  • ::math::decimal::tostr decimal β€” convert decimal into a string representing the number in base 10.
  • ::math::decimal::setVariable variable setting β€” sets the variable to setting. the valid variables are:
    • rounding β€” the method of rounding to use during rescale. valid methods are round_half_even, round_half_up, round_half_down, round_down, round_up, round_floor, round_ceiling.
    • precision β€” maximum number of digits allowed in mantissa.
    • extended β€” set to 1 for extended mode. 0 for simplified mode.
    • maxExponent β€” maximum value for the exponent. defaults to 999.
    • minExponent β€” minimum value for the exponent. default to -998.
  • ::math::decimal::add a b
  • ::math::decimal::+ a b β€” return the sum of the two decimals a and b. besties adding up. 🫢
  • ::math::decimal::subtract a b
  • ::math::decimal::- a b β€” return the differnece of the two decimals a and b. (yes, "differnece" β€” that's the original's spelling, we do not correct the queen.)
  • ::math::decimal::multiply a b
  • ::math::decimal::* a b β€” return the product of the two decimals a and b.
  • ::math::decimal::divide a b
  • ::math::decimal::/ a b β€” return the quotient of the division between the two decimals a and b.
  • ::math::decimal::divideint a b β€” return a the integer portion of the quotient of the division between decimals a and b. (again, original wording, we don't touch it.)
  • ::math::decimal::remainder a b β€” return the remainder of the division between the two decimals a and b.
  • ::math::decimal::abs decimal β€” return the absolute value of the decimal. no negativity allowed. ✨
  • ::math::decimal::compare a b β€” compare the two decimals a and b, returning 0 if a == b, 1 if a > b, and -1 if a < b.
  • ::math::decimal::max a b β€” compare the two decimals a and b, and return a if a >= b, and b if a < b.
  • ::math::decimal::maxmag a b β€” compare the two decimals a and b while ignoring their signs, and return a if abs(a) >= abs(b), and b if abs(a) < abs(b).
  • ::math::decimal::min a b β€” compare the two decimals a and b, and return a if a <= b, and b if a > b.
  • ::math::decimal::minmag a b β€” compare the two decimals a and b while ignoring their signs, and return a if abs(a) <= abs(b), and b if abs(a) > abs(b).
  • ::math::decimal::plus a β€” return the result from ::math::decimal::+ 0 $a.
  • ::math::decimal::minus a β€” return the result from ::math::decimal::- 0 $a.
  • ::math::decimal::copynegate a β€” returns a with the sign flipped. πŸ”„
  • ::math::decimal::copysign a b β€” returns a with the sign set to the sign of the b.
  • ::math::decimal::is-signed decimal β€” return the sign of the decimal. the procedure returns 0 if the number is positive, 1 if it's negative.
  • ::math::decimal::is-zero decimal β€” return true if decimal value is zero, otherwise false is returned.
  • ::math::decimal::is-NaN decimal β€” return true if decimal value is NaN (not a number), otherwise false is returned.
  • ::math::decimal::is-infinite decimal β€” return true if decimal value is Infinite, otherwise false is returned.
  • ::math::decimal::is-finite decimal β€” return true if decimal value is finite, otherwise false is returned.
  • ::math::decimal::fma a b c β€” return the result from first multiplying a by b and then adding c. rescaling only occurs after completion of all operations. in this way the result may vary from that returned by performing the operations individually. (fused multiply-add said "i do it my way" πŸ’…)
  • ::math::decimal::round_half_even decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round to the nearest. if equidistant, round so the final digit is even.
  • ::math::decimal::round_half_up decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round to the nearest. if equidistant, round up.
  • ::math::decimal::round_half_down decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round to the nearest. if equidistant, round down.
  • ::math::decimal::round_down decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round toward 0. (truncate)
  • ::math::decimal::round_up decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round away from 0.
  • ::math::decimal::round_floor decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round toward -Infinity.
  • ::math::decimal::round_ceiling decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round toward Infinity.
  • ::math::decimal::round_05up decimal digits β€” rounds decimal to digits number of decimal points with the following rules: round zero or five away from 0. the same as round-up, except that rounding up only occurs if the digit to be rounded up is 0 or 5, and after overflow the result is the same as for round-down. (she's specific. she's particular. she's that girl.)

πŸ’€ BUGS, IDEAS, FEEDBACK πŸ’€

ngl, this document, and the package it describes, will undoubtedly contain bugs and other problems. it's giving "we know". please report such in the category decimal of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist. please also report any ideas for enhancements you may have for either the package and/or the documentation. we love a suggestion era. πŸ™ˆ

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 (c) 2011 Mark Alston <mark at beernut dot com>


okay bestie that's the whole page, we ate and left no crumbs πŸ“ this was tcllib version 1.0.5, dated back to the math::decimal(3tcl) era, and honestly? still slaying. go forth and do decimal math. periodt. πŸ’–πŸ§šβ­

πŸ“¦ where this page comes from