Finding the Precision of Doubles

Discussion in 'C Programming' started by mvelez999, Sep 24, 2006.

  1. mvelez999

    mvelez999

    Joined:
    Sep 24, 2006
    Messages:
    4
    Likes Received:
    0
    Hello,

    I am trying to find the smallest 'delta' value that makes the following equation evaluate to TRUE.
    ***Let x be a double***
    x + 'delta' != x

    This is the code I wrote. Can somebody help me figure out why it stops after 1 execution of the WHILE loop?

    #include <iostream>

    using namespace std;

    void main()
    {
    int i=1;
    double x=1;
    while (x != (x + (1/i)))
    {
    i++;
    }
    cout << "I equals " << i << endl;
    //cout << "Delta equals " << 1/(i-1) << endl;
    }

    Result: I equals 2

    ****This next snippet stops after 1 execution as well (different code, same math). LOL.****

    void main()
    {
    int i=1;
    double x=1;
    double y = x + (1/i);
    while (x != y)
    {
    i++;
    y = x + (1/i);
    }
    cout << "I equals " << i << endl;
    //cout << "Delta equals " << 1/i << endl;
    }

    RESULT: I equals 2
    Thanks in advance.
     
    Last edited: Sep 24, 2006
    mvelez999, Sep 24, 2006
    #1
    1. Advertisements

  2. mvelez999

    mvelez999

    Joined:
    Sep 24, 2006
    Messages:
    4
    Likes Received:
    0
    New Attempt

    This is the newest code that I have tried. It still does not work. LOL.
    void main()
    {
    double i=2000000000;
    double delta = 1/i;
    int x=1;
    cout << "Original Delta " << delta << endl;
    while (x != (x + delta))
    {
    if (((int) i % 100000) == 0)
    {
    cout << "I equals " << i << endl;
    cout << "Delta equals " << delta << endl;
    }
    i++;
    delta = 1/i;
    }
    cout << "Final I equals " << i << endl;
    cout << "Final Delta equals " << delta << endl;
    }

    Final RESULTS:
    I equals 2.1472e+009
    Delta equals 4.65679e-010

    The results that I am expecting are:
    Final I equals . . .
    Final Delta equals . . .

    Why isn't this printing after the while loops ends? Or does the while never end?
     
    mvelez999, Sep 24, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.