473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strtod - Dynamic Memory?

atof is not working.

double a = atof("12.345");

a gets set to 12.000

I am working on a toshiba micro. The data map has no space allocated
to it for dynamic memory.

Does anyone have an idea? Could it be due to the lack of dynamic
memmor space?

All thoughts gratefully received

Marky C
Nov 14 '05
21 2281
In <Bo************ ******@newsread 1.news.pas.eart hlink.net> "Mike Wahler" <mk******@mkwah ler.net> writes:

"Marky C" <eg*********@ho tmail.com> wrote in message
news:9e******* *************** ****@posting.go ogle.com...
atof is not working.

double a = atof("12.345");

a gets set to 12.000


If you're using a pre-C99 compiler, and failed
to provide a prototype for 'atof()' (by #including
<stdlib.h>), then the compiler will assume that 'atof()'
returns type 'int'. This fact will truncate the
fractional part of the return value before assigning
it to 'a'.


Think harder. If atof is supposed to return an int, the compiler
will look in the wrong place for the result (e.g. an integer register
instead of a floating point register), or will interpret part of
the representation of the f.p. result as an integer, if the result
is passed in memory (on the stack). No conversion of 12.345 to the
implicitly declared type of atof() is supposed to take place.
A result of 12 can only be explained as a pure accident (and I have
no better explanation).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
"Dan Pop" <Da*****@cern.c h> wrote in message
news:c4******** **@sunnews.cern .ch...
In <Bo************ ******@newsread 1.news.pas.eart hlink.net> "Mike Wahler" <mk******@mkwah ler.net> writes:
"Marky C" <eg*********@ho tmail.com> wrote in message
news:9e******* *************** ****@posting.go ogle.com...
atof is not working.

double a = atof("12.345");

a gets set to 12.000
If you're using a pre-C99 compiler, and failed
to provide a prototype for 'atof()' (by #including
<stdlib.h>), then the compiler will assume that 'atof()'
returns type 'int'. This fact will truncate the
fractional part of the return value before assigning
it to 'a'.


Think harder.


That's too hard. :-)
If atof is supposed to return an int, the compiler
will look in the wrong place for the result (e.g. an integer register
instead of a floating point register), or will interpret part of
the representation of the f.p. result as an integer, if the result
is passed in memory (on the stack). No conversion of 12.345 to the
implicitly declared type of atof() is supposed to take place.
A result of 12 can only be explained as a pure accident (and I have
no better explanation).


You're right, I didn't give the issue enough thought.
I'll blame it on the (unseasonably) nice weather here
today, which has me frequently looking out the window. :-)

-Mike
Nov 14 '05 #12
eg*********@hot mail.com (Marky C) wrote in message news:<9e******* *************** ****@posting.go ogle.com>...
atof is not working.

double a = atof("12.345");

a gets set to 12.000

I am working on a toshiba micro. The data map has no space allocated
to it for dynamic memory.

Does anyone have an idea? Could it be due to the lack of dynamic
memmor space?

All thoughts gratefully received

Marky C


Perhaps your program is not running in the "C" locale. In which case the
character that represents the decimal point depends on the locale. As
far as I know, it is always either a '.' or a ',', but that isn't
guaranteed. Example that demonstrates the issue using strtod, which
is preferable to atof:

dresnick(3276)$ cat foo.c
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
printf("%g\n", strtod("12.345" , NULL));
setlocale(LC_NU MERIC, "de_DE");
printf("%g\n", strtod("12.345" , NULL));
printf("%g\n", strtod("12,345" , NULL));
return 0;
}
dresnick(3277)$ gcc -Wall -ansi -pedantic -o foo foo.c
dresnick(3278)$ foo
12.345
12
12,345
-David
Nov 14 '05 #13
Marky C wrote:
atof is not working.

double a = atof("12.345");

a gets set to 12.000


If you forget to include a prototype for atof it will
default to return int and as we all know
(double)(int)12 .345 == 12.000 or thereabouts.

Have you included stdlib.h?

--
Thomas.

Nov 14 '05 #14
In <59************ *************@p osting.google.c om> ln********@hotm ail.com (David Resnick) writes:
eg*********@ho tmail.com (Marky C) wrote in message news:<9e******* *************** ****@posting.go ogle.com>...
atof is not working.

double a = atof("12.345");

a gets set to 12.000

I am working on a toshiba micro. The data map has no space allocated
to it for dynamic memory.

Does anyone have an idea? Could it be due to the lack of dynamic
memmor space?

All thoughts gratefully received

Marky C
Perhaps your program is not running in the "C" locale. In which case the
character that represents the decimal point depends on the locale.


Nope:

6 In other than the "C" locale, additional locale-specific subject
^^^^^^^^^^
sequence forms may be accepted.

This means that '.' is accepted in ALL locales, but other locales can also
accept other characters as the "decimal point".
As far as I know, it is always either a '.' or a ',', but that isn't
guaranteed. Example that demonstrates the issue using strtod, which
is preferable to atof:

dresnick(3276) $ cat foo.c
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
printf("%g\n", strtod("12.345" , NULL));
setlocale(LC_NU MERIC, "de_DE");
printf("%g\n", strtod("12.345" , NULL));
printf("%g\n", strtod("12,345" , NULL));
return 0;
}
dresnick(3277) $ gcc -Wall -ansi -pedantic -o foo foo.c
dresnick(3278) $ foo
12.345
12
12,345


Your library is broken in multiple places. The correct output of your
program consists of three *identical* lines because "12.345" is valid
input for strtod in *any* locale and because the current locale does
NOT affect the output of the %g conversion specifier (for reasons
that I do not understand, the Single Unix Specification is at odds
with the C standard here).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15
In <40********@nnt phost.cis.strat h.ac.uk> Thomas Stegen <ts*****@cis.st rath.ac.uk> writes:
Marky C wrote:
atof is not working.

double a = atof("12.345");

a gets set to 12.000


If you forget to include a prototype for atof it will
default to return int and as we all know
(double)(int)1 2.345 == 12.000 or thereabouts.


So what? There is no (double)(int)12 .345 taking place in this code,
if the implicit declaration of atof is in effect.

BTW, there is no need for a prototype to fix the code, a good old
declaration is enough:

double atof();

But, of course, the *right* fix is to include <stdlib.h>.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #16
Da*****@cern.ch (Dan Pop) writes:
In <59************ *************@p osting.google.c om>
ln********@hotm ail.com (David Resnick) writes:

[...]
Perhaps your program is not running in the "C" locale. In which case the
character that represents the decimal point depends on the locale.


Nope:

6 In other than the "C" locale, additional locale-specific subject
^^^^^^^^^^
sequence forms may be accepted.

This means that '.' is accepted in ALL locales, but other locales can also
accept other characters as the "decimal point".


No, I don't think it does.

The standard defines the atof function as (nearly) equivalent to a
call to the strtod function.

The description of the strtod function (C99 7.20.1.3) says:

The expected form of the subject sequence is an optional plus or
minus sign, then one of the following:

-- a nonempty sequence of decimal digits optionally containing
a decimal-point character, then an optional exponent part
as defined in 6.4.4.2;

It uses the term "decimal-point character", not "period". A few
paragraphs later, it says:

If the subject sequence has the expected form for a floating-point
number, the sequence of characters starting with the first digit
or the decimal-point character (whichever occurs first) is
interpreted as a floating constant according to the rules of
6.4.4.2, except that the decimal-point character is used in place
of a period, [...]

(The wording in the C90 standard is similar; it's easier to
cut-and-paste from my copy of the C99 standard.)
dresnick(3276) $ cat foo.c
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
printf("%g\n", strtod("12.345" , NULL));
setlocale(LC_NU MERIC, "de_DE");
printf("%g\n", strtod("12.345" , NULL));
printf("%g\n", strtod("12,345" , NULL));
return 0;
}
dresnick(3277) $ gcc -Wall -ansi -pedantic -o foo foo.c
dresnick(3278) $ foo
12.345
12
12,345


Your library is broken in multiple places. The correct output of your
program consists of three *identical* lines because "12.345" is valid
input for strtod in *any* locale and because the current locale does
NOT affect the output of the %g conversion specifier (for reasons
that I do not understand, the Single Unix Specification is at odds
with the C standard here).


The description of the %e and %f conversion specifiers also uses the
term "decimal-point character", not "period". ("%g" is defined in
terms of "%e" and "%f".)

C99 7.1.1p2 says (underscores indicate italics):

The _decimal-point character_ is the character used by functions
that convert floating-point numbers to or from character sequences
to denote the beginning of the fractional part of such character
sequences. It is represented in the text and examples by a
period, but may be changed by the setlocale function.

Again, the wording in the C90 standard is similar.

For locales other than "C", the numeric conversion functions and
formatted I/O functions are not required to recognize '.' as a
decimal-point character.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #17
Da*****@cern.ch (Dan Pop) wrote in message news:<c4******* ****@sunnews.ce rn.ch>...
In <59************ *************@p osting.google.c om> ln********@hotm ail.com (David Resnick) writes:
eg*********@ho tmail.com (Marky C) wrote in message news:<9e******* *************** ****@posting.go ogle.com>...
atof is not working.

double a = atof("12.345");

a gets set to 12.000

I am working on a toshiba micro. The data map has no space allocated
to it for dynamic memory.

Does anyone have an idea? Could it be due to the lack of dynamic
memmor space?

All thoughts gratefully received

Marky C


Perhaps your program is not running in the "C" locale. In which case the
character that represents the decimal point depends on the locale.


Nope:

6 In other than the "C" locale, additional locale-specific subject
^^^^^^^^^^
sequence forms may be accepted.

This means that '.' is accepted in ALL locales, but other locales can also
accept other characters as the "decimal point".
As far as I know, it is always either a '.' or a ',', but that isn't
guaranteed. Example that demonstrates the issue using strtod, which
is preferable to atof:

dresnick(3276) $ cat foo.c
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
printf("%g\n", strtod("12.345" , NULL));
setlocale(LC_NU MERIC, "de_DE");
printf("%g\n", strtod("12.345" , NULL));
printf("%g\n", strtod("12,345" , NULL));
return 0;
}
dresnick(3277) $ gcc -Wall -ansi -pedantic -o foo foo.c
dresnick(3278) $ foo
12.345
12
12,345


Your library is broken in multiple places. The correct output of your
program consists of three *identical* lines because "12.345" is valid
input for strtod in *any* locale and because the current locale does
NOT affect the output of the %g conversion specifier (for reasons
that I do not understand, the Single Unix Specification is at odds
with the C standard here).

Dan

Well, the library (glibc) is behaving as documented for the library.
Guess it isn't conforming to the C-standard. Admittedly, I do have an
older glibc (not sure the version, I'm not at work now). The glibc
docs for strtod say:

....
A floating point number in decimal or hexadecimal format. The decimal
format is:

* A nonempty sequence of digits optionally containing a
decimal-point character--normally ., but it depends on the locale (see
General Numeric).
....

I see that N869 says this isn't correct. The standard says that
strtod needs to take a floating point constant as described by section
6.4.4.2, which does explicitly have the '.' character. And as you
say, locale setting can give it an additional options.

That said, the original poster's issue could be due to a similar
(perhaps the same?) broken library implementation.

-David
Nov 14 '05 #18
ln********@hotm ail.com (David Resnick) writes:
[...]
Well, the library (glibc) is behaving as documented for the library.
Guess it isn't conforming to the C-standard. Admittedly, I do have an
older glibc (not sure the version, I'm not at work now). The glibc
docs for strtod say:

...
A floating point number in decimal or hexadecimal format. The decimal
format is:

* A nonempty sequence of digits optionally containing a
decimal-point character--normally ., but it depends on the locale (see
General Numeric).
...

I see that N869 says this isn't correct. The standard says that
strtod needs to take a floating point constant as described by section
6.4.4.2, which does explicitly have the '.' character. And as you
say, locale setting can give it an additional options.


I think you're misreading N869. It says:

The expected form of the subject sequence is an optional plus or
minus sign, then one of the following:

-- a nonempty sequence of decimal digits optionally containing
a decimal-point character, then an optional exponent part
as defined in 6.4.4.2;

The phrase "as defined in 6.4.4.2" refers only to the optional
exponent part, not to the entire floating point costant. The fact
that it uses the term "decimal-point character" rather than "period"
makes it clear that it doesn't have to recognize a period. The term
"decimal-point character" is defined in 7.1.1p3:

The _decimal-point character_ is the character used by functions
that convert floating-point numbers to or from character sequences
to denote the beginning of the fractional part of such character
sequences. It is represented in the text and examples by a period,
but may be changed by the setlocale function.

The actual C99 standard has identical wording; the wording in the C90
standard is identical or very similar.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #19
Marky C <eg*********@ho tmail.com> scribbled the following:
atof is not working. double a = atof("12.345"); a gets set to 12.000 I am working on a toshiba micro. The data map has no space allocated
to it for dynamic memory. Does anyone have an idea? Could it be due to the lack of dynamic
memmor space?


Am I the only person who can't understand why the topic is about strtod
but the message is about atof?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
Nov 14 '05 #20

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

Similar topics

20
380
by: Marky C | last post by:
atof is not working. double a = atof("12.345"); a gets set to 12.000 I am working on a toshiba micro. The data map has no space allocated to it for dynamic memory. Does anyone have an idea? Could it be due to the lack of dynamic
9
1971
by: Adam Warner | last post by:
Hi all, Message ID <c1qo3f0tro@enews2.newsguy.com> is one of many informative articles by Chris Torek about C. The particular message discusses aliasing and concludes with this paragraph: Under these strict type-aliasing rules, casting from (e.g.) "int *" to "short *" is not only quite suspicious, it is also likely to cause puzzling behavior, at least if you expect your "short *" to access or modify your "int". Even the time-honored,...
18
2449
by: coder | last post by:
Hi experts, Is the following usage of strtod okay (p is a char pointer): value = strtod(p, &p); Is it possible that this would evoke undefined behaviour? Or should I use a temporary pointer and then assign its value to p? Thanks
0
8822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.