File: fuzzy.test

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (387 lines) | stat: -rw-r--r-- 10,616 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- tcl -*-
# fuzzy.test --
#
#    Test suite for the math::fuzzy procs of tolerant comparisons
#    (Tcl-only version)
#
#    version 0.2: improved and extended implementation, march 2002
#    version 0.2.1: added test for bug #2933130, january 2010

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 1.0

support {
    useLocal math.tcl math
}
testing {
    useLocal fuzzy.tcl math::fuzzy
}

# -------------------------------------------------------------------------

namespace import ::math::fuzzy::*

# -------------------------------------------------------------------------

#
# Test: tolerance has sane value
#
#test math-fuzzy-Tolerance-1.0 {Tolerance has acceptable value} {
#   expr {(1.0+0.5*$::math::fuzzy::eps3) != 1.0}
#} 1
#test math-fuzzy-Tolerance-1.1 {Tolerance has acceptable value} {
#   expr {(1.0-0.5*$::math::fuzzy::eps3) != 1.0}
#} 1

test math-fuzzy-Tolerance-1.0 {Tolerance has acceptable value} {
   expr {(1.0+0.5*$::math::fuzzy::eps3) != 1.0}
} 1

test math-fuzzy-Tolerance-1.1 {Tolerance has acceptable value} {
   expr {(1.0-0.5*$::math::fuzzy::eps3) != 1.0}
} 1

#
# Note: Equal-1.* and NotEqual-1.* are complementary
#       GrEqual-1.* and Lower-1.* ditto
#       GrThan-1.* and LoEqual-1.* ditto
#

test math-fuzzy-Equal-1.0 {Compare two floats and see if they are equal} {
   teq 1.0 1.001
} 0
test math-fuzzy-Equal-1.1 {Compare two floats and see if they are equal} {
   teq 1.0 1.0001
} 0
test math-fuzzy-Equal-1.2 {Compare two floats and see if they are equal} {
   teq 1.0 1.00000000000000001
} 1
test math-fuzzy-Equal-1.3 {Compare two floats and see if they are equal} {
   teq 1.0 1.000000000000001
} 0

test math-fuzzy-NotEqual-1.0 {Compare two floats and see if they differ} {
   tne 1.0 1.001
} 1
test math-fuzzy-NotEqual-1.1 {Compare two floats and see if they differ} {
   tne 1.0 1.0001
} 1
test math-fuzzy-NotEqual-1.2 {Compare two floats and see if they differ} {
   tne 1.0 1.00000000000000001
} 0
test math-fuzzy-NotEqual-1.3 {Compare two floats and see if they differ} {
   tne 1.0 1.000000000000001
} 1

test math-fuzzy-GrEqual-1.0 {Compare two floats - check greater/equal} {
   tge 1.0 1.001
} 0
test math-fuzzy-GrEqual-1.1 {Compare two floats - check greater/equal} {
   tge 1.0 1.0001
} 0
test math-fuzzy-GrEqual-1.2 {Compare two floats - check greater/equal} {
   tge 1.0 1.00000000000000001
} 1
test math-fuzzy-GrEqual-1.3 {Compare two floats - check greater/equal} {
   tge 1.0 1.000000000000001
} 0

test math-fuzzy-Lower-1.0 {Compare two floats - check lower} {
   tlt 1.0 1.001
} 1
test math-fuzzy-Lower-1.1 {Compare two floats - check lower} {
   tlt 1.0 1.0001
} 1
test math-fuzzy-Lower-1.2 {Compare two floats - check lower} {
   tlt 1.0 1.00000000000000001
} 0
test math-fuzzy-Lower-1.3 {Compare two floats - check lower} {
   tlt 1.0 1.000000000000001
} 1
test math-fuzzy-Lower-1.4 {Compare two floats - check lower} {
   # They can not both be true
   expr {[tlt 1.1 1.0] && [tlt 1.0 1.1]}
} 0

test math-fuzzy-LoEqual-1.0 {Compare two floats - check lower/equal} {
   tle 1.0 1.001
} 1
test math-fuzzy-LoEqual-1.1 {Compare two floats - check lower/equal} {
   tle 1.0 1.0001
} 1
test math-fuzzy-LoEqual-1.2 {Compare two floats - check lower/equal} {
   tle 1.0 1.00000000000000001
} 1
test math-fuzzy-LoEqual-1.3 {Compare two floats - check lower/equal} {
   tle 1.0 1.000000000000001
} 1

test math-fuzzy-Greater-1.0 {Compare two floats - check greater} {
   tgt 1.0 1.001
} 0
test math-fuzzy-Greater-1.1 {Compare two floats - check greater} {
   tgt 1.0 1.0001
} 0
test math-fuzzy-Greater-1.2 {Compare two floats - check greater} {
   tgt 1.0 1.00000000000000001
} 0
test math-fuzzy-Greater-1.3 {Compare two floats - check greater} {
   tgt 1.0 1.000000000000001
} 0

#
# Note: there is no possibility to print the results of the
# naive comparison or floor/ceil?
#
# Note: no attention paid to tcl_precision!
#
test math-fuzzy-ManyCompares-1.0 {Compare results of calculations} {
   set tol_eq 0
   set tol_ne 0
   set tol_ge 0
   set tol_gt 0
   set tol_le 0
   set tol_lt 0

   for { set i -1000 } { $i <= 1000 } { incr i } {
      if { $i == 0 } continue

      set x [expr {1.01/double($i)}]
      set y [expr {(2.1*$x)*(double($i)/2.1)}]

      if { [teq $y 1.01] } { incr tol_eq }
      if { [tne $y 1.01] } { incr tol_ne }
      if { [tge $y 1.01] } { incr tol_ge }
      if { [tgt $y 1.01] } { incr tol_gt }
      if { [tle $y 1.01] } { incr tol_le }
      if { [tlt $y 1.01] } { incr tol_lt }
   }
   set result [list $tol_eq $tol_ne $tol_ge $tol_gt $tol_le $tol_lt]
} {2000 0 2000 0 2000 0}

test math-fuzzy-ManyCompares-1.1 {Compare fails due to missing braces at reduced precision} {
   set tol_eq 0
   set tol_ne 0
   set tol_ge 0
   set tol_gt 0
   set tol_le 0
   set tol_lt 0

   #
   # Force Tcl8.4 or earlier behaviour in expanding numbers
   # Requires tcl_precision of 12!
   #
   set prec $::tcl_precision
   set ::tcl_precision 12

   for { set i -1000 } { $i <= 1000 } { incr i } {
      if { $i == 0 } continue

      #
      # NOTE: The braces in the assignment for y are missing on purpose!
      #
      set x [expr {1.01/double($i)}]
      set y [expr (2.1*$x)*(double($i)/2.1)]

      if { [teq $y 1.01] } { incr tol_eq }
      if { [tne $y 1.01] } { incr tol_ne }
      if { [tge $y 1.01] } { incr tol_ge }
      if { [tgt $y 1.01] } { incr tol_gt }
      if { [tle $y 1.01] } { incr tol_le }
      if { [tlt $y 1.01] } { incr tol_lt }
   }
   set result [list $tol_eq $tol_ne $tol_ge $tol_gt $tol_le $tol_lt]
   set intended {2000 0 2000 0 2000 0}
   set equal 1
   foreach r $result i $intended {
      if { $r != $i } {
         set equal 0
      }
   }
   set tcl_precision $prec
   set equal
} 0

test math-fuzzy-ManyCompares-1.2 {Compare does not fail even with missing braces because of sufficient precision} {
   set tol_eq 0
   set tol_ne 0
   set tol_ge 0
   set tol_gt 0
   set tol_le 0
   set tol_lt 0

   #
   # Force sufficient precision if Tcl8.4 or earlier
   #
   set prec $::tcl_precision
   if {![package vsatisfies [package provide Tcl] 8.5]} {
       set ::tcl_precision 17
   } else {
       set ::tcl_precision 0
   }

   for { set i -1000 } { $i <= 1000 } { incr i } {
      if { $i == 0 } continue

      #
      # NOTE: The braces in the assignment for y are missing on purpose!
      #
      set x [expr {1.01/double($i)}]
      set y [expr (2.1*$x)*(double($i)/2.1)]

      if { [teq $y 1.01] } { incr tol_eq }
      if { [tne $y 1.01] } { incr tol_ne }
      if { [tge $y 1.01] } { incr tol_ge }
      if { [tgt $y 1.01] } { incr tol_gt }
      if { [tle $y 1.01] } { incr tol_le }
      if { [tlt $y 1.01] } { incr tol_lt }
   }
   set result [list $tol_eq $tol_ne $tol_ge $tol_gt $tol_le $tol_lt]
   set intended {2000 0 2000 0 2000 0}
   set equal 1
   foreach r $result i $intended {
      if { $r != $i } {
         set equal 0
      }
   }
   set tcl_precision $prec
   set equal
} 1

test math-fuzzy-ManyCompares-1.3 {Compare fails due to naive comparison} {
   set naiv_eq 0
   set naiv_ne 0
   set naiv_ge 0
   set naiv_gt 0
   set naiv_le 0
   set naiv_lt 0

   for { set i -1000 } { $i <= 1000 } { incr i } {
      if { $i == 0 } continue

      set x [expr {1.01/double($i)}]
      set y [expr {(2.1*$x)*(double($i)/2.1)}]

      if { $y == 1.01 } { incr naiv_eq }
      if { $y != 1.01 } { incr naiv_ne }
      if { $y >= 1.01 } { incr naiv_ge }
      if { $y >  1.01 } { incr naiv_gt }
      if { $y <= 1.01 } { incr naiv_le }
      if { $y <  1.01 } { incr naiv_lt }
   }
   set result [list $naiv_eq $naiv_ne $naiv_ge $naiv_gt $naiv_le $naiv_lt]
   set intended {2000 0 2000 0 2000 0}
   set equal 1
   foreach r $result i $intended {
      if { $r != $i } {
         set equal 0
      }
   }
   set equal
} 0

test math-fuzzy-Floor-Ceil-1.0 {Check floor and ceil functions} {
   set fc_eq 0
   set fz_eq 0
   set fz_ne 0

   for { set i -1000 } { $i <= 1000 } { incr i } {

      set x [expr {0.11*double($i)}]
      set y [expr {(($x*11.0)-$x)-double($i)/10.0}]
      set z [expr {double($i)}]

      if { [tfloor $y] == $z }         { incr fz_eq }
      if { [tfloor $y] == [tceil $y] } { incr fc_eq }
   }
   set result [list $fc_eq $fz_eq]
} {2001 2001}

test math-fuzzy-Floor-Ceil-1.1 {Naive floor and ceil fail} {
   set fc_eq 0
   set fz_eq 0
   set fz_ne 0

   for { set i -1000 } { $i <= 1000 } { incr i } {

      set x [expr {0.11*double($i)}]
      set y [expr {(($x*11.0)-$x)-double($i)/10.0}]
      set z [expr {double($i)}]

      if { [expr {floor($y)}]  == $z } { incr fz_eq }
      if { [expr {floor($y)}] == [expr {ceil($y)}] } { incr fc_eq }
   }
   set result [list $fc_eq $fz_eq]
   set intended {2001 2001}
   set equal 1
   foreach r $result i $intended {
      if { $r != $i } {
         set equal 0
      }
   }
   set equal
} 0

test math-fuzzy-Roundoff-1.0 {Rounding off numbers} {

   set result {}
   foreach x {
       0.1  0.3  0.4999999  0.5000001  0.99999
       -0.1 -0.3 -0.4999999 -0.5000001 -0.99999
   } {
      lappend result [tround $x]
   }
   set result
} {0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 -1.0 -1.0}

test math-fuzzy-Roundoff-1.1 {Rounding off numbers naively - may fail} {
   set result {}
   foreach x {
       0.1  0.3  0.4999999  0.5000001  0.99999
       -0.1 -0.3 -0.4999999 -0.5000001 -0.99999
   } {
      lappend result [expr {floor($x+0.5)}]
   }
   set result
} {0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 -1.0 -1.0}

test math-fuzzy-Roundoff-2.1 {Rounding off numbers with one digit} {
   set result {}
   foreach x {
       0.11  0.32  0.4999999  0.5000001  0.99999
       -0.11 -0.32 -0.4999999 -0.5000001 -0.99999
   } {
      lappend result [troundn $x 1]
   }
   set result
} {0.1 0.3 0.5 0.5 1.0 -0.1 -0.3 -0.5 -0.5 -1.0}

test math-fuzzy-Roundoff-2.2 {Rounding off numbers with two digits} {
   set result {}
   foreach x {
       0.11  0.32  0.4999999  0.5000001  0.99999
       -0.11 -0.32 -0.4999999 -0.5000001 -0.99999
   } {
      lappend result [troundn $x 2]
   }
   set result
} {0.11 0.32 0.5 0.5 1.0 -0.11 -0.32 -0.5 -0.5 -1.0}

test math-fuzzy-Roundoff-2.3 {Rounding off numbers with three digits} {
   set result {}
   foreach x {
       0.1115  0.3210  0.4909999  0.5123401  0.99999
       -0.1115 -0.3210 -0.4909999 -0.5123401 -0.99999
   } {
      lappend result [troundn $x 3]
   }
   set result
} {0.112 0.321 0.491 0.512 1.0 -0.111 -0.321 -0.491 -0.512 -1.0}
#
# Hm, here we have a discrepancy: 0.112 and -0.111!