Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ++ / -- operators with floats and doubles

Reply
Thread Tools

++ / -- operators with floats and doubles

 
 
Walter Roberson
Guest
Posts: n/a
 
      05-26-2005
In article <JCale.138456$(E-Mail Removed)>,
August Karlstrom <(E-Mail Removed)> wrote:

>If I change the example above to


You missed stdio.h

>int main(void)
>{
> float x = 0.0;
>
> while (x < x + 1) { x++; }
> fprintf(stdout, "x = %f\n", x);
> return 0;
>}


>the program doesn't seem to terminate. To me they seem to be more or
>less equivalent. Why does it behave differently?


Data point:

With the addition of stdio.h, it terminates for me on SGI IRIX,
using the native compiler or gcc. The termination point is 16777216.

--
History is a pile of debris -- Laurie Anderson
 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      05-26-2005
In article <JCale.138456$(E-Mail Removed)>,
August Karlstrom <(E-Mail Removed)> wrote:

> E. Robert Tisdale wrote:
> > Jonathan Bartlett wrote:
> >
> >>> Any time that you might want to increase the value of a float by one.
> >>> It could happen, right?
> >>
> >>
> >> I think the problem with floats is that, depending on the magnitude of
> >> the float, it may be a noop.

> >
> >
> > You think?
> >
> > > cat main.c

> > #include <stdio.h>
> >
> > int main(int argc, char* argv[]) {
> > float x = 0.0, y;
> > do {
> > y = x;
> > ++x;
> > } while (y < x);


Old value of x has type float. New value of x is calculated with
possibly higher precision than float, but is then rounded to type float.
On typical implementations, 2^24 + 1 cannot be represented as a number
of type float and will be rounded down to 2^24, so your loop will finish
when y = 2^24.

> > fprintf(stdout, "x = %f\n", x);
> > //do {
> > // y = x;
> > // } while (y < ++x);
> > //fprintf(stdout, "x = %f\n", x);
> > return 0;
> > }
> >
> > > gcc -Wall -std=c99 -pedantic -o main main.c
> > > ./main

> > x = 16777216.000000


As I thought. That is two to the 24th power.


>
> If I change the example above to
>
> int main(void)
> {
> float x = 0.0;
>
> while (x < x + 1) { x++; }
> fprintf(stdout, "x = %f\n", x);
> return 0;
> }



Increasing x is done using type float, so x will be stuck at 2^24, as
before. However, in the test "x < x+1", your compiler may evaluate x+1
with higher precision as float, for example double. In that case you
will always be comparing 2^24 < 2^24 + 1, which is always true. Your
loop doesn't finish, but x will be stuck on the same value.
 
Reply With Quote
 
 
 
 
August Karlstrom
Guest
Posts: n/a
 
      05-26-2005
E. Robert Tisdale wrote:
> August Karlstrom wrote:
>
>> If I change the example above to
>>
>> int main(void) {
>> float x = 0.0;
>>
>> while (x < x + 1) { x++; }
>> fprintf(stdout, "x = %f\n", x);
>> return 0;
>> }
>>
>> the program doesn't seem to terminate.
>> To me they seem to be more or less equivalent.
>> Why does it behave differently?

>
>
> I don't know.
> Which computer, operating system and compiler are you using?


I have an AMD Athlon XP machine with Fedora Core 3 and gcc 3.4.3.

> Which compiler options are you using?


gcc -Wall -o test test.c


-- August

 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      05-26-2005
Walter Roberson wrote:
> In article <JCale.138456$(E-Mail Removed)>,
> August Karlstrom <(E-Mail Removed)> wrote:
>
>
>>If I change the example above to

>
>
> You missed stdio.h


No I do include stdio.h, I just didn't show the whole file:

$ cat test.c
#include <stdio.h>

int main(void)
{
float x = 0.0;

while (x < x + 1) { x++; }

/* float x = 0.0, y; */
/* do { */
/* y = x; */
/* ++x; */
/* } while (y < x); */
/* fprintf(stdout, "x = %f\n", x); */
return 0;
}


-- August
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      05-26-2005
Christian Bau wrote:
>>If I change the example above to
>>
>>int main(void)
>>{
>> float x = 0.0;
>>
>> while (x < x + 1) { x++; }
>> fprintf(stdout, "x = %f\n", x);
>> return 0;
>>}

>
>
>
> Increasing x is done using type float, so x will be stuck at 2^24, as
> before. However, in the test "x < x+1", your compiler may evaluate x+1
> with higher precision as float, for example double. In that case you
> will always be comparing 2^24 < 2^24 + 1, which is always true. Your
> loop doesn't finish, but x will be stuck on the same value.


Okay, so in the first case we are comparing floats, x and y, and in the
second case we compare a float, x, to a number with higher precision, `x
+ 1'? Very subtle indeed.

-- August
 
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
Need a library to write/read floats and doubles in IEEE754 Pavel C++ 13 03-14-2008 04:01 PM
how do you convert and array of doubles into floats? SpreadTooThin Python 7 09-16-2006 01:14 AM
Re: Floats, doubles C and MSVC Andrew Reilly C Programming 2 10-14-2004 02:50 PM
Problems with multiplications of doubles and/or floats J.K. Becker C++ 43 04-23-2004 10:41 PM
floats doubles long doubles dan C++ 1 11-26-2003 05:12 AM



Advertisments