Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > modf() - behavior

Reply
Thread Tools

modf() - behavior

 
 
MarkN
Guest
Posts: n/a
 
      08-18-2004
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



 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-18-2004
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

--


 
Reply With Quote
 
 
 
 
MarkN
Guest
Posts: n/a
 
      08-18-2004
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" <> wrote in message
news:...
> 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
>
> --
>
>



 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      08-18-2004
MarkN <> wrote:

Please don't top-post, thanks.

> "Eric Sosman" <> wrote in message
> news:...
>> 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.99999999999999999 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 ___
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VERY odd routing behavior when attempting VPN connections over Wifi Robert Gordon Wireless Networking 0 08-25-2005 04:04 PM
Question about behavior of View Available Networks window mszablocky Wireless Networking 4 07-24-2005 08:28 PM
Firefox under Linux -- odd behavior Dennis J. Tuchler Firefox 0 07-28-2004 04:05 PM
[FF] middle button behavior Gillou Firefox 0 05-01-2004 08:46 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57