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

In article <>,
mdeh <> wrote:

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

This is going from Bad to Worse!!

Using a News-Watcher which required authentication to post...giving the
false ( !apparently!) impression that the article was not posted.

I really only want one answer!!!

mdeh, Nov 8, 2007

3. CBFalconerGuest

mdeh wrote:
>
> 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.

Note the sign of "'0' - *s++".

Why post the same thing three times?

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

CBFalconer, Nov 8, 2007
4. mdhGuest

On Nov 7, 6:28 pm, CBFalconer <> wrote:
> mdeh wrote:
>
> > 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.

>
> Note the sign of "'0' - *s++".
>
> Why post the same thing three times?
>

Just in case you missed the first 2

No Seriously, you must have missed my above post. Google was not