- Home /
How do i get the part of a float after the point?
What i want to do isn't too complex, i think. I want to create a weighted luck-based rounding algorithm. For example, feeding 1.5 into it, will have a 50% chance each way, of giving 1, or 2. Feeding 1.79 into it will have a 79% chance of returning 2, and a 21% chance of returning 1. In other words, the chance of rounding up is equal to how far it is towards the next integer.
The simplest way i can think of to do this would be to grab whatever is after the point, and generate a random number in the 0..1 range, round up if lower, round down if higher.
but how do i get that part? is there a better way than converting it to string and searching it?
is there a better way to achieve my goal that i'm missing?
Answer by _Gkxd · Apr 12, 2015 at 06:34 PM
The modulus (remainder) operator (%) works with floats too.
So decimalPart = yourFloat % 1
would be what you need.
Another option to go along with the $$anonymous$$odulus operator would be $$anonymous$$athf.Repeat.
float myVar = 3.14159;
myVar = $$anonymous$$athf.Repeat(myVar, 1.0f);
Debug.Log(myVar); // 0.14159
now this i like, its more compact and elegant than flooring or int casting.
Not that it matters, but is using % or repeat likely to be any faster than the other suggestions?
$$anonymous$$odulus is made to be fast, but it's not naturally supported by float values. Therefore, workarounds have been made to support the same functionality as well as possible, but they still won't be as fast as modulus operations on non-float values. $$anonymous$$athf.Repeat is probably the "most appropriate" way of doing this, but the easiest way to be sure what's fastest is to just create a loop and calculate something a few (hundred?) million times to see what's faster or slower to calculate.
Talking about how the speed of these types of operations is a bit of a moot point. You won't actually be able to tell the difference, since they are all sufficiently cheap, so it's essentially up to whatever you think looks better in code.
It's much more likely that other areas in a game are bottlenecks (e.g. searching through large lists, high-polygon mesh collisions, lighting calculations over large scenes, large image files, etc.), and identifying and making those bottlenecks faster is much more important than optimizing something like this. After all, if a high-poly mesh (or something else) is causing the lag, switching from modulus to some other operation won't fix the lag.
Answer by Eric5h5 · Apr 12, 2015 at 04:36 PM
Definitely don't use strings. You can just subtract the integer part.
Answer by Taziar · Dec 27, 2017 at 01:02 AM
I am assuming you are converting from Float to Int?
Value += Random.value; int value = (int) Value;
Random.Value will add a random value 0 - 1.0 to the number, possibly increasing it to the next integer. The higher the decimal starts, the more likely that is. Casting it will then strip the decimal. (or you could also use Mathf.Floor if you want to keep it a Float)
(if you want negative numbers to round away from zero...)
if (value >0) Value += Random.value; if (value <0) Value -= Random.value; int rounded = (int) value;
Yours is the correct answer.
Example: Value = 1.5; result is 1 as long as Random.value < 0.5, then it is 2 (1 - 0.5 => p = 0.5) Value = 1.79; result is 1 as long as Random.value < 0.21, then it is 2 (1 - 0.21 => p = 0.79)
Answer by GraySky · Apr 12, 2015 at 05:52 PM
The following should do what you want. As Eric said, no need to use strings.
public float GetMyRandom (float number)
{
float floored = Mathf.Floor (number);
float fractionalPart = number - floored;
float randNum = Random.Range (0, 1f);
if (randNum >= fractionalPart) {
return Mathf.Ceil (number);
} else {
return floored;
}
}
Answer by Gimos · Apr 12, 2015 at 05:53 PM
private int GodWill (float number) {
int p = (int)((number - (int)number) * 100);
if (p < (int)Random.Range (0, 100))
return (int)number + 1;
else
return (int)number;
}
Your answer
Follow this Question
Related Questions
Float to Int 4 Answers
Maths issue + Null reference error? -1 Answers
Why can't I get a floating point value here?... 1 Answer
[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers