Floating point numbers¶
Myokit includes a module myokit.float
that contains methods to work with
floating point numbers.
These are used for example when checking if a pacing event has started or ended
to within machine precision, or when checking if two units are equivalent.
This module is imported automatically when import myokit
is run.
Comparison¶
- myokit.float.eq(a, b)¶
Checks if floating point numbers
a
andb
are equal, or so close to each other that the difference could be a single rounding error.
- myokit.float.geq(a, b)¶
Checks if
a >= b
, but usingmyokit.float.eq()
instead of==
.
- myokit.float.close(a, b, reltol=1e-09, abstol=1e-09)¶
Test whether two numbers are close enough to be considered equal.
Differs from
myokit.float.eq()
in that it tries to answer the question “are two numbers resulting from various calculations close enough to be considered equal”. Whereas :meth`myokit.float.eq` aims to deal with umbers that are numerically indistinguishable but still have a slightly different floating point representation.
Rounding to integer¶
- myokit.float.cround(x, reltol=1e-09, abstol=1e-09)¶
Checks if a float
x
is close to an integer withclose()
, and if so, returns that integer.
- myokit.float.round(x)¶
Checks if a float
x
is within a single rounding error of an integer, and if so, returns that integer.
Conversion to string¶
- myokit.float.str(number, full=False, precision=64)¶
Converts the given number to a string, using a full-precision representation if needed.
Arguments:
full
Set to
True
to force full precision output.precision
Set to
myokit.SINGLE_PRECISION
to assume single-precision numbers when formatting with full precision.