Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 10th, 2013, 09:21 AM
Registered User
Points: 41, Level: 1
Points: 41, Level: 1 Points: 41, Level: 1 Points: 41, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!
Reply With Quote
  #2 (permalink)  
Old December 17th, 2013, 05:51 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #3 (permalink)  
Old December 17th, 2013, 11:02 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
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!)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:13 AM.


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