Thread: Absurd Math?!!

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Absurd Math?!!

    Hello,
    It's has been a long time since my last post here (just saying).
    Anyway, here's my code:
    Code:
    double i;
    for(i = 0;i<1;i=i+0.1){
    if(i*2 == 1.6)printf("%s\n", "Found the Number");
    }
    If could spare a few minutes for me, and run the program, you'll notice, that the message doesn’t appear(even though 0.8, as you know, is the answer, and the 'for' loop definitely covers that number).
    Any suggestions?
    Thank you very much for your attention,
    Thanks,
    Daniel
    P.S
    I've tried the code in Java, Qbasic, and it's working perfectly on them...
    Last edited by danielakkerman; 01-09-2007 at 11:42 AM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    for(i = 0;i<1i+i0.1){
    Your program should give a error message
    Code:
    for(i = 0;i<1;i+=0.1)
    Look u should compare the float with the == operator. search the form. And why this way

    Code:
    printf("%s\n", "Found the Number");
    could me

    Code:
    printf("Found the Number\n");


    ssharish2005
    Last edited by ssharish2005; 01-09-2007 at 11:48 AM.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Didn't quite get that...

    Could you correct the code please?
    Thanks,
    Daniel

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    for(i=0; i<1; i += 0.1)
        {
            if(fabs(i*2- 1.6) < 0.00001)
                    printf("Found the Number\n");
        }
    ssharish2005

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can search the forum, this was discussed many times... for example
    http://cboard.cprogramming.com/showt...compare+floats
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Working! XD Thanks :D!!!

    Thank you very much again, it's working.
    Thanks,
    Daniel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM