Wrox Programmer Forums
|
BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C#
This is the forum to discuss the Wrox book MCSD Certification Toolkit (Exam 70-483): Programming in C# by Tiberiu Covaci, Rod Stephens, Vincent Varallo, Gerry O'Brien; ISBN: 978-1-118-61209-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: MCSD Certification Toolkit (Exam 70-483): Programming in C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 10th, 2013, 09:21 AM
Registered User
 
Join Date: May 2013
Posts: 6
Thanks: 0
Thanked 1 Time in 1 Post
Default widening and precision

Provide an example of a widening conversion that does not result in loss of magnitude but would result in loss of precision. Thanks!
 
Old December 17th, 2013, 05:51 AM
Registered User
 
Join Date: Dec 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am also intrested in such an example.

The book says (p. 157): "Widening conversions always succeed and a cast is optional. Magnitude is never lost but precision may be."

The type conversion from decimal to double is magnitude safe but not precision safe. So this could be a candidate example, but the problem is that the cast is NOT optional. So according to the citation above it is not widening.

Would appreciate some help with this, thanks.
 
Old December 17th, 2013, 11:02 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Yes it's a bit inconsistent, but it's taken pretty much directly from the Microsoft documentation and I wanted to be sure everyone picked up on it in case it appeared on the exam.

The decimal --> double conversion may not be a great example because you're right a cast is required.

Personally, if I had been on the Microsoft team, I would have argued that loss of precision makes a narrowing conversion. Saying it's not is like saying, "Casting an Employee into a Person is a widening conversion. Sure you lose the EmployeeId but you keep the name."

From Type Conversion in the .NET Framework:

Quote:
Widening conversions cannot result in data loss (although they may result in a loss of precision). Because data cannot be lost, compilers can handle the conversion implicitly or transparently, without requiring the use of an explicit conversion method or a casting operator.
And on Type Conversion Tables there's a table that lists decimal --> double and decimal --> double as widening conversions.

So the documentation is a bit inconsistent. Microsoft regards those conversions as widening but the compiler requires a cast.

Here's an example that doesn't require a cast:

Code:
long longValue = 1234567890;
float floatValue = longValue;

Console.WriteLine(string.Format("{0,15:N0}", longValue));
Console.WriteLine(string.Format("{0,15:N0}", floatValue));
The result is:

Code:
  1,234,567,890
  1,234,568,000
I think as long as you understand the issues, you'll be okay on the exam. To summarize, the issues are:
  • Widening conversions may result in a loss of precision but not magnitude.
  • In general (except for this case converting from decimal), widening conversions don't require casting.
I hope that helps.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
possible loss of precision hobby Java Basics 7 September 19th, 2008 09:41 PM
Float precision nulogix C++ Programming 1 November 10th, 2005 09:53 PM
Using Datepart to get half an hour precision venomm Access 2 March 16th, 2005 10:08 AM
Number precision problem adriant42 Excel VBA 2 June 28th, 2004 01:00 PM
Length vs precision in decimal datatype rvw SQL Server 2000 2 January 5th, 2004 12:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.