JuliaMath / FixedPointNumbers.jl

fixed point types for julia
Other
79 stars 34 forks source link

`promote_type(::Fixed, ::Rational)` should return `Fixed` #261

Open ParadaCarleton opened 1 year ago

ParadaCarleton commented 1 year ago

Julia suggests using Rational numbers rather than floating-point literals, as floats are more disruptive. While good advice in general, this means code following the style guide will tend to disrupt fixed point numbers, since promote_type(::Fixed, ::Rational) currently returns Rational. I think we should flip this convention, to have fixed point numbers behave like floats in this regard.

timholy commented 1 year ago

You can convert nearly every Fixed number into a Rational, but not vice-versa. If we made this change, we might get errors that currently aren't a problem. Are you arguing that's worth it?

ParadaCarleton commented 1 year ago

You can convert nearly every Fixed number into a Rational, but not vice-versa. If we made this change, we might get errors that currently aren't a problem. Are you arguing that's worth it?

I think it is, for consistency with Float. I assume you're referring to cases where the Rational is too big to be represented by a Fixed number?

timholy commented 1 year ago

Yes.

I think it is, for consistency with Float.

Can you clarify how you reason about the consistency? Is it that "Rational always loses"? (why?) Is it that FixedPoint numbers print as float?

To me, fixed-point numbers seem closer to Integer or Rational than AbstractFloat:

julia> x = N0f8(0.8)
0.8N0f8

julia> x + x
0.596N0f8

That's not behavior you expect to see from a floating-point number. In what way do you think that float is a better model?