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

Remove the type constraint on sincosd #41021

Merged
merged 5 commits into from
Jun 5, 2021
Merged

Remove the type constraint on sincosd #41021

merged 5 commits into from
Jun 5, 2021

Conversation

singularitti
Copy link
Contributor

Closes #41017

@singularitti
Copy link
Contributor Author

singularitti commented May 31, 2021

I can't add test here, so I just paste the test in this PR:

julia> using Unitful, Test

julia> sincosd(5u"°")
ERROR: MethodError: no method matching sincosd(::Quantity{Int64, NoDims, Unitful.FreeUnits{(°,), NoDims, nothing}})
Closest candidates are:
  sincosd(::Real) at special/trig.jl:1249
  sincosd(::Missing) at special/trig.jl:1261
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

julia> function Base.sincosd(x)
           if isinf(x)
               return throw(DomainError(x, "sincosd(x) is only defined for finite `x`."))
           elseif isnan(x)
               return (oftype(x,NaN), oftype(x,NaN))
           end

           # It turns out that calling those functions separately yielded better
           # performance than considering each case and calling `sincos_kernel`.
           return (sind(x), cosd(x))
       end

julia> sincosd(5u"°")
(0.08715574274765818, 0.9961946980917455)

julia> @test sind(5u"°") == sind(5) == 0.08715574274765818
Test Passed

julia> @test cosd(5u"°") == cosd(5) == 0.9961946980917455
Test Passed

stevengj
stevengj previously approved these changes May 31, 2021
base/special/trig.jl Outdated Show resolved Hide resolved
@stevengj
Copy link
Member

stevengj commented May 31, 2021

Wouldn't it make more sense to change the type to ::Number?

@singularitti
Copy link
Contributor Author

Wouldn't it make more sense to change the type to ::Number?

sincos has a method that applies to ::Any.

@stevengj
Copy link
Member

stevengj commented Jun 1, 2021

sincos has a method that applies to ::Any.

Yes, but that method actually works for Any. For example, it works fine for sincos(A) where A is a square matrix.

The method defined here will only ever work for numbers.

@singularitti
Copy link
Contributor Author

Ok, I think Number sounds fine.

@imciner2
Copy link
Contributor

imciner2 commented Jun 1, 2021

sincos has a method that applies to ::Any.

Yes, but that method actually works for Any. For example, it works fine for sincos(A) where A is a square matrix.

The method defined here will only ever work for numbers.

Didn't we get sind and cosd for square matrices in PR #39758 that was merged? The sincosd function here just calls sind and cosd with the argument, so I would think it should work for square matrices as well.

base/special/trig.jl Outdated Show resolved Hide resolved
@stevengj
Copy link
Member

stevengj commented Jun 1, 2021

Now that the function has been redefined to just call through to (sind(x), cosd(x)) with no other computations, I agree that Any makes sense.

base/special/trig.jl Outdated Show resolved Hide resolved
@sostock
Copy link
Contributor

sostock commented Jun 2, 2021

I can't add test here

You could add tests with Complex numbers and/or matrices.

@singularitti
Copy link
Contributor Author

I can't add test here

You could add tests with Complex numbers and/or matrices.

I actually want to add tests against Unitful degrees. I don't know whether I can call external libraries like Unitful.

@sostock
Copy link
Contributor

sostock commented Jun 2, 2021

I actually want to add tests against Unitful degrees. I don't know whether I can call external libraries like Unitful.

No, we don’t use Unitful in the tests (and I don’t think it makes sense to test the functionality provided by Unitful in the Base tests). You could use the Furlongs module (you would have to define sind(::Furlong{0}) and cosd(::Furlong{0})), but since sincosd just calls sind/cosd, I think it suffices to test that it works for other types in Base that have methods for sind/cosd.

@singularitti
Copy link
Contributor Author

Can someone tell me should sind(5u"°") and sind(5) return the same results? We are having different opinions here.

@oscardssmith
Copy link
Member

I'm pretty sure that sin(5u"°") == sind(5)is what we want (note the first is sin not sind).

@singularitti
Copy link
Contributor Author

I'm pretty sure that sin(5u"°") == sind(5)is what we want (note the first is sin not sind).

What about sind(5u"°")?

@oscardssmith
Copy link
Member

I think sind(5u"°")==sin(5u"°") since sind's documentation says that it "Compute sine of x, where x is in degrees." and 5u"°" is already in degrees. That said, I don't think you should ever call sind(5u"°"), and that it possibly should throw a methodError. If you want sin in degrees, and you have a type for creating numbers in degrees, that seems like the right way to do this.

In fact, sind and friends are arguably fairly un-Julian API. It's much more natural to define a type for degrees, and make sin work on it, than have 12 extra functions that conceptually do the same thing. These functions were added in 2013 (Julia 0.2), which was well before the ecosystem existed, and before some of the best practices for effectively using multiple dispatch were solidified. We probably can't delete these functions for 2.0, but I would kind of like to.

@singularitti
Copy link
Contributor Author

It's much more natural to define a type for degrees

I agree. There should be a native Julia type for Degree.

@stevengj
Copy link
Member

stevengj commented Jun 3, 2021

See #26362 for discussion of a Degree type.

@oscardssmith
Copy link
Member

That's unfortunate. I wonder if it's still possible to do this.

@stevengj stevengj dismissed their stale review June 3, 2021 16:36

see comments above

@vtjnash vtjnash added the merge me PR is reviewed. Merge when all tests are passing label Jun 3, 2021
@oscardssmith oscardssmith merged commit f9aa058 into JuliaLang:master Jun 5, 2021
@oscardssmith oscardssmith added linalg triage and removed merge me PR is reviewed. Merge when all tests are passing labels Jun 5, 2021
@singularitti singularitti deleted the issue41017 branch June 5, 2021 04:59
shirodkara pushed a commit to shirodkara/julia that referenced this pull request Jun 9, 2021
Co-authored-by: Jameson Nash <[email protected]>
Co-authored-by: Steven G. Johnson <[email protected]>
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
Co-authored-by: Jameson Nash <[email protected]>
Co-authored-by: Steven G. Johnson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove the type constraint on sincosd?
6 participants