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. Advertising

  2. mdeh

    mdeh Guest

    In article <>,
    mdeh <> wrote:

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


    This is going from Bad to Worse!!

    Using a News-Watcher which required authentication to post...giving the
    false ( !apparently!) impression that the article was not posted.
    Sorry about that.

    I really only want one answer!!! :)
     
    mdeh, Nov 8, 2007
    #2
    1. Advertising

  3. mdeh

    CBFalconer Guest

    mdeh wrote:
    >
    > 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.


    Note the sign of "'0' - *s++".

    Why post the same thing three times?

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Nov 8, 2007
    #3
  4. mdeh

    mdh Guest

    On Nov 7, 6:28 pm, CBFalconer <> wrote:
    > mdeh wrote:
    >
    > > 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.

    >
    > Note the sign of "'0' - *s++".
    >
    > Why post the same thing three times?
    >



    Just in case you missed the first 2 :)

    No Seriously, you must have missed my above post. Google was not
    working, so I used a newsreader. When posting, it asked for
    authentication, then kept asking as if nothing was happening. Well,
    obviously, something was happening, as you can see by the number of
    posts.
     
    mdh, Nov 8, 2007
    #4
    1. Advertising

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:
    510
    lezah
    Feb 4, 2004
  2. prem_eda
    Replies:
    5
    Views:
    8,048
    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,308
    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:
    447
    glen herrmannsfeldt
    Nov 28, 2004
  5. Alex Fraser
    Replies:
    8
    Views:
    434
    Joe Wright
    Mar 8, 2006
Loading...

Share This Page