Advertisement

negative floats

Started by June 10, 2000 11:49 PM
8 comments, last by jLeslie 24 years, 6 months ago
If I need to know the absolute value of a float can I just change the value of the byte that stores the positive negative sign? (I know I can, I just want to know if its always stored in the same byte and what not, and how do I go about affecting the value? Or would it be faster to typecast it to an unsigned float?)
I think the most significant bit is the sign bit in a float, correct me if I''m wrong. The sign is controlled by a bit however. So to get absolute value, you can:

float fValue = -3.4f;
fValue = *(float)((*(unsigned long*)&fValue) / 0x8000000);

I''m not sure how else to get fValue so that you can use bit operators on it, that''s why there are all those ugly casts.

Advertisement
I don''t think bitwise operators work on floats and doubles.

Anyway, you can use this functions:

    double abs_f(double f){    if(f < 0) f = (f - f - f);    return f;}    


It takes an double/float and will return its absolute value. If you don''t want to use my function, check out the standard function fabs()

/. Muzzafarath
Mad House Software
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
even better is to use fabsf() which returns an float, so you don''t have the trouble of converting from double to float (or the CPU doesn''t need to switch to double precision).

Lars
--------> http://www.larswolter.de <---------
Muzzafarath: Why use f-f-f? Is 0-f or just -f not faster? What am I missing here?

Another version:

        float my_abs(float f){    if ((*(unsigned int *)&f) & 0x80000000)    {    return -f;    }    else    {    return f;    }}        




Edited by - baskuenen on June 12, 2000 10:50:32 AM
quote: Original post by baskuenen

Muzzafarath: Why use f-f-f? Is 0-f or just -f not faster? What am I missing here?

Another version:

                        float my_abs(float f){    if (((unsigned int *)&f) & 0x80000000)    {    return -f;    }    else    {    return f;    }}        




Your function won't work since bitwise operators (& ^ / ~) doesn't work on floating point variables. Casting the float to an int doesn't help (at least it doesn't in gcc and gpp).

The reason I used f-f-f is because I wrote my function ut of my head, I didn't really think about other solutions you might have that might be faster. But at least my function works (just kidding ).

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 11, 2000 1:30:53 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
May I share my opinion?
Only use:

        float my_abs(float f){    return (f & 0x7FFFFFFF);}#define def_abs(f) ((f) & 0x7FFFFFFF)        



Edited by - Poltras on June 11, 2000 1:18:05 PM
Now I know what I'm made of, and I'm afraid of it...
quote: Original post by Poltras

May I share my opinion?
Only use:

            float my_abs(float f){    return (f & 0x7FFFFFFF);}#define def_abs(f) ((f) & 0x7FFFFFFF)            



Your function won''t work since bitwise operators (that includes & ^ / and ~) does NOT work on floating point values (floats and doubles). When I try your function in gcc and gpp I get an "invalid operands to binary &" error.

/. Muzzafarath
Mad House Software
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Sorry you're right...

But this works under VC6 (and is much faster than f=f<0?-f:f):
                void fabs(float* f){	(*((long*)f)) &= 0x7fffffff;}            


You can even make it inline to accelerate...

Edited by - Poltras on June 11, 2000 3:09:28 PM
Now I know what I'm made of, and I'm afraid of it...
Thanks guys.

This topic is closed to new replies.

Advertisement