FixedPoint2 tweaks (#12431)

This commit is contained in:
Leon Friedrich 2022-11-08 12:14:13 +13:00 committed by GitHub
parent f6e5790b72
commit 5cade18119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 16 deletions

View File

@ -70,21 +70,17 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 operator *(FixedPoint2 a, FixedPoint2 b)
{
var aD = a.ShiftDown();
var bD = b.ShiftDown();
return New(aD * bD);
return new((int) MathF.Round(b._value * a._value / MathF.Pow(10, Shift), MidpointRounding.AwayFromZero));
}
public static FixedPoint2 operator *(FixedPoint2 a, float b)
{
var aD = (float) a.ShiftDown();
return New(aD * b);
return new((int) MathF.Round(a._value * b, MidpointRounding.AwayFromZero));
}
public static FixedPoint2 operator *(FixedPoint2 a, double b)
{
var aD = a.ShiftDown();
return New(aD * b);
return new((int) Math.Round(a._value * b, MidpointRounding.AwayFromZero));
}
public static FixedPoint2 operator *(FixedPoint2 a, int b)
@ -94,18 +90,12 @@ namespace Content.Shared.FixedPoint
public static FixedPoint2 operator /(FixedPoint2 a, FixedPoint2 b)
{
if (b._value == 0)
{
throw new DivideByZeroException();
}
var aD = a.ShiftDown();
var bD = b.ShiftDown();
return New(aD / bD);
return new((int) MathF.Round((MathF.Pow(10, Shift) * a._value) / b._value, MidpointRounding.AwayFromZero));
}
public static FixedPoint2 operator /(FixedPoint2 a, float b)
{
return a / FixedPoint2.New(b);
return new((int) MathF.Round(a._value / b, MidpointRounding.AwayFromZero));
}
public static bool operator <=(FixedPoint2 a, int b)

View File

@ -1,4 +1,4 @@
using System;
using System;
using Content.Shared.FixedPoint;
using NUnit.Framework;
@ -125,6 +125,42 @@ namespace Content.Tests.Shared.Chemistry
Assert.That(min, Is.EqualTo(FixedPoint2.New(1)));
}
[Test]
[TestCase(10.1f, 2.5f, "25.25")]
public void FloatMultiply (float aFloat, float b, string expected)
{
var a = FixedPoint2.New(aFloat);
var result = a*b;
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase(10.1f, 2.5d, "25.25")]
public void DoubleMultiply(float aFloat, double b, string expected)
{
var a = FixedPoint2.New(aFloat);
var result = a * b;
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase(10.1f, 2.5f, "4.04")]
public void FloatDivide(float aFloat, float b, string expected)
{
var a = FixedPoint2.New(aFloat);
var result = a / b;
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase(10.1f, 2.5d, "4.04")]
public void DoubleDivide(float aFloat, double b, string expected)
{
var a = FixedPoint2.New(aFloat);
var result = a / b;
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase(1, 0, false)]
[TestCase(0, 0, true)]