NAME
math::fuzzy - Fuzzy comparison of floating-point numbers
SYNOPSIS
::::mmaatthh::::ffuuzzzzyy::::tteeqq value1 value2 ::::mmaatthh::::ffuuzzzzyy::::ttnnee value1 value2 ::::mmaatthh::::ffuuzzzzyy::::ttggee value1 value2 ::::mmaatthh::::ffuuzzzzyy::::ttllee value1 value2 ::::mmaatthh::::ffuuzzzzyy::::ttlltt value1 value2 ::::mmaatthh::::ffuuzzzzyy::::ttggtt value1 value2 ::::mmaatthh::::ffuuzzzzyy::::ttfflloooorr value ::::mmaatthh::::ffuuzzzzyy::::ttcceeiill value ::::mmaatthh::::ffuuzzzzyy::::ttrroouunndd value ::::mmaatthh::::ffuuzzzzyy::::ttrroouunnddnn value ndigitsDESCRIPTION
The package Fuzzy is meant to solve common problems with floating-point
numbers in a systematic way: +o Comparing two numbers that are "supposed" to be identical, like 1.0 and 2.1/(1.2+0.9) is not guaranteed to give the intuitive result. +o Rounding a number that is halfway two integer numbers can cause strange errors, like int(100.0*2.8) != 28 but 27 The Fuzzy package is meant to help sorting out this type of problems bydefining "fuzzy" comparison procedures for floating-point numbers. It
does so by allowing for a small margin that is 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 canbe distinguished.
In Tcl, which uses double precision floating-point numbers, this is
typically 1.1e-16.
PPRROOCCEEDDUURREESS Effectively the package provides the following procedures: ::::mmaatthh::::ffuuzzzzyy::::tteeqq value1 value2Compares two floating-point numbers and returns 1 if their val-
ues fall within a small range. Otherwise it returns 0. ::::mmaatthh::::ffuuzzzzyy::::ttnnee value1 value2 Returns the negation, that is, if the difference is larger than the margin, it returns 1. ::::mmaatthh::::ffuuzzzzyy::::ttggee value1 value2Compares two floating-point numbers and returns 1 if their val-
ues either fall within a small range or if the first number is larger than the second. Otherwise it returns 0. ::::mmaatthh::::ffuuzzzzyy::::ttllee value1 value2 Returns 1 if the two numbers are equal according to [teq] or if the first is smaller than the second. ::::mmaatthh::::ffuuzzzzyy::::ttlltt value1 value2 Returns the opposite of [tge]. ::::mmaatthh::::ffuuzzzzyy::::ttggtt value1 value2 Returns the opposite of [tle]. ::::mmaatthh::::ffuuzzzzyy::::ttfflloooorr value Returns the integer number that is lower or equal to the givenfloating-point number, within a well-defined tolerance.
::::mmaatthh::::ffuuzzzzyy::::ttcceeiill value Returns the integer number that is greater or equal to the givenfloating-point number, within a well-defined tolerance.
::::mmaatthh::::ffuuzzzzyy::::ttrroouunndd valueRounds the floating-point number off.
::::mmaatthh::::ffuuzzzzyy::::ttrroouunnddnn value ndigitsRounds the floating-point number off to the specified number of
decimals (Pro memorie). Usage: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]
TTEESSTT CCAASSEESSThe problems that can occur with floating-point numbers are illustrated
by the test cases in the file "fuzzy.test":
+o Several test case use the ordinary comparisons, and they fail invariably to produce understandable results +o One test case uses [expr] without braces ({ and }). It too fails. The conclusion from this 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" orfuzzy comparisons.
Note that besides the Tcl-only package, there is also a C-based ver-
sion. REFERENCESOriginal implementation in Fortran by dr. H.D. Knoble (Penn State Uni-
versity). P. E. Hagerty, "More on Fuzzy Floor and Ceiling," APL QUOTE QUAD8(4):20-24, June 1978. Note that TFLOOR=FL5 took five years of refereed
evolution (publication). L. M. Breed, "Definitions for Fuzzy Floor and Ceiling", APL QUOTE QUAD8(3):16-23, March 1978.
D. Knuth, Art of Computer Programming, Vol. 1, Problem 1.2.4-5.
KKEEYYWWOORRDDSSfloating-point, math, rounding
math 1.0 math::fuzzy(n)