diff --git a/Number_types/doc_tex/NumberTypeSupport/Numbertype.tex b/Number_types/doc_tex/NumberTypeSupport/Numbertype.tex index b5f765f4725..876900943af 100644 --- a/Number_types/doc_tex/NumberTypeSupport/Numbertype.tex +++ b/Number_types/doc_tex/NumberTypeSupport/Numbertype.tex @@ -102,17 +102,24 @@ With the built-in integer types overflow might occur. \cgal\ provides several number types that are that can be used for exact computation. These include the \ccc{Quotient} class that can be used to create, for example, a number type that behaves like a rational -number. When used in conjunction with the number type \ccc{MP_Float} that -is able to represent multi-precision floating point values, you achieve -an exact rational number representation. +number when parameterized with a number type which can represent integers. + +The number type \ccc{MP_Float} is able to represent multi-precision floating +point values, a generalization of integers scaled by a (potentially negative) +power of 2. It allows to deal with ring operations over floating-point values +with requiring rational numbers. By plugging it in \ccc{Quotient}, one obtains +rational numbers. Note that \ccc{MP_Float} may not be as efficient as the +integer types provided by \ccc{GMP} or \ccc{LEDA}, but it has the advantage +to make more parts of \cgal\ independent on these external libraries for +handling robustness issues. The templated number type \ccc{Lazy_exact_nt} is able to represent any number that \ccc{NT} is able to represent, but because it first tries to use an -approximate value to perform computations it can be faster than the provided -number type \ccc{NT}. \cgal\ also provides a fixed-precision number type, +approximate value to perform computations it can be faster than \ccc{NT}. A number type for doing interval arithmetic, \ccc{Interval_nt}, is provided. -This number type helps in doing filtering of predicates. +This number type helps in doing arithmetic filtering in many places such +as \ccc{Filtered_predicate}. \ccc{CGAL::Root_of_2} is a number type that allows to represent algebraic numbers of degree up to~2 over a \ccc{RingNumberType}. A generic function diff --git a/Number_types/doc_tex/NumberTypeSupport_ref/EuclideanRingNumberType.tex b/Number_types/doc_tex/NumberTypeSupport_ref/EuclideanRingNumberType.tex index 96122e8139a..b52910bce05 100644 --- a/Number_types/doc_tex/NumberTypeSupport_ref/EuclideanRingNumberType.tex +++ b/Number_types/doc_tex/NumberTypeSupport_ref/EuclideanRingNumberType.tex @@ -39,6 +39,7 @@ RingNumberType \ccc{int} \\ \ccc{long} \\ \ccc{long long} \\ +\ccc{CGAL::MP_Float} \\ \ccc{CGAL::Gmpz} \\ \ccc{leda_integer} diff --git a/Number_types/doc_tex/NumberTypeSupport_ref/FieldNumberType.tex b/Number_types/doc_tex/NumberTypeSupport_ref/FieldNumberType.tex index 31249d104f2..c8a019408c2 100644 --- a/Number_types/doc_tex/NumberTypeSupport_ref/FieldNumberType.tex +++ b/Number_types/doc_tex/NumberTypeSupport_ref/FieldNumberType.tex @@ -33,7 +33,6 @@ RingNumberType \ccc{CGAL::Interval_nt} \\ \ccc{CGAL::Interval_nt_advanced} \\ \ccc{CGAL::Lazy_exact_nt} \\ -\ccc{CGAL::MP_Float} \\ \ccc{CGAL::Quotient} \\ \ccc{leda_rational} \\ \ccc{leda_bigfloat} \\ diff --git a/Number_types/doc_tex/NumberTypeSupport_ref/MP_Float.tex b/Number_types/doc_tex/NumberTypeSupport_ref/MP_Float.tex index 58c74d6f608..dd63edb1f07 100644 --- a/Number_types/doc_tex/NumberTypeSupport_ref/MP_Float.tex +++ b/Number_types/doc_tex/NumberTypeSupport_ref/MP_Float.tex @@ -8,18 +8,17 @@ \ccDefinition An object of the class \ccc{MP_Float} is able to represent a floating point -value with arbitrary precision. This number type has the property that +value with arbitrary precision, that is numbers of the form $m2^e$ where +$m$ and $e$ are integers. This number type has the property that additions, subtractions and multiplications are computed exactly, as well as the construction from \ccc{float}, \ccc{double} and \ccc{long double}. +It also provides integral divisions related operations. -Division and square root are not enabled by default since \cgal\ release 3.2, -since they are computed approximately. We suggest that you use -rationals like \ccc{Quotient} when you need exact (rational) -divisions. -To enable division and square root, you have to define the preprocessor -macro \ccc{CGAL_MP_FLOAT_ALLOW_INEXACT}. - -An exact (integral) division is also provided. +Square root and (field) division are not enabled by default since +\cgal\ release 3.2, since they are computed approximately. We suggest that you +use rationals like \ccc{Quotient} when you need exact (rational) +divisions. To enable division and square root, you have to define the +preprocessor macro \ccc{CGAL_MP_FLOAT_ALLOW_INEXACT}. Note on the implementation : although the mantissa length is basically only limited by the available memory, the exponent is currently represented by a @@ -29,8 +28,7 @@ plan to also have a multiprecision exponent to fix this issue. \ccInclude{CGAL/MP_Float.h} \ccIsModel -\ccc{RingNumberType} -or \ccc{SqrtFieldNumberType} if \ccc{CGAL_MP_FLOAT_ALLOW_INEXACT} is set. +\ccc{EuclideanRingNumberType} \ccCreation \ccCreationVariable{m} @@ -62,16 +60,33 @@ or \ccc{SqrtFieldNumberType} if \ccc{CGAL_MP_FLOAT_ALLOW_INEXACT} is set. {reads a \ccc{double} from \ccc{in}, then converts it to an \ccc{MP_Float}.} \ccFunction{bool divides(const MP_Float &a, const MP_Float &b);} -{returns true iff \ccc{b} divides \ccc{b}.} +{returns true iff \ccc{b} divides \ccc{a}.} \ccFunction{MP_Float exact_division(const MP_Float &a, const MP_Float &b);} {computes the result of the division of \ccc{a} by \ccc{b}. \ccPrecond{divides(a, b)}.} +\ccFunction{MP_Float div(const MP_Float &a, const MP_Float &b);} +{returns the result of the integral division of \ccc{a} by \ccc{b}. +For \ccc{MP_Float}, this is not uniquely defined, but this function +guarantees that the remainder has a smaller bitlength than \ccc{b} +(considering the bitlength of the odd mantissa).} + +\ccFunction{MP_Float operator%(const MP_Float &a, const MP_Float &b);} +{returns the remainder of the integral division of \ccc{a} by \ccc{b}. +For \ccc{MP_Float}, this is not uniquely defined, but this function +guarantees that the remainder has a smaller bitlength than \ccc{b} +(considering the bitlength of the odd mantissa).} + +\ccFunction{MP_Float gcd(const MP_Float &a, const MP_Float &b);} +{returns the greatest common divisor of \ccc{a} and \ccc{b}. +For \ccc{MP_Float}, this is defined up to a power of 2.} +\ccPrecond{\ccc{a} and \ccc{b} are non zero.}} + \ccFunction{MP_Float approximate_division(const MP_Float &a, const MP_Float &b);} -{computes an approximation of the division by converting the operands to -\ccc{double}, performing the division on \ccc{double}, and converting back to -\ccc{MP_Float}.} +{computes an approximation of the (field) division by converting the operands +to \ccc{double}, performing the division on \ccc{double}, and converting back +to \ccc{MP_Float}.} \ccFunction{MP_Float approximate_sqrt(const MP_Float &a);} {computes an approximation of the square root by converting the operand to diff --git a/Number_types/include/CGAL/MP_Float.h b/Number_types/include/CGAL/MP_Float.h index 164086be742..3cf965da7d7 100644 --- a/Number_types/include/CGAL/MP_Float.h +++ b/Number_types/include/CGAL/MP_Float.h @@ -37,6 +37,7 @@ // The main algorithms are : // - Addition/Subtraction // - Multiplication +// - Integral division div(), exact_division(), gcd(), operator%(). // - Comparison // - to_double() / to_interval() // - Construction from a double. @@ -49,8 +50,6 @@ // - Division, sqrt... : different options : // - nothing // - convert to double, take approximation, compute over double, reconstruct -// - exact division as separate function (for Bareiss...) -// - returns the quotient of the division, forgetting the rest. CGAL_BEGIN_NAMESPACE @@ -62,6 +61,7 @@ MP_Float operator*(const MP_Float &a, const MP_Float &b); #ifdef CGAL_MP_FLOAT_ALLOW_INEXACT MP_Float operator/(const MP_Float &a, const MP_Float &b); #endif +MP_Float operator%(const MP_Float &a, const MP_Float &b); Comparison_result compare (const MP_Float & a, const MP_Float & b); @@ -191,6 +191,7 @@ public: #ifdef CGAL_MP_FLOAT_ALLOW_INEXACT MP_Float& operator/=(const MP_Float &a) { return *this = *this / a; } #endif + MP_Float& operator%=(const MP_Float &a) { return *this = *this % a; } exponent_type max_exp() const { @@ -597,6 +598,37 @@ divides(const MP_Float & n, const MP_Float & d) return CGALi::division(n, d).second == 0; } +inline +MP_Float +operator%(const MP_Float& n1, const MP_Float& n2) +{ + return CGALi::division(n1, n2).second; +} + +inline +MP_Float +div(const MP_Float& n1, const MP_Float& n2) +{ + return CGALi::division(n1, n2).first; +} + +inline +MP_Float +gcd( const MP_Float& a, const MP_Float& b) +{ + CGAL_precondition( a != 0 ); + CGAL_precondition( b != 0 ); + MP_Float x = a, y = b; + while (true) { + x = x % y; + if (x == 0) { + CGAL_postcondition(divides(a, y) && divides(b, y)); + return y; + } + swap(x, y); + } +} + CGAL_END_NAMESPACE #endif // CGAL_MP_FLOAT_H