C#
  Home arrow C# arrow Page 2 - 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 - Determining Approximate Equality Between a Fraction and Floating-Point Value


    (Page 2 of 12 )

    Problem

    You need to compare a fraction with a value of type double or float to determine whether they are within a close approximation to each other. Take, for example, the result of comparing the expression 1/6 and the value 0.16666667. These seem to be equivalent, except that 0.16666666 is precise to only 8 places to the right of the decimal point, and 1/6 is precise to the maximum number of digits to the right of the decimal point that the data type will hold.

    Solution

    Verify that the difference between the two values is within an acceptable tolerance:

    using System;

    public static bool IsApproximatelyEqualTo(double numerator,
    double denominator,
    double dblValue,
    double epsilon)

    {

    double difference = (numerator/denominator) - dblValue;

    if (Math.Abs(difference) < epsilon)

    {
    // This is a good approximation
    return (true);

    }
    else
    {

    // This is NOT a good approximation
    return (false);

    }

    }

    Replacing the type double with float allows you to determine whether a fraction and a float value are approximately equal.

    Discussion

    Fractions can be expressed as a numerator over a denominator; however, storing them as a floating-point value might be necessary. Storing fractions as floating-point values introduces rounding errors that make it difficult to perform comparisons. Expressing the value as a fraction (e.g., 1/6) allows the maximum precision. Expressing the value as a floating-point value (e.g., 0.16667) can limit the precision of the value. In this case, the precision depends on the number of digits that the developer decides to use to the right of the decimal point.

    You might need a way to determine whether two values are approximately equal to each other. This comparison is achieved by defining a value (epsilon) that is the smallest positive value, greater than zero, in which the absolute value of the difference between two values (numerator/denominator -dblValue) must be less than. In other words, by taking the absolute value of the difference between the fraction and the floating-point value and comparing it to a predetermined value passed to the epsilon argument, we can determine whether the floating-point value is a good approximation of the fraction.

    Consider a comparison between the fraction 1/7 and its floating-point value, 0.14285714285714285. The following call to the IsApproximatelyEqualTo method indicates that there are not enough digits to the right of the decimal point in the floating-point value to be a good approximation of the fraction (there are 6 digits, although 7 are required):

    bool Approximate = Class1.IsApproximatelyEqualTo(1, 7, .142857, .0000001);
    // Approximate == false

    Adding another digit of precision to the third parameter of this method nowindicates that this more precise number is what we require for a good approximation of the fraction 1/7:

    bool Approximate = Class1.IsApproximatelyEqualTo(1, 7, .1428571, .0000001);
    // Approximate == true

    See Also: See the “Double.Epsilon Field” and “Single.Epsilon Field” topics in the MSDN documentation.

    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.

    More C# Articles
    More By O'Reilly Media


     

    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 4 hosted by Hostway
    Stay green...Green IT