Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Dev sOs Net

Dev sOs Net Logo Dev sOs Net Logo

Dev sOs Net Navigation

  • Home
  • About Us
  • Dev Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About Us
  • Dev Blog
  • Contact Us
Home/ Questions/Q 107
Next
Answered

Dev sOs Net Latest Questions

Dev
  • 3
  • 3
DevTeacher
Asked: April 18, 20182018-04-18T21:36:57+00:00 2018-04-18T21:36:57+00:00In: 3. Windows Hosting, 3.2. ASP.NET Nutshell, Knowledge Base

C# Cookbook – Recipe 1.1 Determining Approximate Equality Between a Fraction and Floating-Point Value

  • 3
  • 3

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?

6. ASP.NET AuthorJay HilyardStephen Teilhet
  • 1 1 Answer
  • 74 Views
  • 2 Followers
  • 3
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse


1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Jay Hilyard, Stephen Teilhet
    Best Answer
    Jay Hilyard, Stephen Teilhet
    2018-04-18T21:39:58+00:00Added an answer on April 18, 2018 at 9:39 pm
    This answer was edited.

    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 now indicates 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.

      • 4
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 10
  • Answers 10
  • Best Answers 7
  • User 1
  • Popular
  • Answers
  • Dev

    Apache Cookbook - Recipe 1.1 Installing from Red Hat Linux’s ...

    • 1 Answer
  • Dev

    Linux Cookbook - Recipe 1.1 Introduction

    • 1 Answer
  • Dev

    PHP Cookbook - Recipe 1.1 Introduction

    • 1 Answer
  • Martin Hope
    Martin Hope added an answer They might be as confused as to why you keep… April 19, 2018 at 2:07 am
  • Marko Smith
    Marko Smith added an answer I have never heard a British person EVER call a… April 19, 2018 at 2:07 am
  • Barry Carter
    Barry Carter added an answer Calling a bread roll a “biscuit” really takes the biscuit.… April 19, 2018 at 2:07 am

Related Questions

  • SQL Cookbook - Recipe 1.1 Retrieving All Rows and Columns ...

    • 1 Answer
  • Visual Basic Cookbook - Recipe 1.1 Creating a Windows Forms ...

    • 1 Answer
  • ASP.NET Cookbook - Recipe 1.1 Selecting the Right Tabular Control

    • 1 Answer
  • Windows Server Cookbook - Recipe 1.1 Introduction

    • 1 Answer
  • WordPress Manual - Recipe 1.1 How WordPress Works

    • 1 Answer

Top Members

Dev

Dev

  • 10 Questions
  • 82 Points
Teacher

Trending Tags

1. Linux Author 4. WordPress Author 5. Windows Server Author 6. ASP.NET Author 7. SQL Author Adam Trachtenberg Anthony Molinaro Carla Schroder David Sklar Geoffrey T. LeBlond Jay Hilyard John Clark Craig Ken Coar Matthew MacDonald Michael A. Kittel Paul DuBois Rich Bowen Robbie Allen Stephen Teilhet Tim Patrick

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Find Solutions

Footer

Dev sOs Net

Dev sOs Net

Develop
Share Online Solution Network

sOs Net

  • About Us
  • About Team

Legal Agreement

  • Privacy Policy
  • Terms and Conditions

User Support

  • Knowledge Base
  • Contact Us

Follow Us

Copyright © Dev sOs Net. All rights reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.