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

Specialize inv #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Specialize inv #7

wants to merge 2 commits into from

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Sep 1, 2021

Given that many of the constants defined here are reciprocals of each other, we may specialize inv and literal_pow to improve floating-point accuracy.

On main

ulia> sqrt2π*invsqrt2π
1.0000000000000002

julia> sqrt2*invsqrt2
1.0000000000000002

This PR

julia> sqrt2π*invsqrt2π == 1
true

julia> sqrt2*invsqrt2 == 1
true

@JeffreySarnoff
Copy link
Member

before going into your PR more deeply, the else clause below is suspect

if VERSION > v"1.6.0"
    _one(x) = one(x)
else
    _one(x) = true
end

as is this result in v1.6.1

julia> sqrt2π * invsqrt2π
true

@devmotion
Copy link
Member

one and zero for AbstractIrrationals was added in JuliaLang/julia#34773, so this is the expected result in recent Julia versions.

@JeffreySarnoff
Copy link
Member

Good; still shouldn't sqrt2π * invsqrt2π === true be avoided?

@devmotion
Copy link
Member

The advantage of true is that it is the least invasive identity. I assume this was also the motivation behind the definition in base.

@jishnub
Copy link
Member Author

jishnub commented Sep 2, 2021

Yes indeed, that's the idea, true is equal to 1 and sits the lowest in the promotion hierarchy for Real numbers. If this is not desired, perhaps one may define an analogous real identity type that isn't a Bool but gets promoted identically.

@JeffreySarnoff
Copy link
Member

Where would this approach leave e.g. sqrt4π * invsqrt2π? Is there a more general approach that covers that and similar cases? Would handling those relations be worth the overhead [i.e. what would be a parsimonious and effective way to handle exact integer ratios and their reciprocal results]?

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