Velocity Reviews > Negative Value

# Negative Value

mdeh
Guest
Posts: n/a

 11-08-2007
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.

Ben Bacarisse
Guest
Posts: n/a

 11-08-2007
mdeh <(E-Mail Removed)> 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.

mdeh
Guest
Posts: n/a

 11-08-2007
In article <(E-Mail Removed)>,
Ben Bacarisse <(E-Mail Removed)> wrote:

> mdeh <(E-Mail Removed)> 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

Jack Klein
Guest
Posts: n/a

 11-08-2007
On Wed, 07 Nov 2007 16:30:10 -0800, mdeh <(E-Mail Removed)> 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

Keith Thompson
Guest
Posts: n/a

 11-08-2007
mdeh <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"

Kenneth Brody
Guest
Posts: n/a

 11-08-2007
mdeh wrote:
>
> In article <(E-Mail Removed)>,
> Ben Bacarisse <(E-Mail Removed)> wrote:
>
> > mdeh <(E-Mail Removed)> 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: <(E-Mail Removed)>

mdh
Guest
Posts: n/a

 11-08-2007
On Nov 7, 10:46 pm, Keith Thompson <(E-Mail Removed)> wrote:
> mdeh <(E-Mail Removed)> 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.

Keith Thompson
Guest
Posts: n/a

 11-09-2007
mdh <(E-Mail Removed)> writes:
> On Nov 7, 10:46 pm, Keith Thompson <(E-Mail Removed)> wrote:
>> mdeh <(E-Mail Removed)> 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) (E-Mail Removed) <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"

mdeh
Guest
Posts: n/a

 11-09-2007
On 2007-11-08 16:35:53 -0800, Keith Thompson <(E-Mail Removed)> said:

> mdh <(E-Mail Removed)> writes:
>> On Nov 7, 10:46 pm, Keith Thompson <(E-Mail Removed)> 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.