狮身人面像浮点格式化 [英] Sphinx floating point formatting

查看:156
本文介绍了狮身人面像浮点格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Sphinx从代码生成文档。有谁知道是否有一种方法来控制从默认参数生成的浮点数的格式。



例如,如果我有以下功能:

$ $ p $ def f (x = 0.97):
return x + 1

生成的文档看起来像:

  foo(x = 0.96999999999997)



显然这是一个浮点精度问题,但是有没有办法使文档看起来不那么难看?

解决您可以用 .. autofunction :: 指令覆盖函数签名。因此,为了解决你的例子,在模块 bar 中定义为 foo(x = 0.97)的函数:

  .. automodule :: bar 

.. autofunction :: foo(x = 0.97)

结果文档将使用提供的签名而不是解释版本,
$ b

你可以用 .. autoclass :: 。automethod :: 狮身人面像这部分中的选项和高级用法中记录的用法。 ext.autodoc docs。


I'm using Sphinx to generate documentation from code. Does anyone know if there is a way to control the formatting of floating point numbers generated from default arguments.

For example if I have the following function:

def f(x = 0.97):
    return x+1

The generated documentation ends up looking like:

foo(x = 0.96999999999997)

Obviously this is a floating point precision issue, but is there a way to make the documentation not look so ugly?

解决方案

You can override a function signature with the .. autofunction:: directive. So to address your example, a function defined as foo(x=0.97) in module bar:

.. automodule:: bar

   .. autofunction:: foo(x=0.97)

And the resulting doc will use the signature provided instead of the interpreted version with the really long number.

You can do this equivalently with .. autoclass:: and .. automethod:: and the like. This is usage is documented in "Options and advanced usage" in this part of the sphinx.ext.autodoc docs.

这篇关于狮身人面像浮点格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆