Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sphinx floating point formatting

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?

like image 368
ctrlc Avatar asked Aug 31 '11 16:08

ctrlc


1 Answers

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.

like image 133
ddbeck Avatar answered Nov 19 '22 00:11

ddbeck