Skip to content
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

Size and SizeF should not throw on NaN #22890

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 18 additions & 49 deletions src/Graphics/src/Graphics/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,62 @@ namespace Microsoft.Maui.Graphics
[TypeConverter(typeof(Converters.SizeTypeConverter))]
public partial struct Size
{
double _width;
double _height;

public static readonly Size Zero;

public Size(double size = 0)
{
if (double.IsNaN(size))
throw new ArgumentException("NaN is not a valid value for size");
_width = size;
_height = size;
Width = size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include some unit tests accepting NaN values to cover any unexpected change/behavior in the future?

Height = size;
}

public Size(double width, double height)
{
if (double.IsNaN(width))
throw new ArgumentException("NaN is not a valid value for width");
if (double.IsNaN(height))
throw new ArgumentException("NaN is not a valid value for height");
_width = width;
_height = height;
Width = width;
Height = height;
}

public Size(Vector2 vector)
{
if (float.IsNaN(vector.X))
throw new ArgumentException("NaN is not a valid value for X");
if (float.IsNaN(vector.Y))
throw new ArgumentException("NaN is not a valid value for Y");
_width = vector.X;
_height = vector.Y;
Width = vector.X;
Height = vector.Y;
}

public bool IsZero => _width == 0 && _height == 0;
public bool IsZero => Width == 0 && Height == 0;

[DefaultValue(0d)]
public double Width
{
get => _width;
set
{
if (double.IsNaN(value))
throw new ArgumentException("NaN is not a valid value for Width");
_width = value;
}
}
public double Width { get; set; }

[DefaultValue(0d)]
public double Height
{
get => _height;
set
{
if (double.IsNaN(value))
throw new ArgumentException("NaN is not a valid value for Height");
_height = value;
}
}
public double Height { get; set; }

public static Size operator +(Size s1, Size s2)
{
return new Size(s1._width + s2._width, s1._height + s2._height);
return new Size(s1.Width + s2.Width, s1.Height + s2.Height);
}

public static Size operator -(Size s1, Size s2)
{
return new Size(s1._width - s2._width, s1._height - s2._height);
return new Size(s1.Width - s2.Width, s1.Height - s2.Height);
}

public static Size operator *(Size s1, double value)
{
return new Size(s1._width * value, s1._height * value);
return new Size(s1.Width * value, s1.Height * value);
}

public static Size operator /(Size s1, double value)
{
return new Size(s1._width / value, s1._height / value);
return new Size(s1.Width / value, s1.Height / value);
}

public static bool operator ==(Size s1, Size s2)
{
return s1._width == s2._width && s1._height == s2._height;
return s1.Width == s2.Width && s1.Height == s2.Height;
}

public static bool operator !=(Size s1, Size s2)
{
return s1._width != s2._width || s1._height != s2._height;
return s1.Width != s2.Width || s1.Height != s2.Height;
}

public static explicit operator Point(Size size)
Expand All @@ -106,7 +75,7 @@ public static explicit operator Point(Size size)

public bool Equals(Size other)
{
return _width.Equals(other._width) && _height.Equals(other._height);
return Width.Equals(other.Width) && Height.Equals(other.Height);
}

public override bool Equals(object obj)
Expand All @@ -120,13 +89,13 @@ public override int GetHashCode()
{
unchecked
{
return (_width.GetHashCode() * 397) ^ _height.GetHashCode();
return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
}
}

public override string ToString()
{
return string.Format("{{Width={0} Height={1}}}", _width.ToString(CultureInfo.InvariantCulture), _height.ToString(CultureInfo.InvariantCulture));
return string.Format("{{Width={0} Height={1}}}", Width.ToString(CultureInfo.InvariantCulture), Height.ToString(CultureInfo.InvariantCulture));
}

public void Deconstruct(out double width, out double height)
Expand Down
71 changes: 20 additions & 51 deletions src/Graphics/src/Graphics/SizeF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,33 @@ namespace Microsoft.Maui.Graphics
[TypeConverter(typeof(Converters.SizeFTypeConverter))]
public partial struct SizeF
{
float _width;
float _height;

public static readonly SizeF Zero;

public SizeF(float size = 0)
{
if (float.IsNaN(size))
throw new ArgumentException("NaN is not a valid value for size");
_width = size;
_height = size;
Width = size;
Height = size;
}

public SizeF(float width, float height)
{
if (float.IsNaN(width))
throw new ArgumentException("NaN is not a valid value for width");
if (float.IsNaN(height))
throw new ArgumentException("NaN is not a valid value for height");
_width = width;
_height = height;
Width = width;
Height = height;
}

public SizeF(Vector2 vector)
{
if (float.IsNaN(vector.X))
throw new ArgumentException("NaN is not a valid value for X");
if (float.IsNaN(vector.Y))
throw new ArgumentException("NaN is not a valid value for Y");
_width = vector.X;
_height = vector.Y;
Width = vector.X;
Height = vector.Y;
}

public bool IsZero => _width == 0 && _height == 0;
public bool IsZero => Width == 0 && Height == 0;

[DefaultValue(0d)]
public float Width
{
get => _width;
set
{
if (float.IsNaN(value))
throw new ArgumentException("NaN is not a valid value for Width");
_width = value;
}
}
[DefaultValue(0f)]
public float Width { get; set; }

[DefaultValue(0d)]
public float Height
{
get => _height;
set
{
if (float.IsNaN(value))
throw new ArgumentException("NaN is not a valid value for Height");
_height = value;
}
}
[DefaultValue(0f)]
public float Height { get; set; }

public SizeF TransformNormalBy(in Matrix3x2 transform)
{
Expand All @@ -76,32 +45,32 @@ public SizeF TransformNormalBy(in Matrix3x2 transform)

public static SizeF operator +(SizeF s1, SizeF s2)
{
return new SizeF(s1._width + s2._width, s1._height + s2._height);
return new SizeF(s1.Width + s2.Width, s1.Height + s2.Height);
}

public static SizeF operator -(SizeF s1, SizeF s2)
{
return new SizeF(s1._width - s2._width, s1._height - s2._height);
return new SizeF(s1.Width - s2.Width, s1.Height - s2.Height);
}

public static SizeF operator *(SizeF s1, float value)
{
return new SizeF(s1._width * value, s1._height * value);
return new SizeF(s1.Width * value, s1.Height * value);
}

public static SizeF operator /(SizeF s1, float value)
{
return new SizeF(s1._width / value, s1._height / value);
return new SizeF(s1.Width / value, s1.Height / value);
}

public static bool operator ==(SizeF s1, SizeF s2)
{
return s1._width == s2._width && s1._height == s2._height;
return s1.Width == s2.Width && s1.Height == s2.Height;
}

public static bool operator !=(SizeF s1, SizeF s2)
{
return s1._width != s2._width || s1._height != s2._height;
return s1.Width != s2.Width || s1.Height != s2.Height;
}

public static explicit operator PointF(SizeF size)
Expand All @@ -121,7 +90,7 @@ public static explicit operator SizeF(Vector2 size)

public bool Equals(SizeF other)
{
return _width.Equals(other._width) && _height.Equals(other._height);
return Width.Equals(other.Width) && Height.Equals(other.Height);
}

public override bool Equals(object obj)
Expand All @@ -135,13 +104,13 @@ public override int GetHashCode()
{
unchecked
{
return (_width.GetHashCode() * 397) ^ _height.GetHashCode();
return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
}
}

public override string ToString()
{
return string.Format("{{Width={0} Height={1}}}", _width.ToString(CultureInfo.InvariantCulture), _height.ToString(CultureInfo.InvariantCulture));
return string.Format("{{Width={0} Height={1}}}", Width.ToString(CultureInfo.InvariantCulture), Height.ToString(CultureInfo.InvariantCulture));
}

public void Deconstruct(out float width, out float height)
Expand Down
Loading