Skip to content

Instantly share code, notes, and snippets.

@zunda
Last active June 4, 2022 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zunda/9d7bd054a5f5a0cf70b916292487a383 to your computer and use it in GitHub Desktop.
Save zunda/9d7bd054a5f5a0cf70b916292487a383 to your computer and use it in GitHub Desktop.
二進のπ
# 二進のπ
# 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