1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

小道具:固定小数点を10進文字列にする

Last updated at Posted at 2022-03-04
Python
# x=整数(正を想定), b=小数部のビット数, p=小数点以下の桁数【丸めなし、切り捨て】
# p=-1 は b 桁
# p=0 は int(b * 0.301) 桁
# (p > 0) は p 桁
fixdec = (lambda x, b, p=0: (lambda x, b, p, f: f(x >> b, x & ((1 << b) - 1), b, p))
          (x, b, (b, int(b * 0.301), p)[min(2, p + 1)],
           lambda h, l, b, p: (('%d.' % h) + ('%d' % (((l | (1 << b)) * (10**p)) >> b))[1:])))
テスト
>>> for p in [0, 32, -1]: print(fixdec(1, 64, p))                                               ... 
0.0000000000000000000
0.00000000000000000005421010862427
0.0000000000000000000542101086242752217003726400434970855712890625
>>> for p in [0, 48, -1]: print(fixdec(1, 96, p))        
... 
0.0000000000000000000000000000
0.000000000000000000000000000012621774483536188886
0.000000000000000000000000000012621774483536188886587657044524579674771302961744368076324462890625
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?