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

Minimise rounding error for Real input to Periodic #14

Open
bencottier opened this issue Feb 9, 2021 · 1 comment
Open

Minimise rounding error for Real input to Periodic #14

bencottier opened this issue Feb 9, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@bencottier
Copy link
Contributor

bencottier commented Feb 9, 2021

For TimeType data, Periodic transform uses the _periodic function to compute the periodic function for each instant in time.

The purpose of the _periodic function is to find an "origin" in time that minimises rounding error. This is particularly important when working with dates, because the calculations are done on milliseconds, and the number of milliseconds since "year 0" is enormous.

julia> sin(2π)
-2.4492935982947064e-16

julia> sin(2π*100)
3.928773447456944e-15

julia> x = Millisecond(DateTime(2021, 02, 09) - DateTime(0)).value
63780048000000

julia> sin(2π*x)
-0.020117088546809787

So _periodic finds the most recent time, period_begin, such that period_begin / period is a whole number, and in turn sin(2π * period_begin / period) ≈ 0 with the highest precision.

But this consideration could just as well apply to Real numbers, albeit less often in practice. So we should accomodate Real input in _periodic or define a new equivalent method, e.g.

function _periodic(f, instant, period, phase_shift=0)
    return f(2π * (mod(instant, period) - phase_shift) / period)
end

Examples:

julia> _periodic(sin, 7/4, 7.)
1.0

julia> _periodic(sin, 0, 7.)
0.0

julia> _periodic(sin, 7e14, 7.)
0.0

julia> _periodic(sin, 7e14 + 7/4, 7.)
1.0
@bencottier bencottier added the enhancement New feature or request label Feb 9, 2021
@bencottier
Copy link
Contributor Author

Note that even in my example implementation, performance degrades for large enough input:

julia> _periodic(sin, 7e16 + 7/4, 7)
0.0

This might be a more fundamental issue that we can't reasonably avoid.

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

No branches or pull requests

1 participant