Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Brilliant! Your site is great...and saving me hours of time at work and making my boss think I am brilliant too! I also picked up on a thread that will potentially save us a lot of money in the future..."

Geography

Where in the world do Tek-Tips members come from?

Converting from positive flaot to negative float

titanzero (Programmer)
9 Aug 07 16:21
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
uolj (Programmer)
9 Aug 07 16:53
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';
}
titanzero (Programmer)
10 Aug 07 7:14
lol...I knew there would be an easy solution. Thanks very much!

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!

Back To Forum

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