Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active February 21, 2021 01:26
Show Gist options
  • Save nattybear/f6904d32b428f3eabb65f9c5440c59b5 to your computer and use it in GitHub Desktop.
Save nattybear/f6904d32b428f3eabb65f9c5440c59b5 to your computer and use it in GitHub Desktop.
파이썬 분수

파이썬 분수

파이썬에서 분수를 쉽게 다루려면 내장 라이브러리 fractions를 사용합니다.

import fractions

x = fractions.Fraction(3, 8)

print(x)  # Fraction(3, 8)

이 때 Fraction이 가분수를 대분수로 표현해주는 것은 없습니다.

다만 floor 함수로 대분수의 자연수 부분을 따로 구할 수는 있습니다.

improper = Fraction(8, 3)

natural = floor(improper)

print(natural)  # 2

대분수를 표현하려면 직접 구현을 하시거나 다른 사람이 만든 라이브러리를 찾아 써야 합니다.

스택 오버 플로우 관련 질문과 답변 링크를 공유합니다.

감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment