Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting from positive flaot to negative float

Status
Not open for further replies.

titanzero

Programmer
May 3, 2007
30
0
0
EU
Hello everybody

I've been tring to find a function or method for converting a posative float like -45.67 to a possative float (45.67). Using the NOT bitwise operator I can convert ints and using the abs function I can convert a negative float to a positive float but it won't work the other way.

I was thinking that I could take a possative number double it and then take that away from the original to get a negative number. E.G if i had the number 14.5 I could double it (29) and then take that from 14.5 and get -14.5 however I don't think this will work for numbers larger than half the maximum range of a float, as the doubling acion will give a number to large for the flaot to hold.

So my question is does any body have siggestions or information on how I might go about changing between posative and negative floats?

Thanks for you time
Andrew
 
For ints, doubles, or any other numeric type, just use operator-

Code:
#include <iostream>

int main()
{
    double d = -3.0;
    std::cout << d << '\n';
    d = -d;
    std::cout << d << '\n';
    d = -d;
    std::cout << d << '\n';
}
 
lol...I knew there would be an easy solution. Thanks very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top