473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Negative "not taking"

mdh
Hi All,
I understand atof is in the library, but this is part of my learning
curve.

Given

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

where s[] equals -36.03, the return I get is 36.03 not -36.03. Is
there a reason why
"return (sign * d / fractprt) ;"

does not seem to multiply by "-1"

Thanks

Nov 8 '07 #1
6 1396
mdh <md**@comcast.netwrites:
d=d*10.00 + ('0' - *s++);
I think you mean *s++ - '0'.
d=d*10.00 + ('0' - *s++);
Ditto.
there a reason why
"return (sign * d / fractprt) ;"

does not seem to multiply by "-1"
Because you get the sign of each digit wrong.
--
Ben Pfaff
http://benpfaff.org
Nov 8 '07 #2
On Nov 8, 9:46 am, mdh <m...@comcast.netwrote:
Hi All,
I understand atof is in the library, but this is part of my learning
curve.

Given

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++);
Consider this: what is the result of ('0' - '3') ?
Hint: it isn't what you think it is.

}
if (*s == '.'){
s++;
while(isadigit(*s)){
d=d*10.00 + ('0' - *s++);
Again, what is the result of ('0' - '3')?
fractprt *=10;
}
}
return (sign * d / fractprt) ;

}

where s[] equals -36.03, the return I get is 36.03 not -36.03. Is
there a reason why
"return (sign * d / fractprt) ;"

does not seem to multiply by "-1"
Think of it this way: what sort of number will give you a positive
value when multiplied by -1?
Thats the sort of number that (d/fractprt) represents.
So, how did you get that sort of number? What part of your function is
suspect? What part(s) produce signed values, where the sign can be
suspect?
Thanks

Nov 8 '07 #3
mdh
On Nov 8, 7:00 am, Lew Pitcher <lpitc...@teksavvy.comwrote:
On Nov 8, 9:46 am, mdh <m...@comcast.netwrote:

Think of it this way: what sort of number will give you a positive
value when multiplied by -1?

Thanks

It was a long day!!! Sometimes I think one gets so caught up in the
"progamming" aspect..esp when learning that one overlooks something
really basic.
thank you

Nov 8 '07 #4
mdh wrote:
Hi All,
I understand atof is in the library, but this is part of my learning
curve.

Given

double atof( char *s){
....
d=d*10.00 + ('0' - *s++);
....
d=d*10.00 + ('0' - *s++);
where s[] equals -36.03, the return I get is 36.03 not -36.03.
Deja Vu all over again. Your name isn't Leonard Shelby by any chance?
(http://tinyurl.com/347zrf)
Nov 8 '07 #5
Mark Bluemel <ma**********@pobox.comwrites:
mdh wrote:
>Hi All,
I understand atof is in the library, but this is part of my learning
curve.

Given

double atof( char *s){
...
> d=d*10.00 + ('0' - *s++);
...
> d=d*10.00 + ('0' - *s++);
>where s[] equals -36.03, the return I get is 36.03 not -36.03.

Deja Vu all over again. Your name isn't Leonard Shelby by any chance?
(http://tinyurl.com/347zrf)
99.9999% of questions asked here are deja vu. It's a help group I
thought? my advice to you is to take a break if you are scoffing at the
"usual mistakes".
Nov 8 '07 #6
Ben Pfaff wrote:
mdh <md**@comcast.netwrites:
> d=d*10.00 + ('0' - *s++);

I think you mean *s++ - '0'.
> d=d*10.00 + ('0' - *s++);

Ditto.
How about "- ('0' - *s++)" in both expressions? :-)

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

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

Nov 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: madsgormlarsen | last post by:
$tdsubclass=<<<bumelum <td><a href="http://www.ygabel.php/$artnr" ="" onmouseout=" function onmouseout(event) { window.status = "Done"; return true; } " onmouseover=" function...
6
by: Todd Peterson | last post by:
I'm encountering some wierd behavior with a <link> tag over an HTTPS connection, vs. an HTTP connection... In an ASP/HTML page on my website, I've add a <link rel="shortcut icon"...> in order to...
20
by: Karl Smith | last post by:
I heard a rumour that Opera succeeded where none have before, and implemented the tables described in HTML4 and CSS2. So I thought I'd try it out with the well known Periodic Table. ...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
12
by: Robbie Hatley | last post by:
I'm getting a bizarre error with this code: ... case WM_COMMAND: { switch (LOWORD(wParam)) // switch (control ID) { ... case IDC_RAIN_ENABLE_SIMPLE: {
2
by: anon.asdf | last post by:
Hello! 1) =============================== When trying to define an array of std::string ... func( (std::string ) { std::string("ab"), std::string("cd"), std::string("ef") } , 3 ); ...
6
by: grbgooglefan | last post by:
I am creating functions, the return result of which I am using to make decisions in combined expressions. In some expressions, I would like to inverse the return result of function. E.g....
2
by: Bart Kastermans | last post by:
I have a file in which I am searching for the letter "i" (actually a bit more general than that, arbitrary regular expressions could occur) as long as it does not occur inside an expression that...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.