Skip to content

Commit

Permalink
Use MathUtil.WithinEpsilon to compare float values
Browse files Browse the repository at this point in the history
  • Loading branch information
carmineos committed Feb 22, 2020
1 parent 79ba555 commit 14c13f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions VStancer.Client/VStancerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class VStancerEditor : BaseScript
public float rearMaxOffset = 0.25f;
public float rearMaxCamber = 0.20f;

private float FloatPrecision = 0.001f;
private float Epsilon = 0.001f;
private long timer = 1000;
private bool debug = false;
private bool exposeCommand = false;
Expand Down Expand Up @@ -566,7 +566,7 @@ private void UpdateFloatDecorator(int vehicle, string name, float currentValue,
if (DecorExistOn(vehicle, name))
{
float decorValue = DecorGetFloat(vehicle, name);
if (Math.Abs(currentValue - decorValue) > FloatPrecision)
if (!MathUtil.WithinEpsilon(currentValue, decorValue, Epsilon))
{
DecorSetFloat(vehicle, name, currentValue);
if (debug)
Expand All @@ -575,7 +575,7 @@ private void UpdateFloatDecorator(int vehicle, string name, float currentValue,
}
else // Decorator doesn't exist, create it if required
{
if (Math.Abs(currentValue - defaultValue) > FloatPrecision)
if (!MathUtil.WithinEpsilon(currentValue, defaultValue, Epsilon))
{
DecorSetFloat(vehicle, name, currentValue);
if (debug)
Expand Down
16 changes: 9 additions & 7 deletions VStancer.Client/VStancerPreset.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using CitizenFX.Core;
using System;
using System.Text;

namespace Vstancer.Client
{
public class VStancerPreset : IEquatable<VStancerPreset>
{
public static float Precision { get; private set; } = 0.001f;
public static float Epsilon { get; private set; } = 0.001f;
public int WheelsCount { get; private set; }
public int FrontWheelsCount { get; private set; }

Expand Down Expand Up @@ -44,7 +45,8 @@ public bool IsEdited
{
for (int index = 0; index < WheelsCount; index++)
{
if ((DefaultOffsetX[index] != OffsetX[index]) || (DefaultRotationY[index] != RotationY[index]))
if (!MathUtil.WithinEpsilon(DefaultOffsetX[index], OffsetX[index], Epsilon) ||
!MathUtil.WithinEpsilon(DefaultRotationY[index], RotationY[index], Epsilon))
return true;
}
return false;
Expand Down Expand Up @@ -146,10 +148,10 @@ public bool Equals(VStancerPreset other)

for (int index = 0; index < WheelsCount; index++)
{
if (Math.Abs(DefaultOffsetX[index] - other.DefaultOffsetX[index]) > Precision
|| Math.Abs(DefaultRotationY[index] - other.DefaultRotationY[index]) > Precision
|| Math.Abs(OffsetX[index] - other.OffsetX[index]) > Precision
|| Math.Abs(RotationY[index] - other.RotationY[index]) > Precision)
if (!MathUtil.WithinEpsilon(DefaultOffsetX[index], other.DefaultOffsetX[index], Epsilon) ||
!MathUtil.WithinEpsilon(DefaultRotationY[index], other.DefaultRotationY[index], Epsilon) ||
!MathUtil.WithinEpsilon(OffsetX[index], other.OffsetX[index], Epsilon) ||
!MathUtil.WithinEpsilon(RotationY[index], other.RotationY[index], Epsilon))
return false;
}
return true;
Expand Down

0 comments on commit 14c13f1

Please sign in to comment.