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

Make FD÷FD return an FD. #40

Merged
merged 6 commits into from
Feb 12, 2019
Merged

Make FD÷FD return an FD. #40

merged 6 commits into from
Feb 12, 2019

Conversation

NHDaly
Copy link
Member

@NHDaly NHDaly commented Nov 29, 2018

Before this, FD{T,f} ÷ FD{T,f} returned T, now it returns FD{T,f}.

Same for div, fld, and fld.


Here's an example of the old behavior. Addition and division for two FixedDecimals returns a FixedDecimal, but integer division (div) was returning the raw storage type T instead of wrapping it in a FixedDecimal.

julia> FixedDecimal{Int32, 2}(2)+FixedDecimal{Int32, 2}(1)
>> FixedDecimal{Int32,2}(3.00)

julia> FixedDecimal{Int32,2}(4.00) / FixedDecimal{Int32,2}(2.00)
>> FixedDecimal{Int32,2}(2.00)

julia> FixedDecimal{Int32, 2}(2)÷FixedDecimal{Int32, 2}(1)
>> 2

Before this, `FD{T,f} ÷ FD{T,f}` returned `T`, now it returns `FD{T,f}`.

Same for `div`, `fld`, and `fld`.
@coveralls
Copy link

coveralls commented Nov 29, 2018

Coverage Status

Coverage increased (+0.02%) to 96.471% when pulling 1819dd6 on NHDaly:div_return_FD into 07d24d9 on JuliaMath:master.

@codecov-io
Copy link

codecov-io commented Nov 29, 2018

Codecov Report

Merging #40 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #40      +/-   ##
==========================================
+ Coverage   98.82%   98.83%   +<.01%     
==========================================
  Files           1        1              
  Lines         170      171       +1     
==========================================
+ Hits          168      169       +1     
  Misses          2        2
Impacted Files Coverage Δ
src/FixedPointDecimals.jl 98.83% <100%> (ø) ⬆️

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 1768c58...dceec29. Read the comment docs.

There weren't any tests of `div` before, so this adds some simple ones.
@NHDaly
Copy link
Member Author

NHDaly commented Nov 29, 2018

Also, I noticed that there weren't any unit tests for div, so I added a few simple ones.

@TotalVerb
Copy link
Collaborator

One advantage of returning T is that there's no danger of overflow, whereas when returning the FD there is. Is there a reason why the FD is desirable here?

@omus
Copy link
Contributor

omus commented Nov 30, 2018

Since / returns a FD I do think it makes sense to do the same for div. We probably do need to have some of the widening logic from / here as well though.

@NHDaly
Copy link
Member Author

NHDaly commented Dec 3, 2018

One advantage of returning T is that there's no danger of overflow, whereas when returning the FD there is. Is there a reason why the FD is desirable here?

Yeah, this is a good point, @TotalVerb. By returning a T right now, we avoid overflows in places where it would happen otherwise, e.g.:

julia> FixedPointDecimals.FixedDecimal{Int16, 4}(1) ÷ FixedPointDecimals.FixedDecimal{Int16, 4}(0.0005)
>> 2000

julia> FixedPointDecimals.FixedDecimal{Int16, 4}(2000)
ERROR: InexactError: trunc(Int16, 20000000)

That said, I agree with omus that since all the other operations (such as /) return an FD, I think it makes sense here as well. For example, if + returned an integer it, too, could avoid overflows but that wouldn't really make sense. That said, this one is a bit different than + since it has to do a bit of work to convert the result back to a FD when the actual operation itself causes you to produce the integer representation... But I dunno, it just seems weird for users to need to remember to convert back to an FD every time they integer divide them. And if the user really wants to get the int representation back, they can always just divide their internal values: x.i ÷ y.i?


@omus commented 3 days ago
We probably do need to have some of the widening logic from / here as well though.

Actually, I don't think we do, although it would probably be worth adding a comment explaining why we don't.
When you divide the two FixedDecimals, is you automatically eliminate all of the fractional part of the answer (FD{Int,2}(3) ÷ FD{Int,2}(2) --> 300÷200 == 1).

/ needs to widen the inputs and multiply by 10^f ahead of time, specifically to avoid eliminating the fractional part of the answer, since we don't want to lose precision. But in the case of truncating integer division (÷), "eliminating the fractional part of the answer" is exactly what we want! So it should be enough as is, I think.

@NHDaly
Copy link
Member Author

NHDaly commented Dec 3, 2018

Maybe the comment could be something like:

    # Note that div(x.i, y.i) eliminates the scaling coefficient, so we need to reconstruct the FD.

@NHDaly
Copy link
Member Author

NHDaly commented Dec 5, 2018

Added a comment here to explain why we don't need the widening logic. Does this look okay?:

for divfn in [:div, :fld, :fld1]
# div(x.i, y.i) eliminates the scaling coefficient, so we call the FD constructor.
# We don't need any widening logic, since we won't be multiplying by the coefficient.
@eval $divfn(x::T, y::T) where {T <: FD} = T($divfn(x.i, y.i))
end

Copy link
Contributor

@omus omus left a comment

Choose a reason for hiding this comment

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

Tests need a little work otherwise I'm happy to merge.

Copy link
Member Author

@NHDaly NHDaly left a comment

Choose a reason for hiding this comment

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

Please take another look; should be good to merge now, i think! :)

@omus omus merged commit 315e5cb into JuliaMath:master Feb 12, 2019
@NHDaly NHDaly deleted the div_return_FD branch February 12, 2019 16:23
@NHDaly NHDaly mentioned this pull request Mar 25, 2019
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.

5 participants