floating point operators

This is a discussion on floating point operators within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Why did they not extend the bitwise operators to include floating point values when they created C++? It would be ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Question floating point operators

    Why did they not extend the bitwise operators to include floating point values when they created C++?

    It would be very helpful.

    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++?
    My Website

    "Circular logic is good because it is."

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yeah, because of the way a float is represented in memory. Ummmm... STFW.

    Start here
    Away.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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???

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >#define swap (a,b) a ^= b ^= a ^= b

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

    [EDIT]
    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).
    [/EDIT]
    Last edited by Dave_Sinkula; 10-22-2003 at 02:39 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Originally posted by confuted
    Yeah, because of the way a float is represented in memory. Ummmm... STFW.

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

    Ass.

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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.
    My Website

    "Circular logic is good because it is."

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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.
    My Website

    "Circular logic is good because it is."

  9. #9
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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:
    Understanding floating point
    Last edited by Silvercord; 10-22-2003 at 09:54 AM.

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    Away.

  11. #11
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    >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
    Last edited by DavidP; 10-22-2003 at 02:16 PM.
    My Website

    "Circular logic is good because it is."

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > 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.

    Code:
    #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.000000
    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
    Code:
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    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.

  14. #14
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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?

    Code:
    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.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >did i miss something? or what?

    Give it a try.
    Code:
    #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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. strtol with floating point
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 07-26-2004, 06:02 AM
  4. Understanding floating point
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-11-2003, 03:58 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21