MahjongRepository/mahjong

Calcuating Fu should use integer division

Closed this issue · 5 comments

0xrgb commented

From mahjong/mahjong/hand_calculating/fu.py, line 158

def round_fu(self, fu_details):
	# 22 -> 30 and etc.
	fu = sum([x['fu'] for x in fu_details])
-	return int(math.ceil(fu / 10.0)) * 10
+	return (fu // 10) * 10

In addtion, there is no need to import math.

There is a same problem in mahjong/mahjong/hand_calculating/scores.py, too.

This one will not work :)

int(math.ceil(32 / 10.0)) * 10 is 40

(32 // 10) * 10 is 30

0xrgb commented

Oh I made a mistake. 😢
How about (fu + 9) // 10 * 10? (Score should be (score + 99) // 100 * 100)

It's ugly, but it works without using floating number.

When you wants to implement some huge score cacluation, such as aotenjou rule, it can cause problems because of floating point errors.

6ad207e

You're welcome :)

I did minor refactoring of hand calculation and I'm working on the code validation through tenhou replays (it already founded a couple of errors). Once it will be done I will release new version with documentation how to use this package

0xrgb commented

Thanks for quick fix! 😄

👍