Skip to content

Conversation

adamnemecek
Copy link

No description provided.

@ararslan
Copy link
Member

Cool, thanks for the contribution. Could you add tests?

src/dual.jl Outdated

Base.zero(::Type{Dual{T}}) where {T} = Dual(zero(T), zero(T))
Base.zero(x::Dual{T}) where {T} = zero(T)
Base.iszero(z::Dual{T}) where {T} = iszero(value(z)) & iszero(epsilon(z))
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason not to use short-circuiting && here and for isone?

src/dual.jl Outdated

Base.zero(::Type{Dual{T}}) where {T} = Dual(zero(T), zero(T))
Base.zero(x::Dual{T}) where {T} = zero(T)
Base.iszero(z::Dual{T}) where {T} = iszero(value(z)) & iszero(epsilon(z))
Copy link
Contributor

Choose a reason for hiding this comment

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

This definition of iszero is inconsistent with how == 0 is defined for duals:

Base.:(==)(z::Dual, x::Number) = value(z) == x

src/dual.jl Outdated
Base.round(::Type{T}, z::Dual) where {T<:Real} = round(T, value(z))

Base.zero(::Type{Dual{T}}) where {T} = Dual(zero(T), zero(T))
Base.zero(x::Dual{T}) where {T} = zero(T)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why should these two versions of zero return different types?

Base.round(::Type{T}, z::Dual) where {T<:Real} = round(T, value(z))

Base.zero(::Type{Dual{T}}) where {T} = Dual(zero(T), zero(T))
Base.zero(x::Dual{T}) where {T} = zero(typeof(x))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not

Suggested change
Base.zero(x::Dual{T}) where {T} = zero(typeof(x))
Base.zero(::Dual{T}) where {T} = zero(Dual{T})

Copy link
Contributor

@briochemc briochemc left a comment

Choose a reason for hiding this comment

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

A micro suggestion 🙂

using LinearAlgebra
import DualNumbers: value
import NaNMath
import Base: one, isone, zero, iszero
Copy link
Member

Choose a reason for hiding this comment

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

These are exported from base, so this is unnecessary.

Suggested change
import Base: one, isone, zero, iszero

Copy link
Author

Choose a reason for hiding this comment

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

You need to explicitly import it to extend it. Try this.

struct ABC end
one(x::ABC) = 1

Will give you the error

ERROR: error in method definition: function Base.one must be explicitly imported to be extended
Stacktrace:
[1] top-level scope at none:0

Copy link
Member

Choose a reason for hiding this comment

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

I know that, but they are not being extended in the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants