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 <> writes:

    > 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){

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

    <snip>

    > I get 36.03, not -36.03.


    What is the sign of '0' - '3'?

    --
    Ben.
     
    Ben Bacarisse, Nov 8, 2007
    #2
    1. Advertising

  3. mdeh

    mdeh Guest

    In article <>,
    Ben Bacarisse <> wrote:

    > mdeh <> writes:
    >
    > > 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){

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

    > <snip>
    >
    > > I get 36.03, not -36.03.

    >
    > What is the sign of '0' - '3'?


    o oh!!! Long day!!

    Thanks Ben
     
    mdeh, Nov 8, 2007
    #3
  4. mdeh

    Jack Klein Guest

    On Wed, 07 Nov 2007 16:30:10 -0800, mdeh <> wrote in
    comp.lang.c:

    > 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){


    Creating your own function with the name of a standard library
    function produces undefined behavior. Name it something else.

    > double d = 0.00;
    > double sign = 1.00;


    I would have made this an int.

    > double fractprt = 1.00;
    > /* check for neg number */
    > if ( *s == '-') {
    > sign = -1.00;
    > s++;
    > }
    >
    > while (isadigit(*s)){


    What's "isadigit"? If you include <ctype.h>, there is a standard
    isdigit().

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


    Others have pointed out the reason for the sign reversal.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 8, 2007
    #4
  5. mdeh <> writes:
    [...]
    > while (isadigit(*s)){
    > d=d*10.00 + ('0' - *s++);
    > }

    [...]

    What is "isadigit"? There's a standard function called "isdigit".

    Always post exact code (copy-and-paste it).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 8, 2007
    #5
  6. mdeh wrote:
    >
    > In article <>,
    > Ben Bacarisse <> wrote:
    >
    > > mdeh <> writes:
    > >
    > > > 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){

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

    > > <snip>
    > >
    > > > I get 36.03, not -36.03.

    > >
    > > What is the sign of '0' - '3'?

    >
    > o oh!!! Long day!!


    You might have also noticed that "36.03" would return -36.03, since
    its sign would also be reversed.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Nov 8, 2007
    #6
  7. mdeh

    mdh Guest

    On Nov 7, 10:46 pm, Keith Thompson <> wrote:
    > mdeh <> writes:
    >


    > What is "isadigit"? There's a standard function called "isdigit".
    >
    > Always post exact code (copy-and-paste it).
    >
    > --
    >


    that is the exact code. While going through K&R I like to write my own
    little functions like "isadigit" as it is often a good source of
    instruction to me.
     
    mdh, Nov 8, 2007
    #7
  8. mdh <> writes:
    > On Nov 7, 10:46 pm, Keith Thompson <> wrote:
    >> mdeh <> writes:
    >> What is "isadigit"? There's a standard function called "isdigit".
    >>
    >> Always post exact code (copy-and-paste it).

    >
    > that is the exact code. While going through K&R I like to write my own
    > little functions like "isadigit" as it is often a good source of
    > instruction to me.


    Then you posted incomplete code. For all we know, there could have
    been a bug in your "isadigit" function.

    Incidentally, names starting with "is" and a lowercase letter are
    reserved in some contexts. It's unlikely to cause a problem in
    practice, but you should be aware of it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 9, 2007
    #8
  9. mdeh

    mdeh Guest

    On 2007-11-08 16:35:53 -0800, Keith Thompson <> said:

    > mdh <> writes:
    >> On Nov 7, 10:46 pm, Keith Thompson <> wrote:
    >>>
    >>> What is "isadigit"? There's a standard function called "isdigit".
    >>>

    >>
    >> that is the exact code. While going through K&R I like to write my own
    >> little functions like "isadigit" as it is often a good source of
    >> instruction to me.

    >
    > Then you posted incomplete code.



    Point taken. Thank you.
     
    mdeh, Nov 9, 2007
    #9
    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:
    504
    lezah
    Feb 4, 2004
  2. prem_eda
    Replies:
    5
    Views:
    7,980
    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,304
    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:
    437
    glen herrmannsfeldt
    Nov 28, 2004
  5. Alex Fraser
    Replies:
    8
    Views:
    422
    Joe Wright
    Mar 8, 2006
Loading...

Share This Page