# Negative Value

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

1. ### mdehGuest

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

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

return (sign * d / fractprt) ;

}

I get 36.03, not -36.03.

mdeh, Nov 8, 2007

2. ### Ben BacarisseGuest

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

3. ### mdehGuest

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>
> > 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
4. ### Jack KleinGuest

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

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

> d=d*10.00 + ('0' - *s++);
> }
> if (*s == '.'){
> s++;
> d=d*10.00 + ('0' - *s++);
> fractprt *=10;
> }
> }
>
> return (sign * d / fractprt) ;
>
> }
>
> I get 36.03, not -36.03.
>

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
5. ### Keith ThompsonGuest

mdeh <> writes:
[...]
> 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
6. ### Kenneth BrodyGuest

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

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
8. ### Keith ThompsonGuest

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

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
9. ### mdehGuest

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