• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# 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.

9 replies to this topic

### #1BaneTrapper  Members

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.

### #2Vortez  Members

Posted 22 April 2014 - 06:39 AM

Just do something like this

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

### #3Brother Bob  Moderators

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.

### #4dejaime  Members

Posted 22 April 2014 - 06:40 AM

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

can't delete it.

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

### #5Vortez  Members

Posted 22 April 2014 - 06:42 AM

### #6Erik Rufelt  Members

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.

### #7Lactose  GDNet+

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Álvaro  Members

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.

### #9Lactose  GDNet+

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.

### #10BaneTrapper  Members

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 .

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.