Hipparchus-Math / hipparchus

An efficient, general-purpose mathematics components library in the Java programming language
Apache License 2.0
142 stars 41 forks source link

BigFraction - double constructor #176

Closed axkr closed 2 years ago

axkr commented 2 years ago

Maybe I don't understand this constructor correctly:

but what I'm looking for is a conversion where the result n - numerator and d - denominator of the v - double value satisfy the condition:

|(n/d) - v| < (10^c) / (d^2)

with c = -4 for example.

maisonobe commented 2 years ago

What about adding a stream generator that would produce a Stream<BigFraction>? This way, you could decide by yourself when to select the fraction you want using the Stream API, for example with filter and findFirst.

axkr commented 2 years ago

What about adding a stream generator that would produce a Stream<BigFraction>? This way, you could decide by yourself when to select the fraction you want using the Stream API, for example with filter and findFirst.

That seems to be a good idea. Do you have an example how to do it?

maisonobe commented 2 years ago

I have added static methods convergents in both Fraction and BigFraction to generate streams of fractions. You can look at the unit tests to see how one specific convergent can be selected from the stream.

Beware that your criterion with a threshold of 10^c/d^2 will probably never be fulfilled. One theorem for errors bounds in continued fractions states that |α - pₖ/qₖ| < 1/(qₖqₖ₊₁). With this boundary, the numerator is 1 and not 10^c, and the denominator qₖqₖ₊₁ is bigger than qₖ^2 as the denominator series is an increasing series.

HannesWell commented 2 years ago

I have added static methods convergents in both Fraction and BigFraction to generate streams of fractions. You can look at the unit tests to see how one specific convergent can be selected from the stream.

I also commented this on the commit: Shouldn't the spliterator created from the generatingIterator() also report the ORDERED characteristics, since the elements should not be arbitrarily reordered? Spliterator.DISTINCT | Spliterator.ORDERED