From 619644d6ddc7488aabd0d576d2d63003309f3bc0 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 5 Apr 2022 16:00:26 -0500 Subject: [PATCH] Run binary search until max precision --- common/calculate-cpmm.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/common/calculate-cpmm.ts b/common/calculate-cpmm.ts index ab6fd3c5..0bf16bf0 100644 --- a/common/calculate-cpmm.ts +++ b/common/calculate-cpmm.ts @@ -141,16 +141,18 @@ function calculateCpmmShareValue( const k = computeK(pool.YES, pool.NO, p) const otherPool = outcome === 'YES' ? pool.NO : pool.YES - let lowAmount = 0 // Constrain the max sale value to the lessor of 1. shares and 2. the other pool. // This is because 1. the max value per share is M$ 1, // and 2. The other pool cannot go negative and the sale value is subtracted from it. // (Without this, there are multiple solutions for the same k.) let highAmount = Math.min(shares, otherPool) + let lowAmount = 0 let mid = 0 let kGuess = 0 - while (Math.abs(k - kGuess) > 0.00000000001) { + while (true) { mid = lowAmount + (highAmount - lowAmount) / 2 + + // Break once we've reached max precision. if (mid === lowAmount || mid === highAmount) break kGuess = sellSharesK(pool.YES, pool.NO, p, shares, outcome, mid)