Board index » cppbuilder » Serious problems with std::modf()

Serious problems with std::modf()

2005-02-12 11:37:16 PM
Hi group.
I have a serious problem concerning the function std::modf() from the
<cmath>header. I get the problem when compiling in BCB 6 Pro, but not
in g++ under Unix. The program looks as:
#pragma hdrstop
#include <cmath>
#include <iostream>
#pragma argsused
int main(int argc, char* argv[])
for (int i = 0; i<5; ++i)
float dummy;
float f = std::modf (1.33, &dummy);
std::cout << " i =" << i << std::endl;
return 0;
Simple enough, i would expect that the program would output
i =0
i =1
i =2
i =3
i =4
to the screen, but it doesn't, instead it outputs
i =1072693248
Can anyone here explain why? Debugging the program shows that the value
of i changes form 0 to 1072693248 when modf() is executed. I can make
the program act as expected by doing any of the following:
1) changing the header from <cmath>to the (non-standard?) header <math>
2) changing the type of 'dummy' to double
3) conmmenting out the line containing std::modf()
and finaly
4) compiling the program as it is in g++ under linux
What have I done wrong here? And how can a function call to modf()
change the value of the variable i?
Best regards.

Re:Serious problems with std::modf()

Realizing that I may have posted in the wrong newsgroup, I will repost
this in borland.public.cppbuilder.language.cpp

Re:Serious problems with std::modf()

hall < XXXX@XXXXX.COM >writes:
Well, I've installed the patch for both compiler and linker but the
problem remains. I did discover that running the command-line
compiler, everything works fine, whereas compiling and running through
the IDE (pressing F9) causes this strange problem.

>As I understand it this is the include for the C version of the math
>#include <cmath>
>and this is the standard include for C++.
>#include <math>

Is this so? I thought that <cxxxx>are the C++ versions of the
corresponding C header <xxxx.h>, wrapped in the std namespace.
This is correct. Further, the C variations don't "exist" in the
specification of the C++ language, even though they're usually there
in most implementations. The <cxxxx>version is the only way to
include the C standard library headers according to standard C++.
And I also thought that <math>is the same as <math.h>.
No. <math>does not exist at all according to the C++ standard.
<math.h>is for C, <cmath>is for C++, and <math>is invalid but
happens to compile with Borland due to the way that they #include
files (first trying to pull in the header as spelled verbatim, then
modifying it to have a trailing .h if the first search failes. This
behavior is implementation-dependent, and not common to other
Looking at this problem, it seems that there is a
std::modf(float,*float) defined in <cmath>whereas it doesnt exist
in <math>(as you pointed out), so it seem that <cmath>is not only
the math-functions tucked into std? Loocking at the header file
doesn't tell me anything of value:
They certainly can be different... the standard only requires that
certain functionality be present when you #include a standard header.
It does not require that ONLY the specified behavior is provided, just
that AT LEAST the required behavior is enabled. (So vendors are
allowed to pull in more stuff than you expect when you include a file.)
#define __USING_CNAME__
#include <math.h>
#undef __USING_CNAME__

Not being a friend of the preprocessor and macros yet, i can't even
spot the std namespace...
Please enlighten me if possible.
It's probably inside math.h file, depending on whether __USING_CNAME__
is defined.
Chris (TeamB);