math::decimal(n)
β¨ math::decimal(n): Tcl Decimal Arithmetic Library β¨
okay bestie, welcome to the manual page for math::decimal(n) β the Tcl Decimal Arithmetic Library. This is where decimal math gets its main character moment. π
πΈ NAME πΈ
math::decimal β General decimal arithmetic. No cap, it's giving exact decimal energy. π
β¨ 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 π¦
So the decimal package? It literally provides decimal arithmetic support for both limited precision floating point and arbitrary precision floating point. And yes, integer arithmetic is supported too. Periodt. π
If you want the receipts β more info and the specifications this package depends on β they're on the general decimal arithmetic page at http://speleotrove.com/decimal. Go, bestie. π
This package provides for:
- A new data type
decimalwhich is represented as a list containing sign, mantissa and exponent. It's giving structured. - Arithmetic operations on those decimal numbers such as addition, subtraction, multiplication, etc... you know the vibe. β¨
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 π
::math::decimal::fromstr stringβ Convertstringinto a decimal. Slay. β¨::math::decimal::tostr decimalβ Convertdecimalinto a string representing the number in base 10. Yes bestie, base 10.::math::decimal::setVariable variable settingβ Sets thevariabletosetting. Valid variables are:roundingβ Method of rounding to use during rescale. Valid methods areround_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 to1for extended mode.0for simplified mode.maxExponentβ Maximum value for the exponent. Defaults to999.minExponentβ Minimum value for the exponent. Default to-998.
::math::decimal::add a b/::math::decimal::+ a bβ Return the sum of the two decimalsaandb. Addition? We love to see it. π::math::decimal::subtract a b/::math::decimal::- a bβ Return the difference of the two decimalsaandb. (Original said "differnece", but we fixed the typo, no cap.)::math::decimal::multiply a b/::math::decimal::* a bβ Return the product of the two decimalsaandb. Multiplication ate. π₯::math::decimal::divide a b/::math::decimal::/ a bβ Return the quotient of the division between the two decimalsaandb. Division is giving main character energy. π::math::decimal::divideint a bβ Return the integer portion of the quotient of the division between decimalsaandb. Integer only, bestie.::math::decimal::remainder a bβ Return the remainder of the division between the two decimalsaandb. Leftovers, but make it math. π::math::decimal::abs decimalβ Return the absolute value of thedecimal. Always positive vibes. β¨::math::decimal::compare a bβ Compare the two decimalsaandb, returning0ifa == b,1ifa > b, and-1ifa < b. The comparison is comparing.::math::decimal::max a bβ Compare the two decimalsaandb, and returnaifa >= b, andbifa < b. Biggest one wins. π::math::decimal::maxmag a bβ Compare the two decimalsaandbwhile ignoring their signs, and returnaifabs(a) >= abs(b), andbifabs(a) < abs(b). Magnitude only, sign who? π::math::decimal::min a bβ Compare the two decimalsaandb, and returnaifa <= b, andbifa > b. Smallest one, you're up. π::math::decimal::minmag a bβ Compare the two decimalsaandbwhile ignoring their signs, and returnaifabs(a) <= abs(b), andbifabs(a) > abs(b). Again, signs are not invited. β¨::math::decimal::plus aβ Return the result from::math::decimal::+ 0 $a. Unary plus, but make it decimal.::math::decimal::minus aβ Return the result from::math::decimal::- 0 $a. Unary minus, periodt.::math::decimal::copynegate aβ Returnsawith the sign flipped. Flip it like it's hot. π₯::math::decimal::copysign a bβ Returnsawith the sign set to the sign of theb. Sign borrowing, but consensual.::math::decimal::is-signed decimalβ Return the sign of thedecimal. The procedure returns0if the number is positive,1if it's negative. Binary sign check, iykyk.::math::decimal::is-zero decimalβ Return true if decimal value is zero, otherwise false is returned. Zero check, bestie. π::math::decimal::is-NaN decimalβ Return true if decimal value is NaN (not a number), otherwise false is returned. NaN is not a number, no cap.::math::decimal::is-infinite decimalβ Return true if decimal value is Infinite, otherwise false is returned. Infinity vibes. π::math::decimal::is-finite decimalβ Return true if decimal value is finite, otherwise false is returned. Finite and fine.::math::decimal::fma a b cβ Return the result from first multiplyingabyband then addingc. 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, slay. π₯::math::decimal::round_half_even decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round to the nearest. If equidistant, round so the final digit is even. Even numbers only, bestie. π::math::decimal::round_half_up decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round to the nearest. If equidistant, round up. Up up up. β¬οΈ::math::decimal::round_half_down decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round to the nearest. If equidistant, round down. Down down down. β¬οΈ::math::decimal::round_down decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round toward0. (Truncate). Truncation nation. βοΈ::math::decimal::round_up decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round away from0. Bye, zero.::math::decimal::round_floor decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round toward-Infinity. Floor gang. π§::math::decimal::round_ceiling decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round towardInfinity. Ceiling gang. βοΈ::math::decimal::round_05up decimal digitsβ Roundsdecimaltodigitsnumber of decimal points with the following rules: Round zero or five away from0. The same as round-up, except that rounding up only occurs if the digit to be rounded up is0or5, and after overflow the result is the same as for round-down. Very specific, very iconic. π
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. Bestie, 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 package and/or documentation. We want the feedback. π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u. Yes, actual diffs. No "trust me bro" patches.
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. That's the way. β¨
π KEYWORDS π
decimal, math, tcl
π CATEGORY π
Mathematics
Β©οΈ COPYRIGHT Β©οΈ
Copyright (c) 2011 Mark Alston <mark at beernut dot com>
okay bestie, that's the whole manual for math::decimal(n) from tcllib version 1.0.5. Stay decimal, stay iconic. πβ¨ππ¦πΈπ
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2011 Mark Alston <mark at beernut dot com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms