As HalfInteger <: Real, there should be no reason to convert anything in
this situation. It happens because the convert method resorts to Float64
as an intermediate value.
To still get conversion to floats, we can just dispatch on AbstractFloat
instead. However, it should be better to convert the numerator to T
first and then divide, so that we would not use a potentially lower
precision intermediate value.
This solves the problem where calling sum on an vector of HalfIntegers
yields a floating point value, even though there is no reason to convert
in the summation:
julia> sum([HalfInteger(1//2), HalfInteger(3//2)])
2.0
This is because there is an implicit convert(::Real) in the Base.add_sum
function. With this patch the sum call correctly yields a HalfInteger.
It also updates the tests related to HalfInteger convert methods:
- Make sure that the convert tests also check types
- Add a few tests for converting out of HalfInteger
* Call the HalfInteger field twofold
Makes it more immediately obvious what the meaning of the value stored
in the field is.
* Introduce new constructors for HalfInteger
The primary inner constructor mirrors the two-argument constructor of
the Rational type, where the user provides the numerator and denominator
values.
There is also a single argument outer constructor that makes HalfInteger
behave like a normal numeric type such that HalfInteger(n) == n.
* Move HalfInteger tests to a separate file
The using statements in halfinteger.jl are there so that it would be
possible to run the file separately from the other tests.
* Test the single-argument HalfInteger constructor
* Organize halfinteger.jl a bit
Prioritise the convert methods.
* Add multiplication with integer to HalfInteger
* Implement parsing and printing for HalfInteger
* parse(::HalfInteger, x) method
* Overload show to pretty-print HalfInteger
* Overload Base.numerator/denominator
And add tests for the other supplementary functions and methods as
well.
* Add HalfIntegerRange type
Can be constructed using the range operator :. Currently only supports
unit steps in the positive direction.
* Address feedback
* Rename .twofold -> .numerator
* Consistent variable names
* Remove unnecessary methods for HalfIntegerRange
* Allow constructing HalfIntegerRange with non-integer difference
* Add docs and ceil(::HalfInteger)