-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use generic math for bindable numbers #6248
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a85230f
Rewrite BindableNumber in stable INumber<T>
huoyaoyuan f22376f
Update dependents constraints to generic math
huoyaoyuan 84df9e2
Use T.One for integers
huoyaoyuan d699d51
Generic constraint for test scene
huoyaoyuan aa81aa7
Minor fixes
huoyaoyuan bc3cfda
Fix `RangeConstrainedBindable` throwing on binding
bdach 8142201
Merge branch 'master' into net7.0/generic-math
bdach 339f778
Undo semantics change in bound check
bdach 1a30d36
Merge branch 'master' into net7.0/generic-math
smoogipoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,14 @@ | |
#nullable disable | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Numerics; | ||
using JetBrains.Annotations; | ||
using osu.Framework.Extensions.TypeExtensions; | ||
using osu.Framework.Utils; | ||
|
||
namespace osu.Framework.Bindables | ||
{ | ||
public class BindableNumber<T> : RangeConstrainedBindable<T>, IBindableNumber<T> | ||
where T : struct, IComparable<T>, IConvertible, IEquatable<T> | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
{ | ||
[CanBeNull] | ||
public event Action<T> PrecisionChanged; | ||
|
@@ -40,10 +38,10 @@ public T Precision | |
get => precision; | ||
set | ||
{ | ||
if (precision.Equals(value)) | ||
if (precision == value) | ||
return; | ||
|
||
if (value.CompareTo(default) <= 0) | ||
if (value <= T.Zero) | ||
throw new ArgumentOutOfRangeException(nameof(Precision), value, "Must be greater than 0."); | ||
|
||
SetPrecision(value, true, this); | ||
|
@@ -76,102 +74,22 @@ public override T Value | |
|
||
private void setValue(T value) | ||
{ | ||
if (Precision.CompareTo(DefaultPrecision) > 0) | ||
if (Precision > DefaultPrecision) | ||
{ | ||
// this rounding is purposefully performed on `decimal` to ensure that the resulting value is the closest possible floating-point | ||
// number to actual real-world base-10 decimals, as that is the most common usage of precision. | ||
decimal accurateResult = ClampValue(value, MinValue, MaxValue).ToDecimal(NumberFormatInfo.InvariantInfo); | ||
accurateResult = Math.Round(accurateResult / Precision.ToDecimal(NumberFormatInfo.InvariantInfo)) * Precision.ToDecimal(NumberFormatInfo.InvariantInfo); | ||
decimal accurateResult = decimal.CreateTruncating(T.Clamp(value, MinValue, MaxValue)); | ||
accurateResult = Math.Round(accurateResult / decimal.CreateTruncating(Precision)) * decimal.CreateTruncating(Precision); | ||
|
||
base.Value = convertFromDecimal(accurateResult); | ||
base.Value = T.CreateTruncating(accurateResult); | ||
} | ||
else | ||
base.Value = value; | ||
} | ||
|
||
private T convertFromDecimal(decimal value) | ||
{ | ||
if (typeof(T) == typeof(sbyte)) | ||
return (T)(object)Convert.ToSByte(value); | ||
if (typeof(T) == typeof(byte)) | ||
return (T)(object)Convert.ToByte(value); | ||
if (typeof(T) == typeof(short)) | ||
return (T)(object)Convert.ToInt16(value); | ||
if (typeof(T) == typeof(ushort)) | ||
return (T)(object)Convert.ToUInt16(value); | ||
if (typeof(T) == typeof(int)) | ||
return (T)(object)Convert.ToInt32(value); | ||
if (typeof(T) == typeof(uint)) | ||
return (T)(object)Convert.ToUInt32(value); | ||
if (typeof(T) == typeof(long)) | ||
return (T)(object)Convert.ToInt64(value); | ||
if (typeof(T) == typeof(ulong)) | ||
return (T)(object)Convert.ToUInt64(value); | ||
if (typeof(T) == typeof(float)) | ||
return (T)(object)Convert.ToSingle(value); | ||
if (typeof(T) == typeof(double)) | ||
return (T)(object)Convert.ToDouble(value); | ||
|
||
throw new InvalidCastException($"Cannot convert from decimal to {typeof(T).ReadableName()}"); | ||
} | ||
protected override T DefaultMinValue => T.MinValue; | ||
|
||
protected override T DefaultMinValue | ||
{ | ||
get | ||
{ | ||
Debug.Assert(Validation.IsSupportedBindableNumberType<T>()); | ||
|
||
if (typeof(T) == typeof(sbyte)) | ||
return (T)(object)sbyte.MinValue; | ||
if (typeof(T) == typeof(byte)) | ||
return (T)(object)byte.MinValue; | ||
if (typeof(T) == typeof(short)) | ||
return (T)(object)short.MinValue; | ||
if (typeof(T) == typeof(ushort)) | ||
return (T)(object)ushort.MinValue; | ||
if (typeof(T) == typeof(int)) | ||
return (T)(object)int.MinValue; | ||
if (typeof(T) == typeof(uint)) | ||
return (T)(object)uint.MinValue; | ||
if (typeof(T) == typeof(long)) | ||
return (T)(object)long.MinValue; | ||
if (typeof(T) == typeof(ulong)) | ||
return (T)(object)ulong.MinValue; | ||
if (typeof(T) == typeof(float)) | ||
return (T)(object)float.MinValue; | ||
|
||
return (T)(object)double.MinValue; | ||
} | ||
} | ||
|
||
protected override T DefaultMaxValue | ||
{ | ||
get | ||
{ | ||
Debug.Assert(Validation.IsSupportedBindableNumberType<T>()); | ||
|
||
if (typeof(T) == typeof(sbyte)) | ||
return (T)(object)sbyte.MaxValue; | ||
if (typeof(T) == typeof(byte)) | ||
return (T)(object)byte.MaxValue; | ||
if (typeof(T) == typeof(short)) | ||
return (T)(object)short.MaxValue; | ||
if (typeof(T) == typeof(ushort)) | ||
return (T)(object)ushort.MaxValue; | ||
if (typeof(T) == typeof(int)) | ||
return (T)(object)int.MaxValue; | ||
if (typeof(T) == typeof(uint)) | ||
return (T)(object)uint.MaxValue; | ||
if (typeof(T) == typeof(long)) | ||
return (T)(object)long.MaxValue; | ||
if (typeof(T) == typeof(ulong)) | ||
return (T)(object)ulong.MaxValue; | ||
if (typeof(T) == typeof(float)) | ||
return (T)(object)float.MaxValue; | ||
|
||
return (T)(object)double.MaxValue; | ||
} | ||
} | ||
protected override T DefaultMaxValue => T.MaxValue; | ||
|
||
/// <summary> | ||
/// The default <see cref="Precision"/>. | ||
|
@@ -180,26 +98,12 @@ protected virtual T DefaultPrecision | |
{ | ||
get | ||
{ | ||
if (typeof(T) == typeof(sbyte)) | ||
return (T)(object)(sbyte)1; | ||
if (typeof(T) == typeof(byte)) | ||
return (T)(object)(byte)1; | ||
if (typeof(T) == typeof(short)) | ||
return (T)(object)(short)1; | ||
if (typeof(T) == typeof(ushort)) | ||
return (T)(object)(ushort)1; | ||
if (typeof(T) == typeof(int)) | ||
return (T)(object)1; | ||
if (typeof(T) == typeof(uint)) | ||
return (T)(object)1U; | ||
if (typeof(T) == typeof(long)) | ||
return (T)(object)1L; | ||
if (typeof(T) == typeof(ulong)) | ||
return (T)(object)1UL; | ||
if (typeof(T) == typeof(float)) | ||
return (T)(object)float.Epsilon; | ||
if (typeof(T) == typeof(double)) | ||
return (T)(object)double.Epsilon; | ||
|
||
return (T)(object)double.Epsilon; | ||
return T.One; | ||
Comment on lines
101
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dotnet/csharplang#6308 |
||
} | ||
} | ||
|
||
|
@@ -249,63 +153,11 @@ public override void UnbindEvents() | |
typeof(T) != typeof(float) && | ||
typeof(T) != typeof(double); // Will be **constant** after JIT. | ||
|
||
public void Set<TNewValue>(TNewValue val) where TNewValue : struct, | ||
IFormattable, IConvertible, IComparable<TNewValue>, IEquatable<TNewValue> | ||
{ | ||
Debug.Assert(Validation.IsSupportedBindableNumberType<T>()); | ||
|
||
// Comparison between typeof(T) and type literals are treated as **constant** on value types. | ||
// Code paths for other types will be eliminated. | ||
if (typeof(T) == typeof(byte)) | ||
((BindableNumber<byte>)(object)this).Value = val.ToByte(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(sbyte)) | ||
((BindableNumber<sbyte>)(object)this).Value = val.ToSByte(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(ushort)) | ||
((BindableNumber<ushort>)(object)this).Value = val.ToUInt16(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(short)) | ||
((BindableNumber<short>)(object)this).Value = val.ToInt16(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(uint)) | ||
((BindableNumber<uint>)(object)this).Value = val.ToUInt32(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(int)) | ||
((BindableNumber<int>)(object)this).Value = val.ToInt32(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(ulong)) | ||
((BindableNumber<ulong>)(object)this).Value = val.ToUInt64(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(long)) | ||
((BindableNumber<long>)(object)this).Value = val.ToInt64(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(float)) | ||
((BindableNumber<float>)(object)this).Value = val.ToSingle(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(double)) | ||
((BindableNumber<double>)(object)this).Value = val.ToDouble(NumberFormatInfo.InvariantInfo); | ||
} | ||
public void Set<TNewValue>(TNewValue val) where TNewValue : struct, INumber<TNewValue> | ||
=> Value = T.CreateTruncating(val); | ||
|
||
public void Add<TNewValue>(TNewValue val) where TNewValue : struct, | ||
IFormattable, IConvertible, IComparable<TNewValue>, IEquatable<TNewValue> | ||
{ | ||
Debug.Assert(Validation.IsSupportedBindableNumberType<T>()); | ||
|
||
// Comparison between typeof(T) and type literals are treated as **constant** on value types. | ||
// Code pathes for other types will be eliminated. | ||
if (typeof(T) == typeof(byte)) | ||
((BindableNumber<byte>)(object)this).Value += val.ToByte(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(sbyte)) | ||
((BindableNumber<sbyte>)(object)this).Value += val.ToSByte(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(ushort)) | ||
((BindableNumber<ushort>)(object)this).Value += val.ToUInt16(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(short)) | ||
((BindableNumber<short>)(object)this).Value += val.ToInt16(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(uint)) | ||
((BindableNumber<uint>)(object)this).Value += val.ToUInt32(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(int)) | ||
((BindableNumber<int>)(object)this).Value += val.ToInt32(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(ulong)) | ||
((BindableNumber<ulong>)(object)this).Value += val.ToUInt64(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(long)) | ||
((BindableNumber<long>)(object)this).Value += val.ToInt64(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(float)) | ||
((BindableNumber<float>)(object)this).Value += val.ToSingle(NumberFormatInfo.InvariantInfo); | ||
else if (typeof(T) == typeof(double)) | ||
((BindableNumber<double>)(object)this).Value += val.ToDouble(NumberFormatInfo.InvariantInfo); | ||
} | ||
public void Add<TNewValue>(TNewValue val) where TNewValue : struct, INumber<TNewValue> | ||
=> Value += T.CreateTruncating(val); | ||
|
||
/// <summary> | ||
/// Sets the value of the bindable to Min + (Max - Min) * amt | ||
|
@@ -314,8 +166,10 @@ public void Add<TNewValue>(TNewValue val) where TNewValue : struct, | |
/// </summary> | ||
public void SetProportional(float amt, float snap = 0) | ||
{ | ||
double min = MinValue.ToDouble(NumberFormatInfo.InvariantInfo); | ||
double max = MaxValue.ToDouble(NumberFormatInfo.InvariantInfo); | ||
// TODO: Use IFloatingPointIeee754<T>.Lerp when applicable | ||
|
||
double min = double.CreateTruncating(MinValue); | ||
double max = double.CreateTruncating(MaxValue); | ||
double value = min + (max - min) * amt; | ||
if (snap > 0) | ||
value = Math.Round(value / snap) * snap; | ||
|
@@ -350,20 +204,8 @@ public override bool IsDefault | |
|
||
protected override Bindable<T> CreateInstance() => new BindableNumber<T>(); | ||
|
||
protected sealed override T ClampValue(T value, T minValue, T maxValue) => max(minValue, min(maxValue, value)); | ||
|
||
protected sealed override bool IsValidRange(T min, T max) => min.CompareTo(max) <= 0; | ||
protected sealed override T ClampValue(T value, T minValue, T maxValue) => T.Clamp(value, minValue, maxValue); | ||
|
||
private static T max(T value1, T value2) | ||
{ | ||
int comparison = value1.CompareTo(value2); | ||
return comparison > 0 ? value1 : value2; | ||
} | ||
|
||
private static T min(T value1, T value2) | ||
{ | ||
int comparison = value1.CompareTo(value2); | ||
return comparison > 0 ? value2 : value1; | ||
} | ||
protected sealed override bool IsValidRange(T min, T max) => min <= max; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this can be combination of operator interfaces rather than the meta one, but you shouldn't be happy about that.