473,320 Members | 1,856 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,320 software developers and data experts.

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

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.
Nov 8 '07 #1
8 4214
mdeh <md**@comcast.netwrites:
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>
while (isadigit(*s)){
d=d*10.00 + ('0' - *s++);
}
<snip>
I get 36.03, not -36.03.
What is the sign of '0' - '3'?

--
Ben.
Nov 8 '07 #2
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
mdeh <md**@comcast.netwrites:
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>
while (isadigit(*s)){
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
Nov 8 '07 #3
On Wed, 07 Nov 2007 16:30:10 -0800, mdeh <md**@comcast.netwrote 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++;
}

while (isadigit(*s)){
What's "isadigit"? If you include <ctype.h>, there is a standard
isdigit().
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
Nov 8 '07 #4
mdeh <md**@comcast.netwrites:
[...]
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Nov 8 '07 #5
mdh
On Nov 7, 10:46 pm, Keith Thompson <ks...@mib.orgwrote:
mdeh <m...@comcast.netwrites:
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.
Nov 8 '07 #6
mdeh wrote:
>
In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:
mdeh <md**@comcast.netwrites:
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>
while (isadigit(*s)){
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: <mailto:Th*************@gmail.com>
Nov 8 '07 #7
mdh <md**@comcast.netwrites:
On Nov 7, 10:46 pm, Keith Thompson <ks...@mib.orgwrote:
>mdeh <m...@comcast.netwrites:
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
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Nov 9 '07 #8
On 2007-11-08 16:35:53 -0800, Keith Thompson <ks***@mib.orgsaid:
mdh <md**@comcast.netwrites:
>On Nov 7, 10:46 pm, Keith Thompson <ks...@mib.orgwrote:
>>>
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.

Nov 9 '07 #9

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

Similar topics

5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
2
by: dave8421 | last post by:
Hi, I am reading CSS 2.1 draft specification: http://www.w3.org/TR/2006/WD-CSS21-20061106/ Part of section 10.3.3 (Block-level, non-replaced elements in normal flow) reads: If there is...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
35
by: Martin Wells | last post by:
Plain char may be signed or unsigned. Typical ranges could be: CHAR_MIN == -128, CHAR_MAX == 127 CHAR_MIN == 0, CHAR_MAX == 255 The Standard says that the behaviour is undefined if we...
1
by: vikastcser | last post by:
Hi All , I have a table where four rows are there and each row has two tds.one td is for label and two image icons and other td is used for keeping numeric values. I am able to accomodate digits...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.