PDA

View Full Version : floating point operators

DavidP
10-21-2003, 01:44 AM
Why did they not extend the bitwise operators to include floating point values when they created C++?

For example, one way to swap any variable (not floating point) is to use 3 xor's.

#define swap (a,b) a ^= b ^= a ^= b

However that doesnt work for floating point values because bitwise operators dont work for floating point values.

Anybody have some insight on why that ability is not included in C++?

confuted
10-21-2003, 03:45 PM
Yeah, because of the way a float is represented in memory. Ummmm... STFW.

VirtualAce
10-21-2003, 03:59 PM
You cannot SHL or SHR for floating point. Basically because of what confuted said. They are represented totally different in memory. Now with MMX you can SHR and SHL on 64 bits (double is also 64-bits) but its still not floating point.

Besides both Intel and AMD have come up with some very fast multiplication circuitry and opcodes to handle floating point math. The FPU FMUL is nearly as fast, or faster than its integral counterpart.

Besides why would it have been easier???

Dave_Sinkula
10-21-2003, 04:53 PM
>#define swap (a,b) a ^= b ^= a ^= b

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

Question 10.3
How can I write a generic macro to swap two values?
--------------------------------------------------------------------------------
There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable (and the "obvious" supercompressed implementation for integral types a^=b^=a^=b is illegal due to multiple side-effects).

golfinguy4
10-21-2003, 05:19 PM
Originally posted by confuted
Yeah, because of the way a float is represented in memory. Ummmm... STFW.

Personally, I'd like to reverse that back at you.

Ass.

DavidP
10-21-2003, 10:55 PM
It doesnt matter how it is stored in memory. They are BITWISE operators. Each data type is composed of bits, and bitwise operators do operations on those single bits.

I know how floating point numbers are stored. I have studied them in CS class before. That does not mean that are incapable of having bitwise operators made for them.

I think it would be very wise to make bitwise operators for floating point numbers.

In my example above, no matter WHAT format the bits are in (unsigned int, int, long, double, float, char, etc.), three XORs will swap all the bits, and therefore, swap the variables. This would be very useful. I am sure many uses could be found for the other bitwise operators also on floating point numbers.

joshdick
10-21-2003, 11:02 PM
Maybe I'm missing something here, but why not just use the templated function swap()? It works, it's standard, it suffices, there's no reason to use macros in this situation, and it works.

DavidP
10-21-2003, 11:05 PM
The swap function is just an example. I know a swap function would work perfectly fine.

But bitwise operators should be in place for all base types.

Silvercord
10-22-2003, 09:51 AM
I've thought about the same thing, but I don't *really* know the internal workings of it. I feel hesitant to argue against these things because I assume there must be a good reason. What year of CS are you in David? I mean, if you are only first or second year then there's a good chance you just haven't been exposed to it enough (I don't know what you've done, I'm not attacking so there's no need to get defensive). If you've studied a lot then I just simply don't know.

EDIT this was a good discussion, the first link explains a lot:

confuted
10-22-2003, 01:46 PM
Originally posted by golfinguy4
Personally, I'd like to reverse that back at you.

Ass.
Yup, because I ask questions all the time, and I never use Google. Awesome.

DavidP
10-22-2003, 01:57 PM
>What year of CS are you in David?

hmm...good question...haha...lets see...i started programming in 8th grade...and im now a freshman in college...(i took formal CS classes throughout high school)

so i guess this would be my 6th year of CS

Salem
10-22-2003, 03:07 PM
> Why did they not extend the bitwise operators to include floating point values when they created C++?
Mostly because it doesn't make any sense - sure you can fiddle the bits, but the result doesn't make much sense as a floating point number.

#include <stdio.h>

int main ( ) {
union { float a; unsigned long b; } foo, bar;
foo.a = 1.234;
bar.a = 4.567;
foo.b ^= bar.b;
printf( "Result=%f\n", foo.a );
return 0;
}
Result=191198117439655548494113828686759198720.000 000
Go figure....

It's strange the way people cling to the xor trick of swapping a pair of values, like its super efficient or magical in some way when in reality it isn't.

Here's a case in point

void s1 ( int a, int b ) {
a ^= b;
b ^= a;
a ^= b;
printf( "Swapped = %d, %d\n", a, b );
}
void s2 ( int a, int b ) {
int t = a;
a = b;
b = t;
printf( "Swapped = %d, %d\n", a, b );
}

Now with gcc -O, the first function still contains 3 xor operations.
In the 2nd function the swap has been completely eliminated by reducing the code to printf( "Swapped = %d, %d\n", b, a );
All you've managed to do is confuse the compiler just as much as the average reader.

So do yourself a favour and stick to the obvious code, then perhaps the compiler can make a half-decent job of optimising the code if there is an optimisation to be made.

Of course, since you (inaccurately) mentioned C++, you could always overload the ^ operator and implement something yourself. The integer only rules for bitwise operators came from C.

golfinguy4
10-22-2003, 05:17 PM
Originally posted by confuted
Yup, because I ask questions all the time, and I never use Google. Awesome.

Yeah, I misread it. I wanted to change the last letter of STFW to a U.

no-one
10-22-2003, 07:26 PM
and the XOR was not only meant for swapping...
like Salem said, if you think about it you'll realize XOR'ing a float is meaningless anyway...

sure it would be handy for special cases, but it does not warrant the inclusion of float XOR in the language.

>
>#define swap (a,b) a ^= b ^= a ^= b

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

<

its called, use the comma operator:

x ^= y, x^= y ^= x;

>or if the two values are the same variable

did i miss something? or what?

x 00010001
y ^ 00010001
x = ________________
x 00000000
y ^ 00010001
y = ________________
y 00010001
x ^ 00000000
x = ________________
x 00010001

you'll note even though they equal the values are still effectively swapped...

>
Of course, since you (inaccurately) mentioned C++, you could always overload the ^ operator and implement something yourself.
<

not quite you can't define operators for built in types...
overloaded operators must have an object of some kind as an arguement.

Dave_Sinkula
10-22-2003, 07:44 PM
>did i miss something? or what?

Give it a try.
#include <stdio.h>

#define swap(x,y) x ^= y, x^= y ^= x

int main()
{
int i = 17;
swap(i, i);
printf("i = %d\n", i);
return 0;
}

/* my output
i = 0
*/Honestly, I don't know why people fight this so hard.

no-one
10-22-2003, 07:53 PM
my apology, i missed on thing, eh hem, im stupid, sorry... i misread what it said, i thought it said value... hehe... eh... hehe

my mom said i hit my head a lot when i was a kid... guess this is proof...