Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Ability to change rounding mode for all floats (cf. #2976) #3149

Merged
merged 11 commits into from
Sep 2, 2013
Prev Previous commit
Next Next commit
Documentation for the rounding modes (manual + stdlib)
It also includes some updates to some related sections about
BigFloats.
  • Loading branch information
andrioni committed Aug 28, 2013
commit 285b2389d80de9b0b778dfd7db192bb43fd66415
45 changes: 43 additions & 2 deletions doc/manual/integers-and-floating-point-numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,26 @@ argument respectively: ::
This example highlights the general principle that the adjacent representable
floating-point numbers also have adjacent binary integer representations.

Rounding modes
~~~~~~~~~~~~~~

If a number doesn't have an exact floating-point representation, it must be
rounded to an appropriate representable value, however, if wanted, the manner
in which this rounding is done can be changed according to the rounding modes
presented in the `IEEE 754 standard <http://en.wikipedia.org/wiki/IEEE_754-2008>`_::


julia> 1.1 + 0.1
1.2000000000000002

julia> with_rounding(RoundDown) do
1.1 + 0.1
end
1.2

The default mode used is always ``RoundNearest``, which rounds to the nearest
representable value, with ties rounded towards the nearest value with an even
least significant bit.

Background and References
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -503,10 +523,10 @@ Once created, they participate in arithmetic with all other numeric types thanks
123456789012345678901234567891

julia> BigFloat("1.23456789012345678901")
1.23456789012345678901
1.234567890123456789010000000000000000000000000000000000000000000000000000000004e+00 with 256 bits of precision

julia> BigFloat(2.0^66) / 3
24595658764946068821.3
2.459565876494606882133333333333333333333333333333333333333333333333333333333344e+19 with 256 bits of precision

julia> factorial(BigInt(40))
815915283247897734345611269596115894272000000000
Expand All @@ -531,6 +551,27 @@ However, type promotion between the primitive types above and

julia> typeof(y)
BigInt

The default precision (in number of bits of the significand) and rounding
mode of `BigFloat` operations can be changed, and all further calculations
will take these changes in account::

julia> with_bigfloat_rounding(RoundUp) do
BigFloat(1) + BigFloat("0.1")
end
1.100000000000000000000000000000000000000000000000000000000000000000000000000003e+00 with 256 bits of precision

julia> with_bigfloat_rounding(RoundDown) do
BigFloat(1) + BigFloat("0.1")
end
1.099999999999999999999999999999999999999999999999999999999999999999999999999986e+00 with 256 bits of precision

julia> with_bigfloat_precision(40) do
BigFloat(1) + BigFloat("0.1")
end
1.0999999999985e+00 with 40 bits of precision



.. _man-numeric-literal-coefficients:

Expand Down
21 changes: 20 additions & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2902,6 +2902,25 @@ Numbers
Create an arbitrary precision floating point number. ``x`` may be an ``Integer``, a ``Float64``, a ``String`` or a ``BigInt``. The
usual mathematical operators are defined for this type, and results are promoted to a ``BigFloat``.

.. function:: get_rounding()

Get the current floating point rounding mode. Valid modes are ``RoundNearest``, ``RoundToZero``, ``RoundUp`` and ``RoundDown``.

.. function:: set_rounding(mode)

Set the floating point rounding mode. See ``get_rounding`` for available modes

.. function:: with_rounding(f::Function,mode)

Change the floating point rounding mode for the duration of ``f``. It is logically equivalent to::

old = get_rounding()
set_rounding(mode)
f()
set_rounding(old)

See ``get_rounding`` for available rounding modes.

Integers
~~~~~~~~

Expand Down Expand Up @@ -2990,7 +3009,7 @@ The `BigFloat` type implements arbitrary-precision floating-point aritmetic usin

.. function:: get_bigfloat_rounding()

Get the current BigFloat rounding mode. Valid modes are ``RoundToNearest``, ``RoundToZero``, ``RoundUp``, ``RoundDown``, ``RoundAwayZero``
Get the current BigFloat rounding mode. Valid modes are ``RoundNearest``, ``RoundToZero``, ``RoundUp``, ``RoundDown``, ``RoundFromZero``

.. function:: set_bigfloat_rounding(mode)

Expand Down