math::fuzzy(n)
π math::fuzzy β the Tcl Math Library's fuzzy float bestie π
okay so real talk, this is math::fuzzy(n), straight out of the Tcl Math Library, and it's here to fix the fact that computers are lowkey lying to you about decimal numbers. yes bestie, floating point is a scam and this package is the coping mechanism. let's get into it π«Ά
β¨ SYNOPSIS β¨
like, the whole vibe in one block:
package require Tcl ?8.3?
package require math::fuzzy ?0.2?
::math::fuzzy::teq value1 value2
::math::fuzzy::tne value1 value2
::math::fuzzy::tge value1 value2
::math::fuzzy::tle value1 value2
::math::fuzzy::tlt value1 value2
::math::fuzzy::tgt value1 value2
::math::fuzzy::tfloor value
::math::fuzzy::tceil value
::math::fuzzy::tround value
::math::fuzzy::troundn value ndigits
πΈ DESCRIPTION πΈ
so the Fuzzy package exists to solve the classic floating-point problems in a systematic way, and honestly? it's giving public service. the problems are:
- comparing two numbers that are "supposed" to be identical, like
1.0and2.1/(1.2+0.9), is not guaranteed to give the intuitive result. not me getting betrayed by basic arithmetic π - rounding a number that is halfway between two integer numbers can cause strange errors, like
int(100.0*2.8) != 28but27. the audacity.
the Fuzzy package helps sort out this type of problem by defining "fuzzy" comparison procedures for floating-point numbers π
it does this by allowing for a small margin that's determined automatically β the margin is three times the "epsilon" value, that is, three times the smallest number eps such that 1.0 and 1.0+$eps can be distinguished. in Tcl, which uses double precision floating-point numbers, this is typically 1.1e-16. tiny margin, huge slay.
π PROCEDURES π
effectively the package provides the following procedures, and yes, we're doing all of them, no skips:
::math::fuzzy::teq value1 value2β compares two floating-point numbers and returns1if their values fall within a small range. otherwise it returns0. bestie behavior.::math::fuzzy::tne value1 value2β returns the negation, that is, if the difference is larger than the margin, it returns1. it's the hater ofteq.::math::fuzzy::tge value1 value2β compares two floating-point numbers and returns1if their values either fall within a small range or if the first number is larger than the second. otherwise it returns0. either/or icon.::math::fuzzy::tle value1 value2β returns1if the two numbers are equal according to[teq]or if the first is smaller than the second.::math::fuzzy::tlt value1 value2β returns the opposite of[tge]. no thoughts, just inversion.::math::fuzzy::tgt value1 value2β returns the opposite of[tle]. same energy, other direction.::math::fuzzy::tfloor valueβ returns the integer number that is lower or equal to the given floating-point number, within a well-defined tolerance. it'sfloorbut with β¨emotional supportβ¨::math::fuzzy::tceil valueβ returns the integer number that is greater or equal to the given floating-point number, within a well-defined tolerance. ceiling rights fr fr.::math::fuzzy::tround valueβ rounds the floating-point number off.::math::fuzzy::troundn value ndigitsβ rounds the floating-point number off to the specified number of decimals (Pro memorie).
π usage π
this is the way you actually call them, and the way that this looks so clean is iconic:
if { [teq $x $y] } { puts "x == y" }
if { [tne $x $y] } { puts "x != y" }
if { [tge $x $y] } { puts "x >= y" }
if { [tgt $x $y] } { puts "x > y" }
if { [tlt $x $y] } { puts "x < y" }
if { [tle $x $y] } { puts "x <= y" }
set fx [tfloor $x]
set fc [tceil $x]
set rounded [tround $x]
set roundn [troundn $x $nodigits]
π§ TEST CASES π§
the problems that can occur with floating-point numbers are illustrated by the test cases in the file "fuzzy.test", and the receipts are:
- several test case use the ordinary comparisons, and they fail invariably to produce understandable results. π every single time.
- one test case uses
[expr]without braces ({and}). it too fails. not me forgetting the braces and getting absolutely humbled.
the conclusion from this β and this is the takeaway, write it on your mirror β is that any expression should be surrounded by braces, because otherwise very awkward things can happen if you need accuracy. furthermore, accuracy and understandable results are enhanced by using these "tolerant" or fuzzy comparisons. periodt.
also note that besides the Tcl-only package, there is also a C-based version. options, we love to see it.
π REFERENCES π
giving credit where credit is due, these are the ancestors:
- original implementation in Fortran by dr. H.D. Knoble (Penn State University).
- P. E. Hagerty, "More on Fuzzy Floor and Ceiling," APL QUOTE QUAD 8(4):20-24, June 1978. note that
TFLOOR=FL5took five years of refereed evolution (publication). the patience? unmatched. - L. M. Breed, "Definitions for Fuzzy Floor and Ceiling", APL QUOTE QUAD 8(3):16-23, March 1978.
- D. Knuth, Art of Computer Programming, Vol. 1, Problem 1.2.4-5. yes, the Knuth.
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. (their words, and honestly, self-aware king behavior.)
please report such in the category math :: fuzzy 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 good ideas, bestie, don't gatekeep π«Ά
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 β
floating-point, math, rounding
π CATEGORY π
Mathematics
and that's the whole page, bestie π
brought to you by tcllib, version 0.2 β go forth and compare floats fuzzily, and may your 1.0 always equal your 2.1/(1.2+0.9). mwah ππ¦β¨
π¦ 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
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms