Skip to content

Commit

Permalink
Moved to using double again
Browse files Browse the repository at this point in the history
A compromise that almost doubles the speed, but doesn't seem to affect precision
  • Loading branch information
Prevter committed Sep 16, 2022
1 parent d68854a commit c67fb1c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion FloatTool/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void OnRenamed(object source, RenamedEventArgs e)
}
}

public class EnumToBooleanConverter : IValueConverter
public sealed class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object param, CultureInfo culture)
{
Expand Down
10 changes: 5 additions & 5 deletions FloatTool/Common/Calculations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace FloatTool
{
static public class Calculations
{
static public decimal Craft(InputSkin[] ingridients, decimal minFloat, decimal floatRange)
public static double Craft(InputSkin[] ingridients, double minFloat, double floatRange)
{
return floatRange * (ingridients[0].WearValue
+ ingridients[1].WearValue
Expand All @@ -36,7 +36,7 @@ static public decimal Craft(InputSkin[] ingridients, decimal minFloat, decimal f
+ ingridients[9].WearValue) + minFloat;
}

static public bool NextCombination(int[] num, int n)
public static bool NextCombination(int[] num, int n)
{
bool finished = false;
for (int i = 9; !finished; --i)
Expand All @@ -54,7 +54,7 @@ static public bool NextCombination(int[] num, int n)
return false;
}

static public IEnumerable<InputSkin[]> Combinations(InputSkin[] elem, int start, int skip)
public static IEnumerable<InputSkin[]> Combinations(InputSkin[] elem, int start, int skip)
{
int size = elem.Length - 10;
int[] numbers = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Expand All @@ -63,14 +63,14 @@ static public IEnumerable<InputSkin[]> Combinations(InputSkin[] elem, int start,

for (int i = 0; i < start; i++)
running = NextCombination(numbers, size);

while (running)
{
for (int i = 0; i < 10; ++i)
resultList[i] = elem[numbers[i]];
yield return resultList;

for (int i = 0; i < skip && running; i++)
for (int i = 0; i < skip; i++)
running = NextCombination(numbers, size);
}
}
Expand Down
30 changes: 15 additions & 15 deletions FloatTool/Common/Skin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public struct SkinModel
{
public string Name;
public string Rarity;
public float MinWear;
public float MaxWear;
public double MinWear;
public double MaxWear;

public bool IsQualityInRange(string quality)
{
Expand All @@ -58,16 +58,16 @@ public enum Quality
}

public string Name;
public decimal MinFloat;
public decimal MaxFloat;
public decimal FloatRange;
public double MinFloat;
public double MaxFloat;
public double FloatRange;
public Quality Rarity;

public Skin(string name, float minWear, float maxWear, Quality rarity)
public Skin(string name, double minWear, double maxWear, Quality rarity)
{
Name = name;
MinFloat = (decimal)minWear;
MaxFloat = (decimal)maxWear;
MinFloat = minWear;
MaxFloat = maxWear;
FloatRange = (MaxFloat - MinFloat) / 10;
Rarity = rarity;
}
Expand Down Expand Up @@ -140,27 +140,27 @@ public static FloatRange GetFloatRangeForQuality(string quality)

public sealed class InputSkin
{
public decimal WearValue;
public double WearValue;
public float Price;
public Currency SkinCurrency;

private RelayCommand copyCommand;
public RelayCommand CopyCommand {
get {
public RelayCommand CopyCommand
{
get
{
return copyCommand ??= new RelayCommand(obj => Clipboard.SetText(WearValue.ToString("0.00000000000000", CultureInfo.InvariantCulture)));
}
}

public decimal GetWearValue => WearValue;
public double GetWearValue => WearValue;

public InputSkin(decimal wear, float price, Currency currency)
public InputSkin(double wear, float price, Currency currency)
{
WearValue = wear;
Price = price;
SkinCurrency = currency;
}

public InputSkin(double wear, float price, Currency currency) : this((decimal)wear, price, currency) { }

internal int CompareTo(InputSkin b)
{
Expand Down
20 changes: 10 additions & 10 deletions FloatTool/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ public static class Utils
public const string API_URL = "https://git.prevter.ml/api/floattool";
private static readonly HttpClient Client = new();

public static async Task<decimal> GetWearFromInspectURL(string inspect_url)
public static async Task<double> GetWearFromInspectURL(string inspect_url)
{
var result = await Client.GetAsync($"https://api.csgofloat.com/?url={inspect_url}");
result.EnsureSuccessStatusCode();
string response = await result.Content.ReadAsStringAsync();
dynamic json = JsonConvert.DeserializeObject(response);
return Convert.ToDecimal(json["iteminfo"]["floatvalue"]);
return Convert.ToDouble(json["iteminfo"]["floatvalue"]);
}

public static async Task<UpdateResult> CheckForUpdates()
Expand Down Expand Up @@ -206,8 +206,8 @@ public class Asset

public struct CraftSearchSetup
{
public decimal SearchTarget;
public decimal TargetPrecision;
public double SearchTarget;
public double TargetPrecision;
public string SearchFilter;

public Skin[] Outcomes;
Expand All @@ -221,10 +221,10 @@ public struct CraftSearchSetup

public struct FloatRange
{
readonly float min;
readonly float max;
readonly double min;
readonly double max;

public FloatRange(float min, float max)
public FloatRange(double min, double max)
{
this.min = min;
this.max = max;
Expand All @@ -235,8 +235,8 @@ public bool IsOverlapped(FloatRange other)
return Min <= other.Max && other.Min <= Max;
}

public float Min { get { return min; } }
public float Max { get { return max; } }
public double Min { get { return min; } }
public double Max { get { return max; } }
}

/// <summary>
Expand All @@ -246,7 +246,7 @@ public sealed class RPCSettingsPersist
{
public string Details { get; set; }
public string State { get; set; }

public DiscordRPC.Timestamps Timestamp { get; set; }
public bool ShowTime { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions FloatTool/ViewModels/BenchmarkViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public sealed class BenchmarkResult
private static readonly LinearGradientBrush AMDBrush = Application.Current.Resources["AmdBenchmarkFill"] as LinearGradientBrush;
private static readonly LinearGradientBrush IntelBrush = Application.Current.Resources["IntelBenchmarkFill"] as LinearGradientBrush;
private static readonly LinearGradientBrush CurrentBrush = Application.Current.Resources["CurrentBenchmarkFill"] as LinearGradientBrush;

public int ProgressPercentage
{
get { return progressPercentage; }
Expand Down Expand Up @@ -86,7 +86,7 @@ public bool CanPublish
OnPropertyChanged();
}
}

public int ThreadCount
{
get
Expand Down
9 changes: 5 additions & 4 deletions FloatTool/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public enum SearchMode

public sealed class Combination
{
public decimal Wear { get; set; }
public string IEEE754 { get; set; }
public double Wear { get; set; }
public string Wear32Bit { get; set; }
public string Wear128Bit { get; set; }
public InputSkin[] Inputs { get; set; }
public string OutcomeName { get; set; }
public Currency Currency { get; set; }
Expand Down Expand Up @@ -119,7 +120,7 @@ public sealed class MainViewModel : INotifyPropertyChanged

private List<string> skinList = new();
private List<string> outcomeList = new();
public Dictionary<Tuple<float, float>, List<Skin>> Outcomes = new();
public Dictionary<Tuple<double, double>, List<Skin>> Outcomes = new();

public List<Collection> SkinsDatabase;
public Settings Settings { get; set; }
Expand Down Expand Up @@ -473,7 +474,7 @@ public void UpdateOutcomes()

for (int i = 0; i < skinlist.Count; i++)
{
var range = new Tuple<float, float>(skinlist[i].MinWear, skinlist[i].MaxWear);
var range = new Tuple<double, double>(skinlist[i].MinWear, skinlist[i].MaxWear);
if (Outcomes.ContainsKey(range))
Outcomes[range].Add(new Skin(skinlist[i]));
else
Expand Down
8 changes: 4 additions & 4 deletions FloatTool/Views/BenchmarkWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void FloatCraftWorkerThread(CraftSearchSetup options)
{
for (int i = 0; i < options.Outcomes.Length; ++i)
{
decimal resultFloat = Calculations.Craft(
double resultFloat = Calculations.Craft(
resultList, options.Outcomes[i].MinFloat, options.Outcomes[i].FloatRange
);

Expand Down Expand Up @@ -125,7 +125,7 @@ private void StartBenchmark_Click(object sender, RoutedEventArgs e)
new Thread(() =>
{
Skin[] outcomes = new Skin[] {
new Skin("AK-47 | Safari Mesh", 0.06f, 0.8f, Skin.Quality.Industrial)
new Skin("AK-47 | Safari Mesh", 0.06, 0.8, Skin.Quality.Industrial)
};

double[] pool = {
Expand Down Expand Up @@ -156,8 +156,8 @@ private void StartBenchmark_Click(object sender, RoutedEventArgs e)

string searchFilter = "0.250000000";

decimal searched = decimal.Parse(searchFilter, CultureInfo.InvariantCulture);
decimal precission = (decimal)Math.Pow(0.1, searchFilter.Length - 2);
double searched = double.Parse(searchFilter, CultureInfo.InvariantCulture);
double precission = Math.Pow(0.1, searchFilter.Length - 2);

// Create thread pool
List<Task> threadPool = new();
Expand Down
4 changes: 2 additions & 2 deletions FloatTool/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@
<TextBlock Text="{Binding Wear, StringFormat=n14}" Foreground="{DynamicResource CombinationForeground}" FontSize="13"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="IEEE754:" Foreground="{DynamicResource CombinationForeground}" FontSize="13" Margin="0,0,4,0"/>
<TextBlock Text="{Binding IEEE754}" Foreground="{DynamicResource CombinationForeground}" FontSize="13"/>
<TextBlock Text="32-bit:" Foreground="{DynamicResource CombinationForeground}" FontSize="13" Margin="0,0,4,0"/>
<TextBlock Text="{Binding Wear32Bit}" Foreground="{DynamicResource CombinationForeground}" FontSize="13"/>
</StackPanel>
</StackPanel>
<StackPanel Margin="8" Grid.Column="1" VerticalAlignment="Center">
Expand Down
20 changes: 10 additions & 10 deletions FloatTool/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Media;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -40,7 +41,7 @@ public sealed partial class MainWindow : Window
{
public MainViewModel ViewModel;
public Settings Settings;
private RPCSettingsPersist RPCSettings = new();
private readonly RPCSettingsPersist RPCSettings = new();

private static long PassedCombinations;
private static List<Task> ThreadPool;
Expand Down Expand Up @@ -189,7 +190,7 @@ private void FloatCraftWorkerThread(CraftSearchSetup options)
{
for (int i = 0; i < options.Outcomes.Length; ++i)
{
decimal resultFloat = Calculations.Craft(
double resultFloat = Calculations.Craft(
resultList, options.Outcomes[i].MinFloat, options.Outcomes[i].FloatRange
);

Expand Down Expand Up @@ -235,7 +236,7 @@ private void FloatCraftWorkerThread(CraftSearchSetup options)
Inputs = result,
Currency = result[0].SkinCurrency,
Price = price,
IEEE754 = ((double)ieee).ToString("0.000000000000000", CultureInfo.InvariantCulture)
Wear32Bit = ((double)ieee).ToString("0.000000000000000", CultureInfo.InvariantCulture),
});
if (Settings.Sound)
CombinationFoundSound.Play();
Expand Down Expand Up @@ -282,7 +283,7 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
ViewModel.CanEditSettings = false;
ViewModel.ProgressPercentage = 0;
ViewModel.TotalCombinations = Calculations.GetCombinationsCount(ViewModel.SkinCount);

int index = 0;
Skin[] outcomes = Array.Empty<Skin>();
bool found = false;
Expand All @@ -304,7 +305,6 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
}

ConcurrentBag<InputSkin> inputSkinBag = new();
List<Task> downloaderTasks = new();

string url = $"https://steamcommunity.com/market/listings/730/{ViewModel.FullSkinName}/render/?count={ViewModel.SkinCount}&start={ViewModel.SkinSkipCount}&currency={(int)Settings.Currency}";
try
Expand All @@ -323,7 +323,7 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)

SetStatus("m_GettingFloats");

Dictionary<Task<decimal>, float> floatTasks = new();
Dictionary<Task<double>, float> floatTasks = new();
foreach (var skin in r["listinginfo"])
{
string lid = r["listinginfo"][skin.Name]["listingid"].ToString();
Expand Down Expand Up @@ -413,8 +413,8 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)

string searchFilter = ViewModel.SearchFilter;

decimal searched = decimal.Parse(searchFilter, CultureInfo.InvariantCulture);
decimal precission = (decimal)Math.Pow(0.1, searchFilter.Length - 2);
double searched = double.Parse(searchFilter, CultureInfo.InvariantCulture);
double precission = Math.Pow(0.1, searchFilter.Length - 2);

// Create thread pool
ThreadPool = new();
Expand Down Expand Up @@ -456,7 +456,7 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
timer.Restart();

bool isAnyRunning = false;
foreach (Task t in ThreadPool)
foreach (Task t in CollectionsMarshal.AsSpan(ThreadPool))
{
if (t.Status != TaskStatus.RanToCompletion)
{
Expand All @@ -476,7 +476,7 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
LastCombinationCount = PassedCombinations;
ViewModel.ProgressPercentage = (float)PassedCombinations * 100 / ViewModel.TotalCombinations;
ViewModel.ParsedCombinations = PassedCombinations;
ViewModel.CombinationsLabel = String.Empty;
ViewModel.CombinationsLabel = string.Empty;

if (!isAnyRunning)
break;
Expand Down

0 comments on commit c67fb1c

Please sign in to comment.