1math::fuzzy(n) Tcl Math Library math::fuzzy(n)
2
3
4
5______________________________________________________________________________
6
8 math::fuzzy - Fuzzy comparison of floating-point numbers
9
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
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
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). Usage:
96
97 if { [teq $x $y] } { puts "x == y" }
98 if { [tne $x $y] } { puts "x != y" }
99 if { [tge $x $y] } { puts "x >= y" }
100 if { [tgt $x $y] } { puts "x > y" }
101 if { [tlt $x $y] } { puts "x < y" }
102 if { [tle $x $y] } { puts "x <= y" }
103
104 set fx [tfloor $x]
105 set fc [tceil $x]
106 set rounded [tround $x]
107 set roundn [troundn $x $nodigits]
108
109
111 The problems that can occur with floating-point numbers are illustrated
112 by the test cases in the file "fuzzy.test":
113
114 · Several test case use the ordinary comparisons, and they fail
115 invariably to produce understandable results
116
117 · One test case uses [expr] without braces ({ and }). It too
118 fails. The conclusion from this is that any expression should
119 be surrounded by braces, because otherwise very awkward things
120 can happen if you need accuracy. Furthermore, accuracy and
121 understandable results are enhanced by using these "tolerant" or
122 fuzzy comparisons.
123
124 Note that besides the Tcl-only package, there is also a C-based ver‐
125 sion.
126
128 Original implementation in Fortran by dr. H.D. Knoble (Penn State Uni‐
129 versity).
130
131 P. E. Hagerty, "More on Fuzzy Floor and Ceiling," APL QUOTE QUAD
132 8(4):20-24, June 1978. Note that TFLOOR=FL5 took five years of refereed
133 evolution (publication).
134
135 L. M. Breed, "Definitions for Fuzzy Floor and Ceiling", APL QUOTE QUAD
136 8(3):16-23, March 1978.
137
138 D. Knuth, Art of Computer Programming, Vol. 1, Problem 1.2.4-5.
139
141 floating-point, math, rounding
142
143
144
145math 0.2 math::fuzzy(n)