Negative "not taking"

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

  1. mdh

    mdh Guest

    Hi All,
    I understand atof is in the library, but this is part of my learning
    curve.

    Given

    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) ;
    }

    where s[] equals -36.03, the return I get is 36.03 not -36.03. Is
    there a reason why
    "return (sign * d / fractprt) ;"

    does not seem to multiply by "-1"

    Thanks
     
    mdh, Nov 8, 2007
    #1
    1. Advertising

  2. mdh

    Ben Pfaff Guest

    mdh <> writes:

    > d=d*10.00 + ('0' - *s++);


    I think you mean *s++ - '0'.

    > d=d*10.00 + ('0' - *s++);


    Ditto.

    > there a reason why
    > "return (sign * d / fractprt) ;"
    >
    > does not seem to multiply by "-1"


    Because you get the sign of each digit wrong.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Nov 8, 2007
    #2
    1. Advertising

  3. mdh

    Lew Pitcher Guest

    On Nov 8, 9:46 am, mdh <> wrote:
    > Hi All,
    > I understand atof is in the library, but this is part of my learning
    > curve.
    >
    > Given
    >
    > 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++);


    Consider this: what is the result of ('0' - '3') ?
    Hint: it isn't what you think it is.


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


    Again, what is the result of ('0' - '3')?

    > fractprt *=10;
    > }
    > }
    > return (sign * d / fractprt) ;
    >
    > }
    >
    > where s[] equals -36.03, the return I get is 36.03 not -36.03. Is
    > there a reason why
    > "return (sign * d / fractprt) ;"
    >
    > does not seem to multiply by "-1"


    Think of it this way: what sort of number will give you a positive
    value when multiplied by -1?
    Thats the sort of number that (d/fractprt) represents.
    So, how did you get that sort of number? What part of your function is
    suspect? What part(s) produce signed values, where the sign can be
    suspect?

    > Thanks
     
    Lew Pitcher, Nov 8, 2007
    #3
  4. mdh

    mdh Guest

    On Nov 8, 7:00 am, Lew Pitcher <> wrote:
    > On Nov 8, 9:46 am, mdh <> wrote:
    >
    > Think of it this way: what sort of number will give you a positive
    > value when multiplied by -1?
    >
    >
    > > Thanks



    It was a long day!!! Sometimes I think one gets so caught up in the
    "progamming" aspect..esp when learning that one overlooks something
    really basic.
    thank you
     
    mdh, Nov 8, 2007
    #4
  5. mdh

    Mark Bluemel Guest

    mdh wrote:
    > Hi All,
    > I understand atof is in the library, but this is part of my learning
    > curve.
    >
    > Given
    >
    > double atof( char *s){

    ....
    > d=d*10.00 + ('0' - *s++);

    ....

    > d=d*10.00 + ('0' - *s++);


    > where s[] equals -36.03, the return I get is 36.03 not -36.03.


    Deja Vu all over again. Your name isn't Leonard Shelby by any chance?
    (http://tinyurl.com/347zrf)
     
    Mark Bluemel, Nov 8, 2007
    #5
  6. mdh

    Richard Guest

    Mark Bluemel <> writes:

    > mdh wrote:
    >> Hi All,
    >> I understand atof is in the library, but this is part of my learning
    >> curve.
    >>
    >> Given
    >>
    >> double atof( char *s){

    > ...
    >> d=d*10.00 + ('0' - *s++);

    > ...
    >
    >> d=d*10.00 + ('0' - *s++);

    >
    >> where s[] equals -36.03, the return I get is 36.03 not -36.03.

    >
    > Deja Vu all over again. Your name isn't Leonard Shelby by any chance?
    > (http://tinyurl.com/347zrf)


    99.9999% of questions asked here are deja vu. It's a help group I
    thought? my advice to you is to take a break if you are scoffing at the
    "usual mistakes".
     
    Richard, Nov 8, 2007
    #6
  7. mdh

    CBFalconer Guest

    Ben Pfaff wrote:
    > mdh <> writes:
    >
    >> d=d*10.00 + ('0' - *s++);

    >
    > I think you mean *s++ - '0'.
    >
    >> d=d*10.00 + ('0' - *s++);

    >
    > Ditto.


    How about "- ('0' - *s++)" in both expressions? :)

    --
    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
    #7
    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. prem_eda
    Replies:
    5
    Views:
    7,943
    Pieter Hulshoff
    Oct 11, 2004
  2. paul
    Replies:
    2
    Views:
    323
    Jacob Yang [MSFT]
    Oct 24, 2003
  3. Josh Anderson - remove Zs to reply

    CausesValidation not taking in Repeater control

    Josh Anderson - remove Zs to reply, Nov 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    380
    Josh Anderson - remove Zs to reply
    Nov 20, 2003
  4. MattC
    Replies:
    1
    Views:
    1,630
    Raterus
    Aug 6, 2004
  5. Malik Asif Joyia
    Replies:
    2
    Views:
    1,093
    =?Utf-8?B?ZGFuaWVs?=
    Aug 12, 2005
Loading...

Share This Page