Информация об изменениях

Сообщение Re[3]: [забыл математику] Оптимизация алгоритма от 15.09.2022 14:59

Изменено 15.09.2022 15:02 xma

Re[3]: [забыл математику] Оптимизация алгоритма
Здравствуйте, Real 3L0, Вы писали:

R3>Часто не совпадает.


во, сделал — наслаждайся

  Скрытый текст
using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        
        decimal marketBuyPrice = 90m, overwriteMinStep = 0.2m;
        
        var ret = test_original(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret = " + ret);
        
        var ret_opt = test_optimal(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret_opt = " + ret_opt);
        
        var ret_opt2 = test_optimal2(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret_opt2 = " + ret_opt2);
    }
    
    public static decimal test_original(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        decimal mediumPrice2 = 0;

        do
        {
            mediumPrice2 += overwriteMinStep;
        } while (mediumPrice2 < marketBuyPrice);
        
        return mediumPrice2;
    }
    
    public static decimal test_optimal(decimal marketBuyPrice, decimal overwriteMinStep)
    {
        if (decimal.Zero == marketBuyPrice) // == 0
            return overwriteMinStep;
        
        decimal temp_remainder = marketBuyPrice % overwriteMinStep;
        
        if (decimal.Zero == temp_remainder) // == 0
            return marketBuyPrice;
        
        decimal mediumPrice2 = (decimal.Floor(marketBuyPrice / overwriteMinStep) + 1) * overwriteMinStep;
        return mediumPrice2;
    }
    
    public static decimal test_optimal2(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        if (decimal.Zero == marketBuyPrice) // == 0
            return overwriteMinStep;

        decimal mediumPrice2 = (decimal.Floor(marketBuyPrice / overwriteMinStep) + 1) * overwriteMinStep;
        
        if ( mediumPrice2 == marketBuyPrice + overwriteMinStep )
            mediumPrice2 -= overwriteMinStep;
            
        return mediumPrice2;
    }
}
Re[3]: [забыл математику] Оптимизация алгоритма
Здравствуйте, Real 3L0, Вы писали:

R3>Часто не совпадает.


во, сделал — наслаждайся

  Скрытый текст
проверяй тут,
https://dotnetfiddle.net/

using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        
        decimal marketBuyPrice = 90m, overwriteMinStep = 0.2m;
        
        var ret = test_original(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret = " + ret);
        
        var ret_opt = test_optimal(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret_opt = " + ret_opt);
        
        var ret_opt2 = test_optimal2(marketBuyPrice, overwriteMinStep);
        Console.WriteLine("ret_opt2 = " + ret_opt2);
    }
    
    public static decimal test_original(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        decimal mediumPrice2 = 0;

        do
        {
            mediumPrice2 += overwriteMinStep;
        } while (mediumPrice2 < marketBuyPrice);
        
        return mediumPrice2;
    }
    
    public static decimal test_optimal(decimal marketBuyPrice, decimal overwriteMinStep)
    {
        if (decimal.Zero == marketBuyPrice) // == 0
            return overwriteMinStep;
        
        decimal temp_remainder = marketBuyPrice % overwriteMinStep;
        
        if (decimal.Zero == temp_remainder) // == 0
            return marketBuyPrice;
        
        decimal mediumPrice2 = (decimal.Floor(marketBuyPrice / overwriteMinStep) + 1) * overwriteMinStep;
        return mediumPrice2;
    }
    
    public static decimal test_optimal2(decimal marketBuyPrice, decimal overwriteMinStep)
    {        
        if (decimal.Zero == marketBuyPrice) // == 0
            return overwriteMinStep;

        decimal mediumPrice2 = (decimal.Floor(marketBuyPrice / overwriteMinStep) + 1) * overwriteMinStep;
        
        if ( mediumPrice2 == marketBuyPrice + overwriteMinStep )
            mediumPrice2 -= overwriteMinStep;
            
        return mediumPrice2;
    }
}


R3>Там не может быть отрицательное значение. Ну, теоретически.


проверял для overwriteMinStep > 0 и marketBuyPrice >= 0