【问题标题】:How do I convert float decimal to float octal/binary?如何将浮点十进制转换为浮点八进制/二进制?
【发布时间】:2017-02-23 10:12:29
【问题描述】:

我到处搜索以找到将浮点数转换为八进制或二进制的方法。我知道float.hexfloat.fromhex。是否有可以对八进制/二进制值执行相同工作的模块?

例如:我有一个浮点数12.325,我应该得到浮点数八进制14.246。告诉我,我该怎么做?提前致谢。

【问题讨论】:

  • 这是干什么用的?你能举一些期望的输入和输出的例子吗?用八进制表示浮点值是一件非常不寻常的事情。对于二进制,您可以简单地使用float.hex,然后使用字符串操作将每个十六进制数字替换为其等效的 4 位二进制。
  • @MarkDickinson 例如:我有一个浮点数12.325,我应该得到浮点数八进制14.246(四舍五入)。我不知道该怎么做
  • 感谢您的示例;将其编辑到问题本身会很有用。另外,你能解释一下“浮点八进制”是什么意思吗?您是否有特定的目标浮点表示,或者您只是想要一个用八进制编写的值,同时包含整数部分和小数部分?
  • @MarkDickinson 我只需要这个值。用作计算器
  • 如何四舍五入到246?

标签: python binary floating-point octal


【解决方案1】:

你可以自己写,如果你只关心小数点后三位,那么将 n 设置为 3:

def frac_to_oct(f, n=4):
    # store the number before the decimal point
    whole = int(f)
    rem = (f - whole) * 8
    int_ = int(rem)
    rem = (rem - int_) * 8
    octals = [str(int_)]
    count = 1
    # loop until 8 * rem gives you a whole num or n times
    while rem and count < n:
        count += 1
        int_ = int(rem)
        rem = (rem - int_) * 8
        octals.append(str(int_))
    return float("{:o}.{}".format(whole, "".join(octals)))

使用您的输入 12.325

In [9]: frac_to_oct(12.325)
Out[9]: 14.2463
In [10]: frac_to_oct(121212.325, 4)
Out[10]: 354574.2463

In [11]: frac_to_oct(0.325, 4)
Out[11]: 0.2463
In [12]: frac_to_oct(2.1, 4)
Out[12]: 2.0631
In [13]:  frac_to_oct(0)
Out[13]: 0.0
In [14]:  frac_to_oct(33)
Out[14]: 41.0

【讨论】:

  • 咳咳...frac(12.325) 给了我10,24631。有问题,但我没看到
  • @MaxLunar。嗯,它不像你所看到的那样。当然,除非您的语言环境可能会影响输出
  • 这个“解决方案”不起作用。它给出了错误的值。
  • 12.325 应该返回 14.246,而不是 10.24631。
  • @Frogboxe,我错过了转换整数,而且 应该返回 14.246 是错误的,它取决于您决定的精度或 8 * rem 给您一个整数跨度>
【解决方案2】:

解决办法如下,解释如下:

def ToLessThanOne(num): # Change "num" into a decimal <1
    while num > 1:
        num /= 10
    return num

def FloatToOctal(flo, places=8): # Flo is float, places is octal places
    main, dec = str(flo).split(".") # Split into Main (integer component)
                                    # and Dec (decimal component)
    main, dec = int(main), int(dec) # Turn into integers

    res = oct(main)[2::]+"." # Turn the integer component into an octal value
                             # while removing the "ox" that would normally appear ([2::])
                             # Also, res means result

    # NOTE: main and dec are being recycled here

    for x in range(places): 
        main, dec = str((ToLessThanOne(dec))*8).split(".") # main is integer octal
                                                           # component
                                                           # dec is octal point
                                                           # component
        dec = int(dec) # make dec an integer

        res += main # Add the octal num to the end of the result

    return res # finally return the result

所以你可以做print(FloatToOctal(12.325)),它会打印出14.246314631

最后,如果您想要更少的八进制位(小数位但八进制),只需添加 places 参数:print(FloatToOctal(12.325, 3)),根据本网站返回 14.246 是正确的:http://coderstoolbox.net/number/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2014-11-24
    • 2021-09-10
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2014-03-01
    相关资源
    最近更新 更多