Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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 BaneTrapper   Members   -  Reputation: 1332

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.


Sponsor:

#2 Vortez   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 Brother Bob   Moderators   -  Reputation: 9250

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 dejaime   Crossbones+   -  Reputation: 4119

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 Vortez   Crossbones+   -  Reputation: 2705

Like
0Likes
Like

Posted 22 April 2014 - 06:42 AM

ph34r.png



#6 Erik Rufelt   Crossbones+   -  Reputation: 4223

Like
8Likes
Like

Posted 22 April 2014 - 06:43 AM

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 Lactose!   GDNet+   -  Reputation: 4836

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;


Project journal, check it out!

http://www.gamedev.net/blog/1830-lactoses-journal/

 

Hello to all my stalkers.


#8 Álvaro   Crossbones+   -  Reputation: 15400

Like
6Likes
Like

Posted 22 April 2014 - 07:13 AM

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 Lactose!   GDNet+   -  Reputation: 4836

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.


Project journal, check it out!

http://www.gamedev.net/blog/1830-lactoses-journal/

 

Hello to all my stalkers.


#10 BaneTrapper   Members   -  Reputation: 1332

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