"The set of mpq functions is quite small." Is this by design?

Marco Bodrato bodrato at mail.dm.unipi.it
Sun Aug 19 11:08:50 UTC 2018


Il Ven, 17 Agosto 2018 7:44 pm, Marc Glisse ha scritto:
> On Sat, 11 Aug 2018, Richard Damon wrote:
>> On 8/11/18 7:50 AM, Torbjörn Granlund wrote:
>>> Anders Andersson <pipatron at gmail.com> writes:

>>>   I'm mostly missing arithmetic with native integers (think mpq_mul_ui)
> gmpxx internally uses different code for some of those. For instance, q+=3
> turns into a call to mpz_addmul_ui. Others fake the conversion to mpq to
> avoid an allocation.

A note about naming conventions: there are currently two functions with
names matching mpq_*_ui: mpq_set_ui, and mpq_cmp_ui. They both take
TWO ui arguments, representing a fraction with both a small numerator
and a small denominator.

If we mean adding mpq_{add,sub,mul,div}_ui, for a _single_ ui argument,
well, why not? It's 80 lines of code... plus some more for testing...

>>> Perhaps mpq_mul_ui would make sense.  It would require a gcd of the ui
>>> argument with the denominator.

>> expect that most of the savings you might be able to get would be
>> handled better in the mpq library with special casing for unity
>> denominators (which is likely a not that uncommon case that might be
>> worth handling).

> These things always depend on the kind of operands you are dealing with. I
> often deal with small numbers where simply building (allocating) a
> rational from an int is already a very costly operation. For people who
> only deal with huge numbers that are often integers, your solution would
> work fine.

Should we also add mpq_{add,sub,mul,div}_z functions?
We already have mpq_cmp_z.

A special case for denominator equal to one can be added.

What if both denominators are 1? no GCDs are needed... to detect this I'd
use the test highlighted (but not suggested) by Bradley Lucier some months
ago: https://gmplib.org/list-archives/gmp-devel/2018-April/004941.html

Is it worth? and what about numerators?



More information about the gmp-discuss mailing list