Hello. I make the algorithm of the exchange, which determines the required amount and the coefficient of 2 players based on the amount and coefficient of 1 player. The algorithm is on both JS and PHP.

var mt = { //ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ коэф. 2 ΠΈΠ³Ρ€ΠΎΠΊΠ° findOtherRatio: function(ratioOtherPlayer){ return ratioOtherPlayer / (ratioOtherPlayer - 1) }, //ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ ΠΏΡ€ΠΈΠ±Ρ‹Π»ΡŒ 1 ΠΈΠ³Ρ€ΠΎΠΊΠ° - Ρ‡Ρ‚ΠΎ Ρ‚Π°ΠΊΠΆΠ΅ являСтся Π½ΡƒΠΆΠ½ΠΎΠΉ, стартовой суммой 2 ΠΈΠ³Ρ€ΠΎΠΊΠ° findProfit: function(start, ratio){ return start * ratio - start; }, //Π‘ΡƒΠΌΠΌΠ° Π²Ρ‹Π²ΠΎΠ΄Π° ΠΈΠ³Ρ€ΠΎΠΊΠ° findOutcome: function(start, ratio){ return start * ratio; } }; 

Everything is considered almost true. But the problem is that in many cases the numbers look like the thousandths, millionths ... And as soon as I want to give a presentable look to the numbers, namely, a maximum of 2 digits after the decimal point, the numbers are no longer equal (1 player’s withdrawal! = 2 players’s ).

For example: imagine that 1 player put 736 rubles for 1.55

  var cof2Player = mt.findOtherRatio(1.55).toFixed(2) var money2Player = mt.findProfit(736,1.55).toFixed(2) mt.findOutcome(money2Player, cof2Player).toFixed(2) // 1141.54 mt.findOutcome(736, 1.55).toFixed(2) // 1140.80 

Is there any way around this? How do different exchanges solve this? After all, they do not show the coefficients "3.8571428571428563" to players.

    1 answer 1

    This is what will happen if you use math.js :

     var mt = { //ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ коэф. 2 ΠΈΠ³Ρ€ΠΎΠΊΠ° findOtherRatio: function(ratioOtherPlayer){ return ratioOtherPlayer.div(ratioOtherPlayer.sub(1)); }, //ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ ΠΏΡ€ΠΈΠ±Ρ‹Π»ΡŒ 1 ΠΈΠ³Ρ€ΠΎΠΊΠ° - Ρ‡Ρ‚ΠΎ Ρ‚Π°ΠΊΠΆΠ΅ являСтся Π½ΡƒΠΆΠ½ΠΎΠΉ, стартовой суммой 2 ΠΈΠ³Ρ€ΠΎΠΊΠ° findProfit: function(start, ratio){ return start.mul(ratio).sub(start); }, //Π‘ΡƒΠΌΠΌΠ° Π²Ρ‹Π²ΠΎΠ΄Π° ΠΈΠ³Ρ€ΠΎΠΊΠ° findOutcome: function(start, ratio){ return start.mul(ratio); } }; var N = math.fraction; var cof2Player = mt.findOtherRatio(N('1.55')); var money2Player = mt.findProfit(N('736'), N('1.55')); var t1 = mt.findOutcome(money2Player, cof2Player); // 1140.8 var t2 = mt.findOutcome(N('736'), N('1.55')); // 1140.8 

    For output you can use format :

     math.format(2.3, {notation: 'fixed', precision: 2}); // returns '2.30' 
    • right. Without tofixed () it turns out. 1 player has a conclusion - 1140.8; 2 players have a conclusion - 1140.7999999999997 And with toFixed () is not correct at all. But I need to conveniently provide and store data in the database, in the format of 2 numbers after the point. But equality does not agree. - Zow
    • You have an error somewhere accumulating. Exact calculations are a hurry to the rescue: mathjs.org/docs/datatypes/fractions.html . Alternatively, use the modulo comparison: Если abs(xy) < 1e-5, Ρ‚ΠΎ x == y. - pank
    • Thank. It is even more important for me to turn this logic on the server using php. - Zow
    • @Vadim, what about nodejs ? - pank
    • one
      @Vadim, s - sign (sign), n - numerator (numerator), d - denominator (denominator). - pank