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

Allow arguments of type Float16 and Float32 in R functions #124

Closed
wants to merge 4 commits into from

Conversation

devmotion
Copy link
Member

Currently it is not possible to call R functions such as nchisqlogpdf with arguments of type Float16 or Float32 which makes it impossible to use these types in e.g. NoncentralChisq in Distributions (JuliaStats/Distributions.jl#1387 (comment)). This PR fixes the problem.

src/rmath.jl Outdated
@@ -6,6 +6,8 @@ import Rmath: libRmath

### import macro

const FloatOrInt = Union{Float16,Float32,Float64,Int}
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to accept smaller integer types? Maybe we could even allow any Real, and let the conversion throw in case of overflow?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not know where to stop so I just started with the types that would fix the linked issue in Distributions 😄 I was wondering about smaller integer types as well. I think Real would break stuff and lead to method ambiguities since for some of these functions we already define Julia implementations that accept Real arguments in StatsFuns (they are used e.g. for Dual numbers).

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, then why not use the Julia implementations when they exist? Is it faster to call Rmath even if that involves conversions?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the motivation for #113 which I did not want to touch or duplicate in this PR. I haven't done any benchmarks but in my opinion performance should not be the main criterion for switching to Julia implementations. For instance, we should make sure that the Julia implementation is at least as accurate as the Rmath version, ideally on the whole input domain.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah #113 is complex enough that it's better to leave it alone. I'm just wondering what's the criterion to add some types to the list but not others. Maybe at least add small integer types?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added IntXs, UIntXs, and Rationals to the union as well since it would fix e.g. issues such as JuliaStats/Distributions.jl#1387 (comment). I am not particularly happy about the hardcoded union and hence used Real instead whenever no Julia fallback existed.

Copy link
Member

Choose a reason for hiding this comment

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

OK, thanks. Some Rational{Int} values cannot be converted to Float64 without a loss in precision, but maybe we don't care, just like promote_type(Int64, Float64) == Float64 despite the precision loss for large values?

It would be nice to test the new types. :-)

@codecov-commenter
Copy link

codecov-commenter commented Sep 5, 2021

Codecov Report

Merging #124 (2afff51) into master (fe65c00) will increase coverage by 4.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #124      +/-   ##
==========================================
+ Coverage   30.23%   34.25%   +4.01%     
==========================================
  Files          12       12              
  Lines         377      397      +20     
==========================================
+ Hits          114      136      +22     
+ Misses        263      261       -2     
Impacted Files Coverage Δ
src/rmath.jl 100.00% <100.00%> (ø)
src/distrs/norm.jl 97.29% <0.00%> (+2.70%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe65c00...2afff51. Read the comment docs.

@@ -109,6 +140,9 @@ macro import_rmath(rname, jname, pargs...)
esc(_import_rmath(rname, jname, pargs))
end

macro import_rmath_real(rname, jname, pargs...)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
macro import_rmath_real(rname, jname, pargs...)
# Used when no Julia fallback exists currently
macro import_rmath_real(rname, jname, pargs...)

src/rmath.jl Outdated
@@ -6,6 +6,8 @@ import Rmath: libRmath

### import macro

const FloatOrInt = Union{Float16,Float32,Float64,Int}
Copy link
Member

Choose a reason for hiding this comment

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

OK, thanks. Some Rational{Int} values cannot be converted to Float64 without a loss in precision, but maybe we don't care, just like promote_type(Int64, Float64) == Float64 despite the precision loss for large values?

It would be nice to test the new types. :-)

@devmotion
Copy link
Member Author

Replaced and fixed by #125.

@devmotion devmotion closed this Sep 28, 2021
@devmotion devmotion deleted the dw/rmath_float16+32 branch September 28, 2021 12:03
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.

3 participants