×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Converting from positive flaot to negative float

Converting from positive flaot to negative float

Converting from positive flaot to negative float

(OP)
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

RE: Converting from positive flaot to negative float

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';
}

RE: Converting from positive flaot to negative float

(OP)
lol...I knew there would be an easy solution. Thanks very much!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close