C#
  Home arrow C# arrow Page 10 - Numbers
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Numbers
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 19
    2004-06-29

    Table of Contents:
  • Numbers
  • Determining Approximate Equality Between a Fraction and Floating-Point Value
  • Converting Degrees to Radians and Converting Radians to Degrees
  • Using the Bitwise Complement Operator with Various Data Types
  • Test for an Even or Odd Value
  • Converting a Number in Another Base to Base10 and Determining Whether a String Is a Valid Number
  • Determining Whether a String Is a Valid Number
  • Rounding a Floating-Point Value and Different Rounding Algorithms Problem
  • Converting Celsius to Fahrenheit and
  • Safely Performing a Narrowing Numeric Cast
  • Discussion
  • Finding the Length of Any Three Sides of a Right Triangle and Finding the Angles of a Right Triangle Problem

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Numbers - Safely Performing a Narrowing Numeric Cast


    (Page 10 of 12 )

     
    1.13 Safely Performing a Narrowing Numeric Cast
     
    Problem

    You need to cast a value from a larger value to a smaller one, while gracefully handling conditions that result in a loss of information. For example, casting a long to an int results only in a loss of information if the long data type is greater than int. MaxSize.

    Solution

    The simplest way to do this check is to use the checked keyword. The following method accepts two long data types and attempts to add them together. The result is stuffed into an int data type. If an overflowcondition exists, the OverflowException is thrown:

    using System;

    public void UseChecked(long lhs, long rhs)
    {
    int result = 0;
    try
    {
    result = checked((int)(lhs + rhs));
    }
    catch (OverflowException e)
    {
    // Handle overflow exception here.

    }
    }

    This is the simplest method. However, if you do not want the overhead of throwing an exception and having to wrap a lot of code in try/catch blocks to handle the overflowcondition, you could use the MaxValue and MinValue fields of each type. A check using these fields can be done prior to the conversion to insure that no loss of information occurs. If this does occur, the code can inform the application that this cast will cause a loss of information. You can use the following conditional statement to determine whether sourceValue can be cast to a short without losing any information:

    // Our two variables are declared and initialized
    int sourceValue = 34000;
    short destinationValue = 0;

    // Determine if sourceValue will lose information in a cast to a short

    if (sourceValue <= short.MaxValue && sourceValue >= short.MinValue)
    {

    destinationValue = (short)sourceValue;
    } else
    {

    // Inform the application that a loss of information will occur
    }

    Instead of placing this conditional throughout your code, you can use the following overloaded methods to determine whether an integral type will lose data in a cast:

    // Overloaded methods to check conversions from unsigned integral types
    // to any other type
    public static bool IsSafeToConvert(byte valueToConvert,

    string typeToConvertTo) { return (IsSafeToConvert((ulong)valueToConvert, typeToConvertTo));
    }

    public static bool IsSafeToConvert(ushort valueToConvert, string typeToConvertTo)
    {
    return (IsSafeToConvert((ulong)valueToConvert, typeToConvertTo));
    }

    public static bool IsSafeToConvert(uint valueToConvert, string typeToConvertTo)
    {
    return (IsSafeToConvert((ulong)valueToConvert, typeToConvertTo));
    }

    public static bool IsSafeToConvert(ulong valueToConvert, string typeToConvertTo)
    {
    bool isSafe = false;

    switch(typeToConvertTo)
    {
    case "byte": if(valueToConvert <= byte.MaxValue && valueToConvert >= 0) isSafe = true; break;

    case "sbyte":
    if(valueToConvert <= (ulong)sbyte.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "short":
    if(valueToConvert <= (ulong)short.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "ushort":
    if(valueToConvert <= ushort.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "int":
    if(valueToConvert <= int.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "uint":
    if(valueToConvert <= uint.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "long":
    if(valueToConvert <= long.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "ulong":
    if(valueToConvert <= ulong.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    case "char":
    if(valueToConvert <= char.MaxValue && valueToConvert >= 0) isSafe = true;
    break;

    default: isSafe = true; break;

    }

    return (isSafe);
    }

    // Overloaded methods to check conversions from signed integral types
    // to any other type
    public static bool IsSafeToConvert(sbyte valueToConvert,

    string typeToConvertTo)
    {
    return (IsSafeToConvert((long)valueToConvert, typeToConvertTo));
    }
    public static bool IsSafeToConvert(short valueToConvert, string typeToConvertTo)
    {
    return (IsSafeToConvert((long)valueToConvert, typeToConvertTo));
    }

    public static bool IsSafeToConvert(int valueToConvert, string typeToConvertTo)

    { return (IsSafeToConvert((long)valueToConvert, typeToConvertTo)); }

    public static bool IsSafeToConvert(char valueToConvert, string typeToConvertTo)
    {
    return (IsSafeToConvert((long)valueToConvert, typeToConvertTo));
    }

    public static bool IsSafeToConvert(long valueToConvert, string typeToConvertTo)
    { bool isSafe = false;

    switch(typeToConvertTo)
    { case "byte":
    if(valueToConvert <= byte.MaxValue && valueToConvert >= byte.MinValue) isSafe = true;
    break;

    case "sbyte":
    if(valueToConvert <= sbyte.MaxValue && valueToConvert >= sbyte.MinValue) isSafe = true;
    break;

    case "short":
    if(valueToConvert <= short.MaxValue && valueToConvert >= short.MinValue) isSafe = true;
    break;

    case "ushort":
    if(valueToConvert <= ushort.MaxValue && valueToConvert >= ushort.MinValue) isSafe = true;
    break;

    case "int":
    if(valueToConvert <= int.MaxValue && valueToConvert >= int.MinValue) isSafe = true;
    break;

    case "uint":
    if(valueToConvert <= uint.MaxValue && valueToConvert >= uint.MinValue) isSafe = true;
    break;

    case "long":
    if(valueToConvert <= long.MaxValue && valueToConvert >= long.MinValue) isSafe = true;
    break;

    case "ulong":
    if(valueToConvert >= 0) isSafe = true;
    break;

    case "char":
    if(valueToConvert <= char.MaxValue && valueToConvert >= char.MinValue) isSafe = true;
    break;

    default: isSafe = true; break;

    }

    return (isSafe);
    }

    // Overloaded methods to check conversions from a float type
    // to any other type public bool IsSafeToConvert(float valueToConvert, string typeToConvertTo)
    {

    bool isSafe = false;

    switch(typeToConvertTo)
    {
    case "byte": if(valueToConvert <= byte.MaxValue && valueToConvert >= byte.MinValue) isSafe = true;
    break;

    case "sbyte":
    if(valueToConvert <= sbyte.MaxValue && valueToConvert >= sbyte.MinValue) isSafe = true;
    break;

    case "short":
    if(valueToConvert <= short.MaxValue && valueToConvert >= short.MinValue) isSafe = true;
    break;

    case "ushort":
    if(valueToConvert <= ushort.MaxValue && valueToConvert >= ushort.MinValue)

    isSafe = true; break;

    case "int":
    if(valueToConvert <= int.MaxValue && valueToConvert >= int.MinValue) isSafe = true;
    break;

    case "uint":
    if(valueToConvert <= uint.MaxValue && valueToConvert >= uint.MinValue) isSafe = true;
    break;

    case "long":
    if(valueToConvert <= long.MaxValue && valueToConvert >= long.MinValue) isSafe = true;
    break;

    case "ulong":
    if(valueToConvert <= ulong.MaxValue && valueToConvert >= ulong.MinValue) isSafe = true;
    break;

    case "char":
    if(valueToConvert <= char.MaxValue && valueToConvert >= char.MinValue) isSafe = true;
    break;

    case "double":
    if(valueToConvert <= double.MaxValue && valueToConvert >= double.MinValue) isSafe = true;
    break;

    case "decimal":
    if(valueToConvert <= (float)decimal.MaxValue && valueToConvert >= (float)decimal.MinValue) isSafe = true;
    break;

    default: isSafe = true;
    break;

    }

    return (isSafe);
    }

    // Overloaded methods to check conversions from a double type
    // to any other type

    public bool IsSafeToConvert(double valueToConvert, string typeToConvertTo)
    {
    bool isSafe = false;

    switch(typeToConvertTo)
    {
    case "byte":
    if(valueToConvert <= byte.MaxValue && valueToConvert >= byte.MinValue) isSafe = true;
    break;

    case "sbyte":
    if(valueToConvert <= sbyte.MaxValue && valueToConvert >= sbyte.MinValue) isSafe = true;
    break;

    case "short":
    if(valueToConvert <= short.MaxValue && valueToConvert >= short.MinValue) isSafe = true;
    break;

    case "ushort":
    if(valueToConvert <= ushort.MaxValue && valueToConvert >= ushort.MinValue) isSafe = true;
    break;

    case "int":
    if(valueToConvert <= int.MaxValue && valueToConvert >= int.MinValue) isSafe = true;
    break;

    case "uint":
    if(valueToConvert <= uint.MaxValue && valueToConvert >= uint.MinValue) isSafe = true;
    break;

    case "long":
    if(valueToConvert <= long.MaxValue && valueToConvert >= long.MinValue) isSafe = true;
    break;

    case "ulong":
    if(valueToConvert <= ulong.MaxValue && valueToConvert >= ulong.MinValue) isSafe = true;
    break;

    case "char":
    if(valueToConvert <= char.MaxValue && valueToConvert >= char.MinValue) isSafe = true;
    break;

    case "float":
    if(valueToConvert <= float.MaxValue && valueToConvert >= float.MinValue) isSafe = true;
    break;

    case "decimal":
    if(valueToConvert <= (double)decimal.MaxValue && valueToConvert >= (double)decimal.MinValue) isSafe = true;
    break;

    default: isSafe = true; break;

    }

    return (isSafe);
    }

    // Overloaded methods to check conversions from a decimal type
    // to any other type public bool IsSafeToConvert(decimal valueToConvert,

    string typeToConvertTo) { bool isSafe = false;

    switch(typeToConvertTo)
    {
    case "byte": if(valueToConvert <= byte.MaxValue && valueToConvert >= byte.MinValue) isSafe = true;
    break;

    case "sbyte":
    if(valueToConvert <= sbyte.MaxValue && valueToConvert >= sbyte.MinValue) isSafe = true;
    break;

    case "short":
    if(valueToConvert <= short.MaxValue && valueToConvert >= short.MinValue) isSafe = true;
    break;

    case "ushort":
    if(valueToConvert <= ushort.MaxValue && valueToConvert >= ushort.MinValue) isSafe = true;
    break;

    case "int":
    if(valueToConvert <= int.MaxValue && valueToConvert >= int.MinValue) isSafe = true;
    break;

    case "uint":
    if(valueToConvert <= uint.MaxValue && valueToConvert >= uint.MinValue) isSafe = true;
    break;

    case "long":
    if(valueToConvert <= long.MaxValue && valueToConvert >= long.MinValue) isSafe = true;
    break;

    case "ulong":
    if(valueToConvert <= ulong.MaxValue && valueToConvert >= ulong.MinValue) isSafe = true;
    break;

    case "char":
    if(valueToConvert <= char.MaxValue && valueToConvert >= char.MinValue) isSafe = true;
    break;

    default: isSafe = true; break;

    }

    return (isSafe);
    }

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT