Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to get float value that is less then 1 without the higher part


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1   Members   -  Reputation: 1473

Like
0Likes
Like

Posted 22 April 2014 - 06:35 AM

Hello.

I am kinda rusty in language so il just make example code;

I need this

float number = 1.332;
float newNumber = RemoveBigNumber(number);
//newNumber = 0.332;

what is the std function for such a method? i cant find it in

http://www.cplusplus.com/reference/cmath/


Edited by BaneTrapper, 22 April 2014 - 06:38 AM.


#2   Crossbones+   -  Reputation: 2705

Like
3Likes
Like

Posted 22 April 2014 - 06:39 AM

Just do something like this

float RemoveIntegerPart(float n)
{
    return n - (int)n;
}


#3   Moderators   -  Reputation: 10002

Like
3Likes
Like

Posted 22 April 2014 - 06:39 AM

Look at it from a different angle instead: how do you obtain the integer part so that you can subtract it from the original number? You can cast the float to an integer, or using the floor function, for example.



#4   Crossbones+   -  Reputation: 4157

Like
1Likes
Like

Posted 22 April 2014 - 06:40 AM

float RemoveWholePart (float N) {
    return N - (int)N;
}

ph34r.png can't delete it.


Edited by dejaime, 22 April 2014 - 06:41 AM.


#5   Crossbones+   -  Reputation: 2705

Like
0Likes
Like

Posted 22 April 2014 - 06:42 AM

ph34r.png



#6   Crossbones+   -  Reputation: 5893

Like
8Likes
Like

Posted 22 April 2014 - 06:43 AM

*
POPULAR

modf returns the fractional and integer part of a floating point number.

If you do it with subtraction then remember to check that it gives you the answer you want for negative numbers.


Edited by Erik Rufelt, 22 April 2014 - 06:44 AM.


#7   GDNet+   -  Reputation: 9128

Like
0Likes
Like

Posted 22 April 2014 - 06:57 AM

With c++11:

 

float number = 1.332;

float truncatedNumber = trunc(number);

float wantedNumber = number - truncatedNumber;


Hello to all my stalkers.


#8   Crossbones+   -  Reputation: 19680

Like
6Likes
Like

Posted 22 April 2014 - 07:13 AM

*
POPULAR

float mantissa(float x) {
  return x - std::floor(x);
}

 

I can't think of any circumstances where you want the answer to be outside of [0,1), so I wouldn't use truncation. Casting to int has even worse problems, since you also have to worry about values larger than what an int can represent.



#9   GDNet+   -  Reputation: 9128

Like
0Likes
Like

Posted 22 April 2014 - 07:37 AM


I can't think of any circumstances where you want the answer to be outside of [0,1), so I wouldn't use truncation.

True -- if negative numbers don't matter, floor would be preferred instead of trunc.


Hello to all my stalkers.


#10   Members   -  Reputation: 1473

Like
0Likes
Like

Posted 03 May 2014 - 03:44 PM

modf returns the fractional and integer part of a floating point number.

If you do it with subtraction then remember to check that it gives you the answer you want for negative numbers.

This is the solution i ended up using.

Thank you on help and i kinda forgot i made this topic mellow.png .






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS