The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Reality Check[ed] from Keller

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Dan Fernandez

Posts: 456
Nickname: danielfe
Registered: Aug, 2003

Daniel Fernandez is the Product Manager for C# in the developer division at Microsoft.
Reality Check[ed] from Keller Posted: Sep 4, 2003 5:02 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Dan Fernandez.
Original Post: Reality Check[ed] from Keller
Feed Title: Dan Fernandez's Blog
Feed URL: /msdnerror.htm?aspxerrorpath=/danielfe/Rss.aspx
Feed Description: Dan discusses Dot Net.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Dan Fernandez
Latest Posts From Dan Fernandez's Blog

Advertisement

[Sorry for not posting, but I've been *way* too busy, and blogging is the first thing to cut when push comes to shove]

After my last post on Pascal's Triangle, Brian Keller and I were in his office when he shot me a concerned look and wondered why anyone would care about Pascal's triangle.  Some examples include calculating the odds of winning a pick six lotto ticket, or knowing how many possible hands you can have with a deck of cards.  Given an N of 52 (cards), and an R of 5 (# of cards in a hand) we can plug this into our code and see that there are 2,598,960 unique hands for poker.  I also switched my code to use double because of arithmetic overflows for ints.  By default, C# does not do boundary checking for arithmetic operations and values silently overflow for runtime expressions. 

Note: Constant expressions don't need the checked keyword as they are checked at compile time rather then runtime. 

 There are a couple of ways you can check for overflows, including the checked keyword and compiler switch shown below:

Use for a particular expression:

 a = checked(b * c);  

Use it for a block of code

checked 
{
    a = b * c;
    d = e * f;
}

Use Visual Studio to check all arithmetic operations in a given project

  1. Right click on the project name in Solution Explorer
  2. Select Properties
  3. Under the Configuration Options folder select Build
  4. Change "Check for arithmetic overflow" from false to true

Use the command line compiler to do the same thing

  1. Run:
    csc.exe foo.cs /checked

Tip: You can also run /checked with a + or - symbol telling the compiler whether you want it to throw an exception if there is an overflow.

csc.exe foo.cs /checked+

If you want to turn off arithmetic checking at compile time for constants (please tell me why you would want to do this), you can also use the unchecked keyword:

a = unchecked(2 * 3);
 

Read: Reality Check[ed] from Keller

Topic: PDC 2003 Agenda and Sessions coming online Previous Topic   Next Topic Topic: Different Views

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use