Negative Value

M

mdeh

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

Jack Klein

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

there is a standard said:
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
 
K

Keith Thompson

mdeh said:
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).
 
K

Kenneth Brody

mdeh said:
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:[email protected]>
 
M

mdh

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

Keith Thompson

mdh said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top