By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,612 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,612 IT Pros & Developers. It's quick & easy.

++ / -- operators with floats and doubles

P: n/a
Yesterday I found in a piece of code a float being incremented with ++.
For some reason (wrong, I guess...) I thought that only integer types
could be incremented that way, and I had never seen floats or doubles
incremented that way before. Just to be sure I did a toy program and
compiled with gcc -ansi -pedantic -Wall and it worked without any error
or warning, showing the correct result.
Is it correct to increment floats or doubles with ++ and --? Is it
defined or am I getting into the realm of undefined behaviour? If it's
correct, can you think of a place where it could be useful?
Many thanks for your time,
Al

Nov 14 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
my**************@gmail.com wrote:
Is it correct to increment floats or doubles with ++ and --? Is it
defined or am I getting into the realm of undefined behaviour?
Any scalar type may be incremented.
You can also increment pointers.
If it's
correct, can you think of a place where it could be useful?


Any time that you might want to increase the value of a float by one.
It could happen, right?

--
pete
Nov 14 '05 #2

P: n/a
> 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.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #3

P: n/a

"Jonathan Bartlett" <jo*****@eskimo.com> 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.

The other problem is that adding one isn't the same as incrementing.
Incrementing is what you do when you count, adding one is adding a constant
that happens to be unity.
So if I am counting spectators as they pass throught he turnstiles to a
football match, it makes sense to say
count++
every time someone passes through.
On the other hand current rules are that a win is three points, whilst a
draw is one point, and a loss no points. It is not right to say
if(goalsfor == goalsagainst)
points++;
It should be
points += 1;
To make clear that 1 is an externally-defined constant that could change.
The rules could change so that a draw scores zero whilst a loss is minus
one, for example.

Floating point values aren't generally used for counting, that's what we
have integers for. So it doesn't usually make sense to increment them.
Nov 14 '05 #4

P: n/a
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.


float a, b;

a = 0.0;
do {
b = a++;
} while (a != b);
printf("%f\n", a);

and time its execution. It will terminate on any machine known to
me. Then change 'float' to 'double' and try again.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5

P: n/a
Malcolm wrote:
The other problem is that adding one isn't the same as incrementing.
In C, it is.
Floating point values aren't generally used for counting,
that's what we have integers for.
So it doesn't usually make sense to increment them.


I agree.

--
pete
Nov 14 '05 #6

P: n/a
In message <42***********@mindspring.com>
pete <pf*****@mindspring.com> wrote:
my**************@gmail.com wrote:
Is it correct to increment floats or doubles with ++ and --? Is it
defined or am I getting into the realm of undefined behaviour?


Any scalar type may be incremented.


Except complex and imaginary types.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
3 Signet Court, Swann Road, Fax: +44 (0) 1728 727430
Cambridge, CB5 8LA, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #7

P: n/a
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);
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
Nov 14 '05 #8

P: n/a
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);
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


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?

-- August
Nov 14 '05 #9

P: n/a
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?
Which compiler options are you using?

On my machine, x + 1 (and x < x + 1)
is evaluated using extended precision arithmetic
but x is rounded and stored as double precision
so x increments to 16777216.0 then never changes.
Nov 14 '05 #10

P: n/a
In article <JC*********************@newsc.telia.net>,
August Karlstrom <fu********@comhem.se> 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
Nov 14 '05 #11

P: n/a
In article <JC*********************@newsc.telia.net>,
August Karlstrom <fu********@comhem.se> 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.
Nov 14 '05 #12

P: n/a
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

Nov 14 '05 #13

P: n/a
Walter Roberson wrote:
In article <JC*********************@newsc.telia.net>,
August Karlstrom <fu********@comhem.se> 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
Nov 14 '05 #14

P: n/a
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
Nov 14 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.