Goto Blog Home PageRevell Research Systems: Alastair Revell's Web Log
On this page....
Archive
<February 2020>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
1234567

RSS 2.0     Atom 1.0     CDF

Summary
Search
Navigation
Categories
Blog Roll
About Alastair Revell
Alastair Revell is the Managing Consultant of Revell Research Systems, a Management and Technology Consulting Practice based at Exeter in the United Kingdom.
Contact Alastair Revell
 EMail Revell Research Systems Limited Email Me
Copyright
Legal & Other Notices
Sign In
Disclaimer
The material published in this web log is for general purposes only. It does not constitute nor is it intended to represent professional advice. You should always seek specific professional advice in relation to particular issues. The information in this web log is provided "as is" with no warranties and confers no rights. The opinions expressed herein are my own personal opinions.

Web Log Home | Welcome to this Web Log | Using this Web Log | New to Blogs? | About Revell Research Systems | Contact Details

Review Entries for Day Tuesday, October 31, 2006

We've recently started porting our large .NET class libraries from .NET 1.1 to .NET 2.0, which has to date been a fairly smooth process. We've also been revising and enhancing classes as we go and I stumbled across a rather nasty catch during this process.

I was working on a numerical class library that represents trigonometric angles, which is heavily dependent on the Double type (as you might expect).

It took me quite a while to track down the cause of some strange results I was getting from our code. The essence of the problem was similar to the code below:- 

Dim result As Double = Double.MaxValue * 3/10

(Obviously, the situation was a little more complex!)

What quantity would you expect result to hold? Would you be surprised to learn that it was PositiveInfinity? I was initially...

A quick scan of the line suggests that result will hold something well below the maximum value allowed by the Double type. However, the VB.NET compiler sticks to the normal rules of operator precedence and finds itself with a value three times greater than the maximum allowable value (after multiplying by 3), which it concludes is PositiveInfinity. It then divides this by 10, which nonetheless still evaluates to PositiveInfinity...

Now consider:-

Dim value As Double
Dim result As Double

value = Double.MaxValue
result = value * 3 / 10

What quantity would you expect result to hold? Would you be surprised to learn that it was NOT PositiveInfinity but the correct mathematical value?

Obviously, the VB.NET compiler evaluates differently if the right-hand side of an expression is a combination of constants and literals rather than based on a variable.

More about Alastair Revell

Tuesday, October 31, 2006 5:52:39 PM (GMT Standard Time, UTC+00:00)  #
Comments [0] .NET | Coding | Trackback

RSS 2.0 Feed If you enjoyed reading an article on this blog, why not subscribe to the RSS 2.0 feed to receive future articles?
   
Revell Research Systems Logo Visit the Revell Research Systems Web Site if you want to learn more about this management and technology consulting practice.