Last active
June 4, 2022 08:24
-
-
Save zunda/9d7bd054a5f5a0cf70b916292487a383 to your computer and use it in GitHub Desktop.
二進のπ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 二進のπ | |
# Inspired from https://twitter.com/indozou/status/1520073427172093952 | |
# | |
# Copyright 2022 <zundan at gmail.com> | |
# | |
# Permission is granted for use, copying, modification, distribution, | |
# and distribution of modified versions of this work as long as the | |
# above copyright notice is included. | |
class Numeric | |
def to_s_radix(radix = 10, n_frac = 31) | |
x = self.abs | |
r = self < 0 ? "-" : "" | |
c = x.floor | |
r += c.to_s(radix) | |
x -= c | |
r += "." if x > 0 | |
while x > 0 and n_frac > 0 | |
x *= radix | |
c = x.floor | |
r += c.to_s(radix) | |
x -= c | |
n_frac -= 1 | |
end | |
return r | |
end | |
end | |
puts Math::PI.to_s_radix(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment