Scott Hanselman

Why you can't Double.Parse(Double.MaxValue.ToString()) or System.OverloadExceptions when using Double.Parse

January 17, '05 Comments [4] Posted in Web Services | XML
Sponsored By

This was an interesting one. A fellow here was returning double.MaxValue in an XML element and was seeing:

System.InvalidOperationException : There is an error in XML document (1, 539).----> System.OverflowException : Value was either too large or too small for a Double.

It seems that while he was sending out Double.MaxValue the system was unable to use Double.Parse() on it. In other words, he wasn't able to "roundtrip" his value, like Double.Parse(Double.MaxValue.ToString()).

The problem stems from there being no 1:1 representation between decimal and floating point numbers. For example, how do you express 2/3? Do you say 0.6666666667? You've rounded up. You can't say 0.66666 with that little bar on the top. So, you either have to round up (creating data) or limit the digits (losing data). 

//This will throw a System.OverflowException…
double d1 = double.Parse( "1.79769313486232E+308" );

Change the last digit:

double d2 = double.Parse( "1.79769313486231E+308" );
//This will work.

A few interesting articles I found referenced in Google Groups:

INFO: Precision and Accuracy in Floating-Point Calculations: Q125056
INFO: IEEE Floating-Point Representation and MS Languages: Q36068

Semi-Conclusion

Using Edge Cases as Magic Numbers is more evil than using Magic Numbers

Quasi-Conclusion

However, you can use Double.MaxValue.ToString("R") to prefer roundtrips over precision. From the MSDN Help:

The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single. Although a precision specifier can be appended to the round-trip format specifier, it is ignored. Round trips are given precedence over precision when using this specifier. This format is supported by floating-point types only.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Another dasBlog Slashdot, this time Greg Hughes' CopsOnTop Charity...audioblogging in Kilimanjaro

January 17, '05 Comments [4] Posted in DasBlog | Diabetes
Sponsored By

Any moment now, the blog that Greg Hughes setup for CopsOnTop is going to get Slashdotted. I was chatting with Greg, and he was telling me how cool it is that the cops are climbing Kilimanjaro as a memorial to a fallen officer. They are blogging via Satellite Telephone to AudioBlog.com and the posts are being posted to their Climbing Blog (running dasBlog 1.6). I was just submitting the story to get him some traffic. I didn't think it'd be accepted within 4 minutes of posting it! Greg didn't have time to upgrade to dasBlog 1.7 (RC this week) so we'll see what happens.

GlucoPilot writes "Apparently a charity called Cops On Top" is climbing Kilimanjaro as a memorial to the late Officer Isaac Espinoza of the SFPD. That in itself is pretty amazing, but the ./ amazing thing is that they are AudioBlogging the whole thing via Satellite Phone by calling to an audioblogging site and having their MP3s automatically posted to their dasBlog (open source blogging system) at http://www.copsontop.com/climblog. Cool stuff. Their latest blog post is at 15,500 ft."

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Vote on this Whidbey Bug: How to create a Tray Icon (NotifyIcon) with WinForms 2.0

January 16, '05 Comments [9] Posted in Programming
Sponsored By

Whatisplaying1

While Tray Icons (and applications that use ONLY TrayIcons as their UI) are a major part of our lives, there's limited support in WinForms (2.0) for them without resorting to P/Invoke and vestigial MainForms who only call this.Hide().

NotifyIcon should be extended to support BalloonHelp. Hans Blomme's now-famous extended version of NotifyIcon is the canonical example. It still works under 2.0, but the 2.0 addition of the ContextMenuStrip makes it a little tricky. His free stuff could be integrated into the 2.0 NotifyIcon easily, adding a powerful (and missing) feature to 2.0 WinForms. Additionally, there should be a way for a Form to startup hidden to avoid the flash of the form before it calls this.Hide(), deferring to it's NotifyIcon.

Help us all out and vote to get this omission fixed in Whidbey.

UPDATE: Some Reflectoring has confirmed that has in fact been fixed in the December CTP! All is as it should be. Thanks to Chris Sells and Scott Swigart.

Now playing: Kanye West, Twista & Jamie Foxx - Slow Jamz

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

I'm digging Skype more and more, the I'm tired of all the different "Me" issues. Also, moving your Skype contacts.

January 13, '05 Comments [8] Posted in Diabetes | XML
Sponsored By

skype1.jpgI'm digging Skype more and more. I'm GlucoPilot (note the custom URL handlers). The new Skype 1.1, while it apparently has some installer issues (I haven't faced) is a great improvement over 1.0.

The best new addition is conference calling. It seems to only be as good as the worst connection in the conference. I spoke with Omar in California and Adam in Brazil (this week), and it seemed a little dodgy due to Adam's connection.

Skype is great but I have to problems. One is simple, the other is unforgivable.

1. It's yet another identity. As far as I'm concerned my email address is the closest thing to "who" I am online that I can get. I'd rather not have ANOTHER 'nick.' But, I can live with this.

2. The real issue is that I have three computers and (unless I'm nuts) Skype doesn't allow storing your Skype Contacts remotely and don't even have an export function.

This is nuts. But, I figured I could look in Applicationdata\skype, etc and manually merge XML files, right?

GASP. This is the first time in recent memory that I looked at a settings file for a NEW application (especially a 'viral marketing' one like Skype) and didn't find XML config underlying. Skype uses a binary format to store your contacts list locally. Frankly - SHOCKING.

However, someone pointed me to www.jyve.com which purports to offer another abstraction layer on top of Skype and manage contacts. We shall see.

For now, there's a lot of Me's out there.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.