473,668 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

modf() - behavior

modf() is a C Standard Library function
double modf(double x, double* ip);
returns fractional part and assigns to *ip integral part of x, both with
same sign as x
I have encountere a situation that has me a bit confused on the behaviot of
this function.

When calling the function with a value that DOES NOT have a fractional
portion - the integral part is no always what is expected. I have seen this
on two compilers - and compiler used for an Hitachi SH* processor and
MSCVC6.00

Here is a test program (MSVC6.00) that demonstrates the behavior:

#include <math.h>

void modftest(void)
{
double dblwhole;
double dblValue = 0.000;

for (int i = 0; i < 10000; i++)
{
modf(dblValue, &dblwhole);
if((i%1000) == 0)
{
cout << dblValue << " " <<dblwhole << endl;
//_beep(1200, 100);
_sleep(1000);
}
dblValue += 0.001;
}
}
Output:
1 1
2 1
3 2
4 3
5 5
6 6
7 7
8 8
9 9

This is not the expected behavior: If you increase the loop count to
100,000, there are other occurences where the
integer portion is 1 less than what it should be!
I don't think this is correc behavior...
Any thoughts?

MarkN

Nov 14 '05 #1
3 2348
MarkN wrote:
modf() is a C Standard Library function
double modf(double x, double* ip);
returns fractional part and assigns to *ip integral part of x, both with
same sign as x
I have encountere a situation that has me a bit confused on the behaviot of
this function.

When calling the function with a value that DOES NOT have a fractional
portion - the integral part is no always what is expected. I have seen this
on two compilers - and compiler used for an Hitachi SH* processor and
MSCVC6.00

Here is a test program (MSVC6.00) that demonstrates the behavior:

#include <math.h>

void modftest(void)
{
double dblwhole;
double dblValue = 0.000;

for (int i = 0; i < 10000; i++)
{
modf(dblValue, &dblwhole);
if((i%1000) == 0)
{
cout << dblValue << " " <<dblwhole << endl;
//_beep(1200, 100);
_sleep(1000);
}
dblValue += 0.001;
}
}
Output:
1 1
2 1
3 2
4 3
5 5
6 6
7 7
8 8
9 9

This is not the expected behavior: If you increase the loop count to
100,000, there are other occurences where the
integer portion is 1 less than what it should be!
I don't think this is correc behavior...
Any thoughts?


Thought the first: You're in the wrong newsgroup.
comp.lang.c++ is just down the hall, in that room where
four New Age bands and the Vienna Philharmonic are all
trying to play simultaneously.

Thought the second: Your problem has nothing to do with
modf(), is not specific to C++ or to C, and has been seen
before. Frequently, in fact: it is Question 14.1 in the
comp.lang.c Frequently Asked Questions list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun .com

Nov 14 '05 #2
I did in fact read 14.1 of FAQ. That would explain a problem in the
fractional portion...
This behavior "problem" is in the integer portion,

MarkN
"Eric Sosman" <Er*********@su n.com> wrote in message
news:41******** ******@sun.com. ..
MarkN wrote:
modf() is a C Standard Library function
double modf(double x, double* ip);
returns fractional part and assigns to *ip integral part of x, both with
same sign as x
I have encountere a situation that has me a bit confused on the behaviot of this function.

When calling the function with a value that DOES NOT have a fractional
portion - the integral part is no always what is expected. I have seen this on two compilers - and compiler used for an Hitachi SH* processor and
MSCVC6.00

Here is a test program (MSVC6.00) that demonstrates the behavior:

#include <math.h>

void modftest(void)
{
double dblwhole;
double dblValue = 0.000;

for (int i = 0; i < 10000; i++)
{
modf(dblValue, &dblwhole);
if((i%1000) == 0)
{
cout << dblValue << " " <<dblwhole << endl;
//_beep(1200, 100);
_sleep(1000);
}
dblValue += 0.001;
}
}
Output:
1 1
2 1
3 2
4 3
5 5
6 6
7 7
8 8
9 9

This is not the expected behavior: If you increase the loop count to
100,000, there are other occurences where the
integer portion is 1 less than what it should be!
I don't think this is correc behavior...
Any thoughts?


Thought the first: You're in the wrong newsgroup.
comp.lang.c++ is just down the hall, in that room where
four New Age bands and the Vienna Philharmonic are all
trying to play simultaneously.

Thought the second: Your problem has nothing to do with
modf(), is not specific to C++ or to C, and has been seen
before. Frequently, in fact: it is Question 14.1 in the
comp.lang.c Frequently Asked Questions list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun .com

Nov 14 '05 #3
MarkN <mn*****@aol.co m> wrote:

Please don't top-post, thanks.
"Eric Sosman" <Er*********@su n.com> wrote in message
news:41******** ******@sun.com. ..
MarkN wrote:
> modf() is a C Standard Library function
> double modf(double x, double* ip);
> returns fractional part and assigns to *ip integral part of x, both with
> same sign as x
> I have encountere a situation that has me a bit confused on the behaviot of > this function.
>
> When calling the function with a value that DOES NOT have a fractional
> portion - the integral part is no always what is expected. I have seen this > on two compilers - and compiler used for an Hitachi SH* processor and
> MSCVC6.00
>
> Here is a test program (MSVC6.00) that demonstrates the behavior:
>
> #include <math.h>
>
> void modftest(void)
> {
> double dblwhole;
> double dblValue = 0.000;
>
> for (int i = 0; i < 10000; i++)
> {
> modf(dblValue, &dblwhole);
> if((i%1000) == 0)
> {
> cout << dblValue << " " <<dblwhole << endl;
> //_beep(1200, 100);
> _sleep(1000);
> }
> dblValue += 0.001;
> }
> }
> Output:
> 1 1
> 2 1
> 3 2
> 4 3
> 5 5
> 6 6
> 7 7
> 8 8
> 9 9
>
> This is not the expected behavior: If you increase the loop count to
> 100,000, there are other occurences where the
> integer portion is 1 less than what it should be!
> I don't think this is correc behavior...
> Any thoughts?
Thought the first: You're in the wrong newsgroup.
comp.lang.c++ is just down the hall, in that room where
four New Age bands and the Vienna Philharmonic are all
trying to play simultaneously.

Thought the second: Your problem has nothing to do with
modf(), is not specific to C++ or to C, and has been seen
before. Frequently, in fact: it is Question 14.1 in the
comp.lang.c Frequently Asked Questions list

http://www.eskimo.com/~scs/C-faq/top.html

I did in fact read 14.1 of FAQ. That would explain a problem in the
fractional portion...


You don't print any "fractional part (which is the return value of
modf()), you just throw it away...
This behavior "problem" is in the integer portion,


No, there isn't. Since 0.001 can't be expressed in binary without
losing some precision you sometimes will end up a little below
or above a "flat" number and the integral part of let's say
2.9999999999999 9999 is still 2 and not 3, as is 3.000000000001.
But if you print out the fractional and the integral part you
will see that their sums are always the expected numbers...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #4

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

Similar topics

19
2568
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can happen" if your C code invokes "undefined behavior". Behavior not defined by the ANSI/ISO C 9 standard may be defined by some other standard (i.e. POSIX) or it may be defined by your compiler, your operating system or your machine architecture.
23
3190
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64 bit machines, i.e. with 64 bit pointers. In the early days of C, where there were problems with the size of int being 16 or 32 bits, the response was that an int was guaranteed to hold a pointer (yes, there were 64Kb address spaces at one time!)....
3
1622
by: Christopher Benson-Manica | last post by:
Given that modf is prototyped as double modf( double x, double *iptr ); is the behavior of the function well-defined if the address of x is passed as the value of iptr? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
1
11653
by: mcruz | last post by:
hi, I was using the following function in a C++ program. I am now "porting" it to C# and I can not find an equivalent to: modf function. As described below. double modf ( double x , double * ipart ); Split floating-point value into fractional and integer parts. Breaks x in two parts: the integer (stored in location pointed by ipart) and the fraction (return value). So does any know of a function that i can use to find the...
1
2516
by: Patrick Sullivan | last post by:
Is there any VB function that can do what modf() does in Visual C/C++, like Modf: Splits a floating-point value into fractional and integer parts. The integer portion is stored in *intptr (address of the variable) and the fraction is returned as the variable x. double modf( double x, double *intptr );
12
3052
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the difference. For example: Consider the following code: a = i; We say that the above expression statement produces undefined
28
2533
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
12
4938
by: bsabiston | last post by:
Hi, I'm trying to get the fractional part of a floating point number. I've tried fmod() and modf(), and while both do work, they also occasionally return 1.0 for the fractional part of the number instead of 0.0. This is on a mac using xcode. This should not ever happen should it? Thanks Bob
33
2824
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined. I am having some confusion with the former statement! Also, state the reason for the statement being undefined!
0
8371
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
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
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
8652
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...
1
6206
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
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2782
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
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.