1math::fuzzy(n)                 Tcl Math Library                 math::fuzzy(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       math::fuzzy - Fuzzy comparison of floating-point numbers
9

SYNOPSIS

11       package require Tcl  ?8.3?
12
13       package require math::fuzzy  ?0.2?
14
15       ::math::fuzzy::teq value1 value2
16
17       ::math::fuzzy::tne value1 value2
18
19       ::math::fuzzy::tge value1 value2
20
21       ::math::fuzzy::tle value1 value2
22
23       ::math::fuzzy::tlt value1 value2
24
25       ::math::fuzzy::tgt value1 value2
26
27       ::math::fuzzy::tfloor value
28
29       ::math::fuzzy::tceil value
30
31       ::math::fuzzy::tround value
32
33       ::math::fuzzy::troundn value ndigits
34
35_________________________________________________________________
36

DESCRIPTION

38       The package Fuzzy is meant to solve common problems with floating-point
39       numbers in a systematic way:
40
41       ·      Comparing two numbers that are "supposed" to be identical,  like
42              1.0  and  2.1/(1.2+0.9)  is not guaranteed to give the intuitive
43              result.
44
45       ·      Rounding a number that is halfway two integer numbers can  cause
46              strange errors, like int(100.0*2.8) != 28 but 27
47
48       The Fuzzy package is meant to help sorting out this type of problems by
49       defining "fuzzy" comparison procedures for floating-point numbers.   It
50       does so by allowing for a small margin that is determined automatically
51       - the margin is three times the "epsilon" value, that  is  three  times
52       the smallest number eps such that 1.0 and 1.0+$eps canbe distinguished.
53       In Tcl, which uses double precision  floating-point  numbers,  this  is
54       typically 1.1e-16.
55

PROCEDURES

57       Effectively the package provides the following procedures:
58
59       ::math::fuzzy::teq value1 value2
60              Compares  two floating-point numbers and returns 1 if their val‐
61              ues fall within a small range. Otherwise it returns 0.
62
63       ::math::fuzzy::tne value1 value2
64              Returns the negation, that is, if the difference is larger  than
65              the margin, it returns 1.
66
67       ::math::fuzzy::tge value1 value2
68              Compares  two floating-point numbers and returns 1 if their val‐
69              ues either fall within a small range or if the first  number  is
70              larger than the second. Otherwise it returns 0.
71
72       ::math::fuzzy::tle value1 value2
73              Returns  1 if the two numbers are equal according to [teq] or if
74              the first is smaller than the second.
75
76       ::math::fuzzy::tlt value1 value2
77              Returns the opposite of [tge].
78
79       ::math::fuzzy::tgt value1 value2
80              Returns the opposite of [tle].
81
82       ::math::fuzzy::tfloor value
83              Returns the integer number that is lower or equal to  the  given
84              floating-point number, within a well-defined tolerance.
85
86       ::math::fuzzy::tceil value
87              Returns the integer number that is greater or equal to the given
88              floating-point number, within a well-defined tolerance.
89
90       ::math::fuzzy::tround value
91              Rounds the floating-point number off.
92
93       ::math::fuzzy::troundn value ndigits
94              Rounds the floating-point number off to the specified number  of
95              decimals (Pro memorie).
96
97       Usage:
98
99       if { [teq $x $y] } { puts "x == y" }
100       if { [tne $x $y] } { puts "x != y" }
101       if { [tge $x $y] } { puts "x >= y" }
102       if { [tgt $x $y] } { puts "x > y" }
103       if { [tlt $x $y] } { puts "x < y" }
104       if { [tle $x $y] } { puts "x <= y" }
105
106       set fx      [tfloor $x]
107       set fc      [tceil  $x]
108       set rounded [tround $x]
109       set roundn  [troundn $x $nodigits]
110
111

TEST CASES

113       The problems that can occur with floating-point numbers are illustrated
114       by the test cases in the file "fuzzy.test":
115
116       ·      Several test case use the ordinary comparisons,  and  they  fail
117              invariably to produce understandable results
118
119       ·      One  test  case  uses  [expr]  without  braces ({ and }). It too
120              fails.
121
122       The conclusion from this is that any expression should be surrounded by
123       braces,  because  otherwise  very awkward things can happen if you need
124       accuracy. Furthermore, accuracy and understandable results are enhanced
125       by using these "tolerant" or fuzzy comparisons.
126
127       Note  that  besides  the Tcl-only package, there is also a C-based ver‐
128       sion.
129

REFERENCES

131       Original implementation in Fortran by dr. H.D. Knoble (Penn State  Uni‐
132       versity).
133
134       P.  E.  Hagerty,  "More  on  Fuzzy  Floor  and Ceiling," APL QUOTE QUAD
135       8(4):20-24, June 1978. Note that TFLOOR=FL5 took five years of refereed
136       evolution (publication).
137
138       L.  M. Breed, "Definitions for Fuzzy Floor and Ceiling", APL QUOTE QUAD
139       8(3):16-23, March 1978.
140
141       D. Knuth, Art of Computer Programming, Vol. 1, Problem 1.2.4-5.
142

BUGS, IDEAS, FEEDBACK

144       This document, and the package it describes, will  undoubtedly  contain
145       bugs  and  other  problems.  Please report such in the category math ::
146       fuzzy     of     the     Tcllib     SF     Trackers     [http://source
147       forge.net/tracker/?group_id=12883].   Please  also report any ideas for
148       enhancements you may have for either package and/or documentation.
149

KEYWORDS

151       floating-point, math, rounding
152
153
154
155math                                  0.2                       math::fuzzy(n)
Impressum