MahjongRepository / mahjong

Implementation of riichi mahjong related stuff (hand cost, shanten, agari end, etc.)
MIT License
381 stars 40 forks source link

Calcuating Fu should use integer division #1

Closed 0xrgb closed 7 years ago

0xrgb commented 7 years ago

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.

Nihisil commented 7 years ago

This one will not work :)

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

(32 // 10) * 10 is 30

0xrgb commented 7 years ago

Oh I made a mistake. :cry: 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.

Nihisil commented 7 years ago

https://github.com/MahjongRepository/mahjong/commit/6ad207ebe45079f0522ed6ae1a023863fa20a439

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 7 years ago

Thanks for quick fix! :smile:

Nihisil commented 7 years ago

:+1: