math::constants(3tcl)
π math::constants β the Tcl Math Library's little box of β¨famous numbersβ¨
okay so bestie, this is math::constants(3tcl) from the Tcl Math Library, and it's basically a snack drawer full of math constants so you never have to type 4.0*atan(1.0) ever again π«Ά
πΈ NAME πΈ
math::constants β mathematical and numerical constants, but make it convenient π
β¨ SYNOPSIS β¨
package require Tcl ?8.3?
package require math::constants ?1.0.4?
::math::constants::constants args
::math::constants::print-constants args
π¦ DESCRIPTION π¦
So this package defines some common mathematical and numerical constants. The whole point: by using it, you get consistent values for numbers like pi and ln(10). No cap, consistency is the vibe here.
It defines two commands:
- One for importing the constants π₯
- One for reporting which constants are defined and what values they actually have π’
The motivation β and honestly this is so real β is that with (mathematical) computations, you very often need a good approximation to, say, the ratio of degrees to radians. Like, you could just define it yourself:
variable radtodeg [expr {180.0/(4.0*atan(1.0))}]
and then use the variable radtodeg whenever you need the conversion. Sure. But this has two drawbacks:
- You need to remember the proper formula or value and that is error-prone. Not the moment, bestie.
- Especially with the use of mathematical functions like
atanyou assume that they have been accurately implemented. This is seldom or never the case and for each platform you can get subtle differences. It's giving chaos across platforms π
Here is the way you can do it with the math::constants package:
package require math::constants
::math::constants::constants radtodeg degtorad
which creates two variables, radtodeg and (its reciprocal) degtorad in the calling namespace. Iconic behaviour, honestly π
Constants that have been defined (their values are mostly taken from mathematical tables with more precision than usually can be handled) include:
- basic constants like
pi,e,gamma(Euler's constant) - derived values like
ln(10)andsqrt(2) - purely numerical values such as
1/3that are included for convenience and for the fact that certain seemingly trivial computations like:
set value [expr {3.0*$onethird}]
give exactly the value you expect (if IEEE arithmetic is available). chef's kiss π
The full set of named constants is listed in section Constants below, bestie, don't worry, we got you π
π PROCEDURES π
The package defines the following public procedures:
::math::constants::constants argsβ import the constants whose names are given as arguments π::math::constants::print-constants argsβ print the constants whose names are given as arguments on the screen (name, value and description) or, if no arguments are given, print all defined constants. This is mainly a convenience procedure π
π CONSTANTS π
The full roster, no gatekeeping:
piβ ratio of circle circumference to diameter π₯§eβ base for natural logarithmln10β natural logarithm of 10phiβ golden ratio β¨gammaβ Euler's constantsqrt2β square root of 2thirdrt2β one-third power of 2sqrt3β square root of 3radtodegβ conversion from radians to degreesdegtoradβ conversion from degrees to radiansonethirdβ one third (0.3333....)twothirdsβ two thirds (0.6666....)onesixthβ one sixth (0.1666....)hugeβ (approximately) largest numbertinyβ (approximately) smallest number not equal zeroepsβ smallest number such that 1+eps != 1
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. Not me being honest about it π Please report such in the category math :: constants of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation. We love ideas fr fr π«Ά
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 π₯
constants, degrees, e, math, pi, radians
π CATEGORY π
Mathematics
π COPYRIGHT π
Copyright (c) 2004 Arjen Markus <arjenmarkus@users.sourceforge.net>
okay that's the whole thing bestie, you ate it, go import some constants π β¨
this has been math::constants, version 1.0.4, brought to you by tcllib β mwah, ily, go compute something gorgeous π¦πΈπ
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2004 Arjen Markus <arjenmarkus@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms