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

float, real, complex: impove docs, move docs inline, and add doctest examples #19166

Merged
merged 1 commit into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,25 @@ Return both the real and imaginary parts of the complex number `z`.
"""
reim(z) = (real(z), imag(z))

"""
real(T::Type)

Returns the type that represents the real part of a value of type `T`.
e.g: for `T == Complex{R}`, returns `R`.
Equivalent to `typeof(real(zero(T)))`.

```jldoctest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an excessive number of examples for such a simple function.

julia> real(Complex{Int})
Int64

julia> real(Float64)
Float64
```
"""
real(T::Type) = typeof(real(zero(T)))
real{T<:Real}(::Type{T}) = T
real{T<:Real}(::Type{Complex{T}}) = T

complex{T<:Real}(::Type{T}) = Complex{T}
complex{T<:Real}(::Type{Complex{T}}) = Complex{T}

isreal(x::Real) = true
isreal(z::Complex) = imag(z) == 0
"""
Expand All @@ -75,9 +88,31 @@ isfinite(z::Complex) = isfinite(real(z)) & isfinite(imag(z))
isnan(z::Complex) = isnan(real(z)) | isnan(imag(z))
isinf(z::Complex) = isinf(real(z)) | isinf(imag(z))

complex(x::Real, y::Real) = Complex(x, y)
complex(x::Real) = Complex(x)
"""
complex(r, [i])

Convert real numbers or arrays to complex. `i` defaults to zero.
"""
complex(z::Complex) = z
complex(x::Real) = Complex(x)
complex(x::Real, y::Real) = Complex(x, y)

"""
complex(T::Type)

Returns an appropriate type which can represent a value of type `T` as a complex number.
Equivalent to `typeof(complex(zero(T)))`.

```jldoctest
julia> complex(Complex{Int})
Complex{Int64}

julia> complex(Int)
Complex{Int64}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two examples should suffice.

"""
complex{T<:Real}(::Type{T}) = Complex{T}
complex{T<:Real}(::Type{Complex{T}}) = Complex{T}

flipsign(x::Complex, y::Real) = ifelse(signbit(y), -x, x)

Expand Down
15 changes: 0 additions & 15 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -950,13 +950,6 @@ The text is assumed to be encoded in UTF-8.
"""
eachline

"""
complex(r, [i])

Convert real numbers or arrays to complex. `i` defaults to zero.
"""
complex

"""
Mmap.Anonymous(name, readonly, create)

Expand Down Expand Up @@ -3129,14 +3122,6 @@ Unicode string.)
"""
reverseind

"""
float(x)

Convert a number, array, or string to a `AbstractFloat` data type. For numeric data, the
smallest suitable `AbstractFloat` type is used. Converts strings to `Float64`.
"""
float

"""
signbit(x)

Expand Down
21 changes: 20 additions & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,28 @@ convert(::Type{AbstractFloat}, x::UInt32) = convert(Float64, x)
convert(::Type{AbstractFloat}, x::UInt64) = convert(Float64, x) # LOSSY
convert(::Type{AbstractFloat}, x::UInt128) = convert(Float64, x) # LOSSY

"""
float(x)

Convert a number or array to a floating point data type.
When passed a string, this function is equivalent to `parse(Float64, x)`.
"""
float(x) = convert(AbstractFloat, x)

# for constructing arrays
"""
float(T::Type)

Returns an appropriate type to represent a value of type `T` as a floating point value.
Equivalent to `typeof(float(zero(T)))`.

```jldoctest
julia> float(Complex{Int})
Complex{Float64}

julia> float(Int)
Float64
```
"""
float{T<:Number}(::Type{T}) = typeof(float(zero(T)))

for Ti in (Int8, Int16, Int32, Int64)
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Data Formats

.. Docstring generated from Julia source

Convert a number, array, or string to a ``AbstractFloat`` data type. For numeric data, the smallest suitable ``AbstractFloat`` type is used. Converts strings to ``Float64``\ .
Convert a number or array to a floating point data type. When passed a string, this function is equivalent to ``parse(Float64, x)``\ .

.. function:: significand(x)

Expand Down