Skip to content

New attempt at preserving the floating-point precision in uconvert#782

Merged
sostock merged 5 commits intoJuliaPhysics:masterfrom
sostock:uconvert-float-again
Jun 8, 2025
Merged

New attempt at preserving the floating-point precision in uconvert#782
sostock merged 5 commits intoJuliaPhysics:masterfrom
sostock:uconvert-float-again

Conversation

@sostock
Copy link
Copy Markdown
Collaborator

@sostock sostock commented May 15, 2025

Closes #753.

This is a follow-up to #754. It should now work without breaking something.

@eliascarv I took the first commits from #754 and added the floattype function.

We should also look at our promote_rule methods to see whether we can use this to address #767.

F = floattype(T)
# Since conversion factors only have Float64 precision,
# there is no point in converting to BigFloat
convert(F == BigFloat ? Float64 : F, cf)
Copy link
Copy Markdown
Contributor

@eliascarv eliascarv May 17, 2025

Choose a reason for hiding this comment

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

Perhaps using <: instead of == is safer to prevent possible BligFloat type changes.

Suggested change
convert(F == BigFloat ? Float64 : F, cf)
convert(F <: BigFloat ? Float64 : F, cf)

What do you think @sostock?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it is unlikely that BigFloat will be changed to a non-concrete type, but we can use <: if you prefer it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you're right. Perhaps just change== to === which is more common to compare types.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don’t really like using === on types, because the same type can have different representations (although it shouldn’t be a problem in this case because BigFloat is a concrete type):

julia> (Rational{T} where T) == (Rational{S} where S)
true

julia> (Rational{T} where T) === (Rational{S} where S)
false

I also didn’t find an instance of using === on types in this package, but there is one for ==: https://github.com/PainterQubits/Unitful.jl/blob/f375caf8f7c60f0dc2e284a97fc902365f55ab76/src/conversion.jl#L67

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see, that makes perfect sense.

@sostock sostock merged commit 038656a into JuliaPhysics:master Jun 8, 2025
16 checks passed
@sostock sostock deleted the uconvert-float-again branch June 8, 2025 19:04
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.

uconvert does not preserve the floating-point precision of quantities in some cases

2 participants