fix rounding issue

This commit is contained in:
David Westgate 2025-03-09 15:45:20 -07:00
parent 01303b625a
commit abdffcef71

View File

@ -50,8 +50,11 @@ const ChipDistributionSummary = ({
return current; return current;
}; };
const round = useCallback((num: number) => Math.round(num * 100) / 100, []);
// Bound for the value of the highest chip // Bound for the value of the highest chip
// This is somewhat arbitray and imperfect, but 1/3 to 1/5 is reasonable depending on the number of colors. // This is somewhat arbitray and imperfect, but 1/3 to 1/5 is reasonable depending on the number of colors.
// Could be possibly improved based on value of buy in amount
const maxDenomination: validDenomination = useMemo(() => { const maxDenomination: validDenomination = useMemo(() => {
let max: validDenomination; let max: validDenomination;
switch (totalChipsCount.length) { switch (totalChipsCount.length) {
@ -91,37 +94,6 @@ const ChipDistributionSummary = ({
[totalChipsCount, playerCount] [totalChipsCount, playerCount]
); );
// Redenominate the chips in case of failure to properly distribute.
// Move the shuffle index to the next lowest denomination, and keep all else same
const _redenominate = useCallback(
(
invalidDenomination: validDenomination[],
shuffleIndex: number
): validDenomination[] => {
let moved = false;
const newDenomination: validDenomination[] = [];
for (let i = invalidDenomination.length - 1; i >= 0; i--) {
if (i > shuffleIndex) {
newDenomination.push(invalidDenomination[i]);
} else if (i == shuffleIndex) {
newDenomination.push(invalidDenomination[i]);
} else if (i < shuffleIndex && !moved) {
const nextLowestDenominationIndex = Math.max(
validDenominations.indexOf(invalidDenomination[i]) - 1,
0
);
newDenomination.push(validDenominations[nextLowestDenominationIndex]);
moved = true;
} else {
newDenomination.push(invalidDenomination[i]);
}
}
newDenomination.reverse();
return newDenomination;
},
[]
);
// Dynamically set denominations and distributions from changing inputs // Dynamically set denominations and distributions from changing inputs
useEffect(() => { useEffect(() => {
let testDenomination: validDenomination[] = []; let testDenomination: validDenomination[] = [];
@ -148,24 +120,24 @@ const ChipDistributionSummary = ({
testDistribution.push(0); testDistribution.push(0);
} }
// Distribute the chips using the test denomination // Distribute the chips using the test denomination with a reverse fibbonaci preference
// If distribution fails to equal the buy-in, redenominate and re-try // Not optimal, nor correct under all inputs but works for most inputs
// Algorithm could be improved with more complexity and optimization // Algorithm could be improved with more complexity and optimization (re-tries, redenominating, etc.)
let remainingValue = buyInAmount; let remainingValue = buyInAmount;
let stop = false; let stop = false;
while (remainingValue > 0 && !stop) { while (remainingValue > 0 && !stop) {
let distributed = false; let distributed = false;
for (let i = numColors - 1; i >= 0; i = i - 1) { for (let i = numColors - 1; i >= 0; i = i - 1) {
if (testDistribution[i] < maxPossibleDistribution[i]) { for (
for ( let j = reverseFib[i];
let j = reverseFib[i]; j > 0 &&
j > 0 && remainingValue >= testDenomination[i]; remainingValue >= testDenomination[i] &&
j = j - 1 testDistribution[i] < maxPossibleDistribution[i];
) { j = j - 1
testDistribution[i] = testDistribution[i] + 1; ) {
remainingValue = remainingValue - testDenomination[i]; testDistribution[i] = testDistribution[i] + 1;
distributed = true; remainingValue = round(remainingValue - testDenomination[i]);
} distributed = true;
} }
} }
if (distributed == false) { if (distributed == false) {
@ -200,7 +172,7 @@ const ChipDistributionSummary = ({
</View> </View>
<View style={{ flexDirection: "row", justifyContent: "space-between" }}> <View style={{ flexDirection: "row", justifyContent: "space-between" }}>
<Text style={styles.p}> <Text style={styles.p}>
{i18n.t("total_value")}: {selectedCurrency} {totalValue.toFixed(2)} {i18n.t("total_value")}: {selectedCurrency} {round(totalValue)}
</Text> </Text>
<Text style={styles.p}> <Text style={styles.p}>
{selectedCurrency} {potValue} {i18n.t("pot")} {selectedCurrency} {potValue} {i18n.t("pot")}