math::fuzzy(3tcl)
math::fuzzy π
okay bestie, this is the Tcl Math Library's fuzzy comparison page, math::fuzzy(3tcl) β the manual for math::fuzzy, the package that saves you from floating-point drama. Yes, that kind of drama. It's giving "why is 1.0 not equal to itself" and honestly? relatable. π«Ά
β¨ NAME β¨
math::fuzzy β Fuzzy comparison of floating-point numbers. Not fuzzy like a peach π, fuzzy like tolerant, and that's the whole point.
β¨ SYNOPSIS β¨
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 here's the tea β. The package Fuzzy exists to solve the common problems you get with floating-point numbers, in a systematic way:
- 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 the vibe. - Rounding a number that is halfway between two integer numbers can cause strange errors, like
int(100.0*2.8) != 28but 27. That's 27. Twenty-seven. When you wanted 28. π
The Fuzzy package is meant to help sort out this type of problem by defining "fuzzy" comparison procedures for floating-point numbers. How does it do that, you ask? Glad you asked. It allows 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! π§ Basically a crumb of a number. And yet it ruins everything if you don't respect it.
β¨ PROCEDURES β¨
Effectively, the package provides the following procedures:
::math::fuzzy::teq value1 value2β Compares two floating-point numbers and returns1if their values fall within a small range. Otherwise it returns0. Equality but with grace.::math::fuzzy::tne value1 value2β Returns the negation, that is, if the difference is larger than the margin, it returns1. Not equal, no cap.::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. Greater-or-equal, tolerant edition.::math::fuzzy::tle value1 value2β Returns1if the two numbers are equal according to[teq]or if the first is smaller than the second. Less-or-equal, bestie-approved.::math::fuzzy::tlt value1 value2β Returns the opposite of[tge]. Flip it.::math::fuzzy::tgt value1 value2β Returns the opposite of[tle]. Flip it again.::math::fuzzy::tfloor valueβ Returns the integer number that is lower or equal to the given floating-point number, within a well-defined tolerance. Down we go.::math::fuzzy::tceil valueβ Returns the integer number that is greater or equal to the given floating-point number, within a well-defined tolerance. Up we go.::math::fuzzy::tround valueβ Rounds the floating-point number off. Round it, queen.::math::fuzzy::troundn value ndigitsβ Rounds the floating-point number off to the specified number of decimals (Pro memorie). Precision but make it specific.
π Usage π
Copy-paste these into your life, they're 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":
- Several test case use the ordinary comparisons, and they fail invariably to produce understandable results. Every single time. It's a curse. π
- One test case uses
[expr]without braces ({and}). It too fails. Same energy.
The conclusion from this is that any expression should be surrounded by braces, because otherwise very awkward things can happen if you need accuracy. Protect your braces like your peace of mind. Furthermore, accuracy and understandable results are enhanced by using these "tolerant" or fuzzy comparisons.
Note: besides the Tcl-only package, there is also a C-based version. iykyk.
β¨ REFERENCES β¨
- Original implementation in Fortran by dr. H.D. Knoble (Penn State University). Foundational. Respect.
- P. E. Hagerty, "More on Fuzzy Floor and Ceiling," APL QUOTE QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5 took five years of refereed evolution (publication). Five years. The dedication.
- 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. The Knuth mention. Main character energy. π
β¨ BUGS, IDEAS, FEEDBACK β¨
This document, and the package it describes, will undoubtedly contain bugs and other problems. Frrrr, they said it themselves. Please report such in the category math :: fuzzy 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. They want your ideas, bestie, don't be shy. π«Ά
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. Left-most button, secondary nav bar. You got this. π
β¨ KEYWORDS β¨
floating-point, math, rounding π
β¨ CATEGORY β¨
Mathematics π₯
And that's the whole page, bestie. Sourced from tcllib, version 0.2, dated on the original page's footer as math::fuzzy(3tcl). Go forth and compare fuzzily, you absolute legend. β¨ππ¦
π¦ 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
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms