Skip to content

Commit eccf231

Browse files
Arch D. Robisontkelman
authored andcommitted
Add manual section about numerical conversions.
(cherry picked from commit 7345a14) ref #14075
1 parent 1896a57 commit eccf231

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

doc/manual/mathematical-operations.rst

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,73 @@ Control flow ``&&`` followed by ``||`` followed by ``?``
347347
Assignments ``= += -= *= /= //= \= ^= ÷= %= |= &= $= <<= >>= >>>=`` and ``.+= .-= .*= ./= .//= .\= .^= .÷= .%=``
348348
================= =============================================================================================
349349

350+
.. _man-numerical-conversions:
351+
352+
Numerical Conversions
353+
---------------------
354+
355+
Julia supports three forms of numerical conversion, which differ in their
356+
handling of inexact conversions.
357+
358+
- The notation ``T(x)`` or ``convert(T,x)`` converts ``x`` to a value of type ``T``.
359+
360+
- If ``T`` is a floating-point type, the result is the nearest representable
361+
value, which could be positive or negative infinity.
362+
363+
- If ``T`` is an integer type, an ``InexactError`` is raised if ``x``
364+
is not representable by ``T``.
365+
366+
367+
- ``x % T`` converts an integer ``x`` to a value of integer type ``T``
368+
congruent to ``x`` modulo ``2^n``, where ``n`` is the number of bits in ``T``.
369+
In other words, the binary representation is truncated to fit.
370+
371+
- The :ref:`man-rounding-functions` take a type ``T`` as an optional argument.
372+
For example, ``round(Int,x)`` is a shorthand for ``Int(round(x))``.
373+
374+
The following examples show the different forms.
375+
376+
.. doctest::
377+
378+
julia> Int8(127)
379+
127
380+
381+
julia> Int8(128)
382+
ERROR: InexactError()
383+
in call at ./essentials.jl:58
384+
in eval at ./boot.jl:263
385+
386+
julia> Int8(127.0)
387+
127
388+
389+
julia> Int8(3.14)
390+
ERROR: InexactError()
391+
in call at ./essentials.jl:58
392+
in eval at ./boot.jl:263
393+
394+
julia> Int8(128.0)
395+
ERROR: InexactError()
396+
in call at ./essentials.jl:58
397+
in eval at ./boot.jl:263
398+
399+
julia> 127 % Int8
400+
127
401+
402+
julia> 128 % Int8
403+
-128
404+
405+
julia> round(Int8,127.4)
406+
127
407+
408+
julia> round(Int8,127.6)
409+
ERROR: InexactError()
410+
in trunc at ./float.jl:357
411+
in round at ./float.jl:177
412+
in eval at ./boot.jl:263
413+
414+
See :ref:`man-conversion-and-promotion` for how to define your own
415+
conversions and promotions.
416+
350417
.. _man-elementary-functions:
351418

352419
Elementary Functions
@@ -358,6 +425,8 @@ class of numerical values as permit sensible definitions, including
358425
integers, floating-point numbers, rationals, and complexes, wherever
359426
such definitions make sense.
360427

428+
.. _man-rounding-functions:
429+
361430
Rounding functions
362431
~~~~~~~~~~~~~~~~~~
363432

0 commit comments

Comments
 (0)