From: Ralf Fassel on 5 Sep 2009 14:39 * solar <solaradmin2009(a)gmail.com> | --> if {[expr $a-int($a)] < 0.00001} { incr i } | works for atleast N upto 15000, but its extremely slow going. :-( Try if { ($a-int($a)) < 0.00001} { incr i } should be a tick faster. proc foo {a} {set i 0 ; if {[expr $a-int($a)] < 0.00001} { incr i }} time {foo 1} 100000 14.36562 microseconds per iteration proc foo {a} {set i 0 ; if {($a-int($a)) < 0.00001} { incr i }} time {foo 1} 100000 1.88318 microseconds per iteration R'
From: Robert Heller on 5 Sep 2009 16:09 At Sat, 05 Sep 2009 08:11:48 -0700 Gerry Snyder <mesmerizerfan(a)gmail.com> wrote: > > Robert Heller wrote: > > At Sat, 5 Sep 2009 01:43:14 -0700 (PDT) Donald Arseneau <asnd(a)triumf.ca> wrote: > > > >> > >> Do you think integers are not a subset of fp numbers for > >> some reason? > > > > Sure they are. It is just not always the case that a fp math library > > function will in fact return an integer result. I.e. the match function > > is not return 101, it is returning 100.9999999999999 or something like > > that. Assuming that although *mathematically* the result should be an > > integer result does not mean that the math library will in fact return > > the exact integer result. As soon as you start doing arithmetic on a > > computer *using finite precision floating point*, you start picking up > > errors and over time (number of calculations) you will pickup errors due > > to guard bit jitter and round off, etc. One should never assume > > mathenetically exact results and almost *never* should use an equality > > test, even when mathematically called for. > > Errr,... > > A math library that says 15 / 3 = 4.999999 or sqrt(10201) = 100.9999999 > has an error that should be fixed. You are assuming that the inputs are 15 and 3 (or 10201). If one does a long string of fp calculations, it is quite possible to end up with: 14.999997 / 3 = 4.999999 or sqrt(10200.9999798000) = 100.9999999 % set x 14.999997 14.999997 % set y 3 3 % expr {$x / $y} 4.999999 % format %.0f $x 15 > > > Gerry > -- Robert Heller -- 978-544-6933 Deepwoods Software -- Download the Model Railroad System http://www.deepsoft.com/ -- Binaries for Linux and MS-Windows heller(a)deepsoft.com -- http://www.deepsoft.com/ModelRailroadSystem/
From: Kevin Kenny on 6 Sep 2009 00:08 Kevin Kenny wrote: > > It appears that the MSVC library generates a result with an error > of 1 unit in the least significant place for hypot(99.0, 20.0). > There is very little that Tcl can do about bugs in the underlying > C library. And following up on this, it appears that the na�ve approach of [expr {sqrt($a*$a + $b*$b)}] works better than [expr {hypot($a,$b)}] for this case. (The chief advantage of hypot() is that it at least supposedly does not overflow even if $a**2 or $b**2 does.) -- 73 de ke9tv/2, Kevin
From: solar on 6 Sep 2009 00:35 On Sep 5, 11:39 pm, Ralf Fassel <ralf...(a)gmx.de> wrote: > * solar <solaradmin2...(a)gmail.com> > | --> if {[expr $a-int($a)] < 0.00001} { incr i } > | works for atleast N upto 15000, but its extremely slow going. :-( > > Try > > if { ($a-int($a)) < 0.00001} { incr i } > > should be a tick faster. > > proc foo {a} {set i 0 ; if {[expr $a-int($a)] < 0.00001} { incr i }} > time {foo 1} 100000 > 14.36562 microseconds per iteration > > proc foo {a} {set i 0 ; if {($a-int($a)) < 0.00001} { incr i }} > time {foo 1} 100000 > 1.88318 microseconds per iteration > > R' Thanks Ralf, Speaking of improving speed, I thought that maybe using Tcl 8.5 would help. But the "very original pythag" above takes about 2.3 times longer to execute on 8.5.2. --------------------------------------- % info pa 8.4.18 % time {pythag 1000} 12705706 microseconds per iteration % time {pythag 1000} 2 12709078.5 microseconds per iteration % info pa 8.5.2 % time {pythag 1000} 29223915 microseconds per iteration % time {pythag 1000} 2 29243033.5 microseconds per iteration --------------------------------------- Now why would this be so? Even with your tip trying --------------------------------------- proc foo1 {a} {set i 0 ; if {($a-int($a)) < 0.00001} { incr i }} proc foo2 {a} {set i 0 ; if {[ expr $a-int($a)] < 0.00001} { incr i }} % info pa 8.4.18 % time {foo1 1} 100000 1.08594 microseconds per iteration % time {foo2 1} 100000 5.24368 microseconds per iteration ~~~~~~~~~~~ % info pa 8.5.2 % time {foo1 1} 100000 1.37046 microseconds per iteration % time {foo2 1} 100000 6.48785 microseconds per iteration --------------------------------------- Your tip would help speed it up a little bit though. Thanks, --
From: Larry W. Virden on 6 Sep 2009 02:35
On Sep 6, 12:35 am, solar <solaradmin2...(a)gmail.com> wrote: > > % info pa > 8.4.18 > % time {foo1 1} 100000 > 1.08594 microseconds per iteration > % time {foo2 1} 100000 > 5.24368 microseconds per iteration > ~~~~~~~~~~~ > % info pa > 8.5.2 > % time {foo1 1} 100000 > 1.37046 microseconds per iteration > % time {foo2 1} 100000 > 6.48785 microseconds per iteration > --------------------------------------- I tried the scripts above on Tcl 8.5.6 and 8.6b1.1 on SPARC Solaris8. My times were significantly worse than what you saw: /var/tmp/Ac*6/bin/tclsh8.5 /var/tmp/timing.tcl 8.5.6 34.62197 microseconds per iteration 165.51024 microseconds per iteration 37.19967 microseconds per iteration tclsh8.6 /var/tmp/timing.tcl 8.6b1.1 52.31166 microseconds per iteration 202.41822 microseconds per iteration 57.00032 microseconds per iteration Note that the third timing is for: proc foo3 {a} {set i 0 ; if {[ expr {$a-int($a) < 0.00001}]} { incr i }} |