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

9 replies to this topic

### #1BaneTrapper  Members   -  Reputation: 1484

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.

### #2Vortez  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;
}


### #3Brother Bob  Moderators   -  Reputation: 10026

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.

### #4dejaime  Crossbones+   -  Reputation: 4157

Like
1Likes
Like

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

Like
0Likes
Like

Posted 22 April 2014 - 06:42 AM

### #6Erik Rufelt  Crossbones+   -  Reputation: 5897

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.

### #7Lactose!  GDNet+   -  Reputation: 9351

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Álvaro  Crossbones+   -  Reputation: 19901

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.

### #9Lactose!  GDNet+   -  Reputation: 9351

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.

### #10BaneTrapper  Members   -  Reputation: 1484

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 .

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