Negative Value

Discussion in 'C Programming' started by mdeh, Nov 8, 2007.

  1. mdeh

    mdeh Guest

    Hi Everyone,
    Tried to post via Google..which once again seems to be fritzed...so
    please excuse if 2 posts show up.

    I am trying to understand why I am not getting a negative value back,
    using my version of atof

    given the argument "-36.63"

    double atof ( char *s){
    double d = 0.00;
    double sign = 1.00;
    double fractprt = 1.00;
    /* check for neg number */
    if ( *s == '-') {
    sign = -1.00;
    s++;
    }

    while (isadigit(*s)){
    d=d*10.00 + ('0' - *s++);
    }
    if (*s == '.'){
    s++;
    while(isadigit(*s)){
    d=d*10.00 + ('0' - *s++);
    fractprt *=10;
    }
    }

    return (sign * d / fractprt) ;

    }

    I get 36.03, not -36.03.

    Thanks in advance.
     
    mdeh, Nov 8, 2007
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lezah
    Replies:
    0
    Views:
    669
    lezah
    Feb 4, 2004
  2. prem_eda
    Replies:
    5
    Views:
    9,234
    Pieter Hulshoff
    Oct 11, 2004
  3. Peter Ammon

    Most negative double value

    Peter Ammon, Feb 19, 2004, in forum: C Programming
    Replies:
    30
    Views:
    1,438
    Christian Bau
    Feb 25, 2004
  4. Angus Comber

    How to convert a negative value to positive

    Angus Comber, Nov 27, 2004, in forum: C Programming
    Replies:
    2
    Views:
    540
    glen herrmannsfeldt
    Nov 28, 2004
  5. Alex Fraser
    Replies:
    8
    Views:
    595
    Joe Wright
    Mar 8, 2006
  6. Martin Wells

    A character with a negative value

    Martin Wells, Nov 1, 2007, in forum: C Programming
    Replies:
    35
    Views:
    1,341
  7. mdeh

    Negative Value

    mdeh, Nov 8, 2007, in forum: C Programming
    Replies:
    8
    Views:
    763
  8. mdeh

    Negative Value

    mdeh, Nov 8, 2007, in forum: C Programming
    Replies:
    3
    Views:
    442
Loading...