|
VB 6 Helper Newsletter
|
Rod Stephens
|
Oct 06, 2008 13:20 PDT
|
As promised, the followup article to my introduction to the Task
Parallel Library posted on DevX this week. Again, I encourage you to
give it a look. These sorts of parallel applications are very likely to
play an important role in programming over the next several years.
DevX: Getting Started with the .NET Task Parallel Library:
Multi-Core Case Studies
September 29, 2008
There's more to any programming tool than just theory-and the TPL is
no exception. Learn about the techniques Rod Stephens used and the
problems he encountered in these parallelism case studies.
This article discusses the problems I encountered and the results I
got when converting several applications to use the Task Parallel
Library. The programs are:
* Solving the 2D Packing Problem
* Four Heuristic Solutions to the Traveling Salesperson Problem
* Multi-Core Mandelbrot Sets
* Image Filtering
* Making Delicate Decisions (the partition problem)
----------
A couple of interesting articles/announcements:
Silverlight 2 Release Candidate Now Available
http://weblogs.asp.net/scottgu/archive/2008/09/25/silverlight-2-release-candidate-now-available.aspx
This one is related to my recent DevX articles about parallel
computation.
Getting more from Moore's Law
http://news.bbc.co.uk/2/hi/technology/7080772.stm
The following two are related to previous examples that generate CAPTCHA
images.
Microsoft’s CAPTCHA successfully broken
http://blogs.zdnet.com/security/?p=1232&tag=rbxccnbzd1
Spammers attacking Microsoft’s CAPTCHA — again
http://blogs.zdnet.com/security/?p=1986&tag=nl.e550
I wonder why no one uses 10 or 20 thousand pictures of random things and
makes you type in the name of whatever is in the picture: dog, ball,
sailboat, donut, Queen of England, etc. (or do you know of anyone who
does that?) I doubt the hackers will be up to recognizing the objects in
a picture for a while yet.
----------
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
----------
*** Now Available ***
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
Both Contents:
1. Updated HowTo: Round a value to a specified number of digits
==========
++++++++++
<Both>
++++++++++
==========
1. Updated HowTo: Round a value to a specified number of digits
http://www.vb-helper.com/howto_round_to_specified_digits.html
http://www.vb-helper.com/HowTo/howto_round_to_specified_digits.zip
This example contains a lot of discussion about rounding, "banker's
rounding," and errors that can arise when rounding.
Ben McCormack discovered a very interesting feature of VBA's Round
function. The following shows two queries in the Immediate window and
their results:
? Round(134.425,2)
134.43
? Round (1.425,2)
1.42
The first rounds 134.425 incorrectly towards an odd digit while the
second correctly rounds 1.425 to an even digit.
Ben's requirement was to reverse-engineer an application that showed
this behavior so this inconsistency was quite a problem. Eventually he
found a Microsoft Knowledge Base article (How To Implement Custom
Rounding Procedures) that seems to imply that VBA's Round function uses
code similar to this:
Function BRound(ByVal X As Double, _
Optional ByVal Factor As Double = 1) As Double
' For smaller numbers:
' BRound = CLng(X * Factor) / Factor
Dim Temp As Double, FixTemp As Double
Temp = X * Factor
FixTemp = Fix(Temp + 0.5 * Sgn(X))
' Handle rounding of .5 in a special manner
If Temp - Int(Temp) = 0.5 Then
If FixTemp / 2 <> Int(FixTemp / 2) Then ' Is Temp odd
' Reduce Magnitude by 1 to make even
FixTemp = FixTemp - Sgn(X)
End If
End If
BRound = FixTemp / Factor
End Function
This code seems to produce the same result as VBA.Round.
It seems that the problem is caused by rounding errors. You cannot store
all decimal numbers with a binary representation without some errors.
You could use an infinite-precision rational representation (where you
track numerators and demoninators) but that would be much less
efficient.
It seems that this example is bumping up against binary representation
errors. I suspect you could fiddle with the algorithm a bit to fix this
particular problem but you'll probably have others for certain values.
You can get better results if you keep an eye on your precision. For
example, instead of asking whether the final digit is exactly 5, ask
whether it is very close to 5 (i.e. 0.05000000001 is close enough to
0.05).
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
http://www.vb-helper.com/cgi-bin/mojo/mojo.cgi?flavor=archive&list=VB_Helper_Newsletter
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|