Merge pull request #26761 from Chaosus/fix_mono_wrap

Fix division by zero at wrap functions in mono
This commit is contained in:
Ignacio Etcheverry 2019-03-07 21:34:03 +01:00 committed by GitHub
commit 47407ba2df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -289,13 +289,13 @@ namespace Godot
public static int Wrap(int value, int min, int max) public static int Wrap(int value, int min, int max)
{ {
int rng = max - min; int rng = max - min;
return min + ((value - min) % rng + rng) % rng; return rng != 0 ? min + ((value - min) % rng + rng) % rng : min;
} }
public static real_t Wrap(real_t value, real_t min, real_t max) public static real_t Wrap(real_t value, real_t min, real_t max)
{ {
real_t rng = max - min; real_t rng = max - min;
return min + ((value - min) % rng + rng) % rng; return !IsEqualApprox(rng, default(real_t)) ? min + ((value - min) % rng + rng) % rng : min;
} }
} }
} }

View file

@ -35,5 +35,10 @@ namespace Godot
{ {
return (int)Math.Round(s); return (int)Math.Round(s);
} }
public static bool IsEqualApprox(real_t a, real_t b, real_t ratio = Mathf.Epsilon)
{
return Abs(a - b) < ratio;
}
} }
} }