[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [commons-numbers] [...] NUMBERS-91: Added ofInt() factory methods [...]

On Fri, 28 Dec 2018 09:17:08 -0800, Eric Barnhill wrote:
Fractions are constructed using either ints or doubles. In the case of
ints, the numerator and denominator are passed (or the denominator is
assumed to be one). Constructing fractions from doubles is more algorithmic work: if I pass a known fixed quantity such as 0.6 of course it will not be hard for the constructor to determine that is the equivalent of 3 / 5 . However if doubles are being passed of unknown precision, then I may want to request a max value on the denominator, or a precision within which the simplest fraction should be returned, or even the maximum iterations in the

I think of those as qualitatively very different activities

I agree.

so I called
them ofInt and ofDouble.

But we could consider:
 * of(long, long)
 * of(int, long)
 * of(BigInteger, BigInteger)
 * ...
 * ofDouble(double)
 * ofDouble(int, double)
 * ...
where "Cat.1" and "Cat.2" delineates the very different handling
which you referred to; and in the case of "Cat.1", an exact (?)
representation is constructed, while "Cat.2" could be lossy.
The former can also be construed as closer to the convention for
"ValJO" ("BigFaction" not being "ValJO" does not preclude choosing
the simplest name for its factory methods).

The example I had in mind was probably Complex,
where we have ofPolar and ofCartesian. I suppose you are right, in this case the hard typing of the passed variables alone could invoke either an int or double based method while with Complex, both constructors are taking

Quite right, there is some inconsistency; we may consider using
"of" if the "ValJO" aspect is more important that the equivalence
between polar and Cartesian input (cf. also the suggestion that
conversion methods should be name "from...", to which I'm not really
a fan yet).
If there is no strong argument yet for either, we could open a JIRA
report asking for opinions.  And leave that open as long as we
release "beta" versions.

You do then have some very similar methods, for example of(int a, int b) will be an integer fraction with a on top and b on bottom; while calling of(double a, int b) will produce a fraction that approximates double a with
max denominator b.

Those two processes are so different that it might be more clarifying to
distinguish them as ofInt(int a, int b) and ofDouble(double a, int b)

IMHO, it is not sufficiently self-documenting anyway: one has to go
to the docs in order to understand the difference; hence my proposal
to have "of" for the "obvious thing" (a/b) and "ofDouble" for the more
elaborate "transform".
Not sure if I'm clear in why the "non-symmetric" makes sense. :-}

Best regards,


On Fri, Dec 28, 2018 at 4:33 AM Gilles <gilles@xxxxxxxxxxxxxxxxxxxxx> wrote:

Hello Eric.

On Thu, 27 Dec 2018 17:00:15 -0800, Eric Barnhill wrote:
> I am overloading:
> public static BigFraction ofInt(final BigInteger num) {
>         return new BigFraction(num, BigInteger.ONE);
>     }
> public static BigFraction ofInt(BigInteger num, BigInteger den) {
>     return new BigFraction(num, den);
>     }
>     private BigFraction(BigInteger num, BigInteger den) {
> Did my comment not give that impression?

I was in fact wondering why "ofInt" rather than just "of".


>> [...]

To unsubscribe, e-mail: dev-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: dev-help@xxxxxxxxxxxxxxxxxx