Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Negative Value (http://www.velocityreviews.com/forums/t550179-negative-value.html)

 mdeh 11-08-2007 12:35 AM

Negative Value

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 11-08-2007 12:44 AM

Re: Negative Value

In article <mdeh-A17589.16353107112007@newsgroups.comcast.net>,
mdeh <mdeh@comcast.net> 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!!! :-)

 CBFalconer 11-08-2007 02:28 AM

Re: Negative Value

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

 mdh 11-08-2007 03:31 AM

Re: Negative Value

On Nov 7, 6:28 pm, CBFalconer <cbfalco...@yahoo.com> 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
authentication, then kept asking as if nothing was happening. Well,
obviously, something was happening, as you can see by the number of
posts.

 All times are GMT. The time now is 08:58 AM.