/usr/share/doc/libntl-dev/NTL/RR.txt is in libntl-dev 6.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | /**************************************************************************\
MODULE: RR
SUMMARY:
The class RR is used to represent arbitrary-precision floating point
numbers.
The functions in this module guarantee very strong accuracy conditions
which make it easy to reason about the behavior of programs using
these functions.
The arithmetic operations always round their results to p bits, where
p is the current precision. The current precision can be changed
using RR::SetPrecision(), and can be read using RR::precision().
The minimum precision that can be set is 53 bits.
The maximum precision is limited only by the word size of the machine.
All arithmetic operations are implemented so that the effect is as if the
result was computed exactly, and then rounded to p bits. If a number
lies exactly half-way between two p-bit numbers, the "round to even"
rule is used. So in particular, the computed result will have a relative error
of at most 2^{-p}.
The above rounding rules apply to all arithmetic operations in this
module, except for the following routines:
* The transcendental functions:
log, exp, log10, expm1, log1p, pow, sin, cos, ComputePi
* The power function
* The input and ascii to RR conversion functions when using "e"-notation
For these functions, a very strong accuracy condition is still
guaranteed: the computed result has a relative error of less than 2^{-p + 1}
(and actually much closer to 2^{-p}).
That is, it is as if the resulted were computed exactly, and then
rounded to one of the two neighboring p-bit numbers (but not necessarily
the closest).
The behavior of all functions in this module is completely platform
independent: you should get *exactly* the same results on any platform
(the only exception to this rule is the random number generator).
Note that because precision is variable, a number may be computed with
to a high precision p', and then be used as input to an arithmetic operation
when the current precision is p < p'.
The above accuracy guarantees still apply; in particular,
no rounding is done until *after* the operation is performed.
EXAMPLE: If x and y are computed to 200 bits of precision,
and then the precision is set to 100 bits, then x-y will
be computed correctly to 100 bits, even if, say, x and y agree
in their high-order 50 bits. If x and y had been rounded to
100 bits before the subtraction, then the difference would
only be accurate to 50 bits of precision.
Note that the assignment operator and the copy constructor
produce *exact* copies of their inputs---they are *never* rounded.
This is a change in semantics from versions 2.0 and earlier
in which assignment and copy rounded their outputs.
This was deemed a design error and has been changed.
If you want to force rounding to current precision, the easiest
way to do this is with the RR to RR conversion routines:
conv(x, a);
or
x = to_RR(a);
This will round a to current precision and store the result in x.
Note that writing
x = a + 0;
or
x = a*1;
also has the same effect.
Unlike IEEE standard floating point, there are no "special values",
like "infinity" or "not a number", nor are there any "denormalized
numbers". Overflow, underflow, or taking a square root of a negative
number all result in an error being raised.
An RR is represented as a mantissa/exponent pair (x, e), where x is a
ZZ and e is a long. The real number represented by (x, e) is x * 2^e.
Zero is always represented as (0, 0). For all other numbers, x is
always odd.
CONVERSIONS AND PROMOTIONS:
The complete set of conversion routines between RR and other types is
documented in the file "conversions.txt". Conversion from any type
to RR always rounds the result to the current precision.
The basic operations also support the notion of "promotions",
so that they promote a double to an RR. For example, one can write
x = y + 1.5;
where x and y are RR's. One should be aware that these promotions are
always implemented using the double to RR conversion routine.
SIZE INVARIANT: max(NumBits(x), |e|) < 2^(NTL_BITS_PER_LONG-4)
\**************************************************************************/
#include <NTL/ZZ.h>
#include <NTL/xdouble.h>
#include <NTL/quad_float.h>
class RR {
public:
RR(); // = 0
RR(const RR& a); // copy constructor
explicit RR(double a); // promotion constructor
RR& operator=(const RR& a); // assignment operator
// NOTE: the copy constructor and assignment operator
// produce exact copies of their inputs, and do not round
// to current precision.
RR& operator=(double a); // convert and assign
~RR(); // destructor
const ZZ& mantissa() const; // read the mantissa
long exponent() const; // read the exponent
static void SetPrecision(long p);
// set current precision to max(p, 53) bits.
// The default is 150
static long precision(); // read current value of precision
static void SetOutputPrecision(long p);
// set the number of output decimal digits to max(p, 1).
// The default is 10
static long OutputPrecision();
// read the current number of output decimal digits
};
/**************************************************************************\
Comparison
\**************************************************************************/
// standard comparison operators:
long operator==(const RR& a, const RR& b);
long operator!=(const RR& a, const RR& b);
long operator<=(const RR& a, const RR& b);
long operator>=(const RR& a, const RR& b);
long operator <(const RR& a, const RR& b);
long operator >(const RR& a, const RR& b);
long IsZero(const RR& a); // test if 0
long IsOne(const RR& a); // test if 1
long sign(const RR& a); // returns sign of a (+1, -1, 0)
long compare(const RR& a, const RR& b); // returns sign(a-b);
// PROMOTIONS: operators ==, ..., > and function compare
// promote double to RR on (a, b).
/**************************************************************************\
Addition
\**************************************************************************/
// operator notation:
RR operator+(const RR& a, const RR& b);
RR operator-(const RR& a, const RR& b);
RR operator-(const RR& a); // unary -
RR& operator+=(RR& x, const RR& a);
RR& operator+=(RR& x, double a);
RR& operator-=(RR& x, const RR& a);
RR& operator-=(RR& x, double a);
RR& operator++(RR& x); // prefix
void operator++(RR& x, int); // postfix
RR& operator--(RR& x); // prefix
void operator--(RR& x, int); // postfix
// procedural versions:
void add(RR& z, const RR& a, const RR& b); // z = a+b
void sub(RR& z, const RR& a, const RR& b); // z = a-b
void negate(RR& z, const RR& a); // z = -a
// PROMOTIONS: operators +, -, and procedures add, sub promote double
// to RR on (a, b).
void abs(RR& z, const RR& a); // z = |a|
RR fabs(const RR& a);
RR abs(const RR& a);
/**************************************************************************\
Multiplication
\**************************************************************************/
// operator notation:
RR operator*(const RR& a, const RR& b);
RR& operator*=(RR& x, const RR& a);
RR& operator*=(RR& x, double a);
// procedural versions:
void mul(RR& z, const RR& a, const RR& b); // z = a*b
void sqr(RR& z, const RR& a); // z = a * a
RR sqr(const RR& a);
// PROMOTIONS: operator * and procedure mul promote double to RR on (a, b).
/**************************************************************************\
Division
\**************************************************************************/
// operator notation:
RR operator/(const RR& a, const RR& b);
RR& operator/=(RR& x, const RR& a);
RR& operator/=(RR& x, double a);
// procedural versions:
void div(RR& z, const RR& a, const RR& b); z = a/b
void inv(RR& z, const RR& a); // z = 1 / a
RR inv(const RR& a);
// PROMOTIONS: operator / and procedure div promote double to RR on (a, b).
/**************************************************************************\
Transcendental functions
\**************************************************************************/
void exp(RR& res, const RR& x); // e^x
RR exp(const RR& x);
void log(RR& res, const RR& x); // log(x) (natural log)
RR log(const RR& x);
void log10(RR& res, const RR& x); // log(x)/log(10)
RR log10(const RR& x);
void expm1(RR& res, const RR& x);
RR expm1(const RR& x);
// e^(x)-1; more accurate than exp(x)-1 when |x| is small
void log1p(RR& res, const RR& x);
RR log1p(const RR& x);
// log(1 + x); more accurate than log(1 + x) when |x| is small
void pow(RR& res, const RR& x, const RR& y); // x^y
RR pow(const RR& x, const RR& y);
void sin(RR& res, const RR& x); // sin(x); restriction: |x| < 2^1000
RR sin(const RR& x);
void cos(RR& res, const RR& x); // cos(x); restriction: |x| < 2^1000
RR cos(const RR& x);
void ComputePi(RR& pi); // approximate pi to current precision
RR ComputePi_RR();
/**************************************************************************\
Rounding to integer values
\**************************************************************************/
/*** RR output ***/
void trunc(RR& z, const RR& a); // z = a, truncated to 0
RR trunc(const RR& a);
void floor(RR& z, const RR& a); // z = a, truncated to -infinity
RR floor(const RR& a);
void ceil(RR& z, const RR& a); // z = a, truncated to +infinity
RR ceil(const RR& a);
void round(RR& z, const RR& a); // z = a, truncated to nearest integer
RR round(const RR& a); // ties are rounded to an even integer
/*** ZZ output ***/
void TruncToZZ(ZZ& z, const RR& a); // z = a, truncated to 0
ZZ TruncToZZ(const RR& a);
void FloorToZZ(ZZ& z, const RR& a); // z = a, truncated to -infinity
ZZ FloorToZZ(const RR& a); // same as RR to ZZ conversion
void CeilToZZ(ZZ& z, const RR& a); // z = a, truncated to +infinity
ZZ CeilToZZ(const ZZ& a);
void RoundToZZ(ZZ& z, const RR& a); // z = a, truncated to nearest integer
ZZ RoundToZZ(const RR& a); // ties are rounded to an even integer
/**************************************************************************\
Miscelaneous
\**************************************************************************/
void MakeRR(RR& z, const ZZ& a, long e);
RR MakeRR(const ZZ& a, long e);
// z = a*2^e, rounded to current precision
void random(RR& z);
RR random_RR();
// z = pseudo-random number in the range [0,1).
// Note that the behaviour of this function is somewhat platform
// dependent, because the underlying pseudo-ramdom generator is.
void SqrRoot(RR& z, const RR& a); // z = sqrt(a);
RR SqrRoot(const RR& a);
RR sqrt(const RR& a);
void power(RR& z, const RR& a, long e); // z = a^e, e may be negative
RR power(const RR& a, long e);
void power2(RR& z, long e); // z = 2^e, e may be negative
RR power2_RR(long e);
void clear(RR& z); // z = 0
void set(RR& z); // z = 1
void swap(RR& a, RR& b); // swaps a and b (by swapping pointers)
/**************************************************************************\
Input/Output
Input Syntax:
<number>: [ "-" ] <unsigned-number>
<unsigned-number>: <dotted-number> [ <e-part> ] | <e-part>
<dotted-number>: <digits> | <digits> "." <digits> | "." <digits> | <digits> "."
<digits>: <digit> <digits> | <digit>
<digit>: "0" | ... | "9"
<e-part>: ( "E" | "e" ) [ "+" | "-" ] <digits>
Examples of valid input:
17 1.5 0.5 .5 5. -.5 e10 e-10 e+10 1.5e10 .5e10 .5E10
Note that the number of decimal digits of precision that are used
for output can be set to any number p >= 1 by calling
the routine RR::SetOutputPrecision(p). The default value of p is 10.
The current value of p is returned by a call to RR::OutputPrecision().
\**************************************************************************/
ostream& operator<<(ostream& s, const RR& a);
istream& operator>>(istream& s, RR& x);
/**************************************************************************\
Specialized routines with explicit precision parameter
These routines take an explicit precision parameter p. The value of p may be
any positive integer. All results are computed to *precisely* p bits of
precision, regardless of the current precision (as set by RR::SetPrecision).
These routines are provided both for convenience and for situations where the
computation must be done with a precision that may be less than 53.
\**************************************************************************/
void AddPrec(RR& z, const RR& a, const RR& b, long p); // z = a + b
RR AddPrec(const RR& a, const RR& b, long p);
void SubPrec(RR& z, const RR& a, const RR& b, long p); // z = a - b
RR SubPrec(const RR& a, const RR& b, long p);
void NegatePrec(RR& z, const RR& a, long p); // z = -a
RR NegatePrec(const RR& a, long p);
void AbsPrec(RR& z, const RR& a, long p); // z = |a|
RR AbsPrec(const RR& a, long p);
void MulPrec(RR& z, const RR& a, const RR& b, long p); // z = a*b
RR MulPrec(const RR& a, const RR& b, long p);
void SqrPrec(RR& z, const RR& a, long p); // z = a*a
RR SqrPrec(const RR& a, long p);
void DivPrec(RR& z, const RR& a, const RR& b, long p); // z = a/b
RR DivPrec(const RR& a, const RR& b, long p);
void InvPrec(RR& z, const RR& a, long p); // z = 1/a
RR DivPrec(const RR& a, long p);
void SqrRootPrec(RR& z, const RR& a, long p); // z = sqrt(a)
RR SqrRootPrec(const RR& a, long p);
void TruncPrec(RR& z, const RR& a, long p); // z = a, truncated to 0
RR TruncPrec(const RR& a, long p);
void FloorPrec(RR& z, const RR& a, long p); // z = a, truncated to -infinity
RR FloorPrec(const RR& a, long p);
void CeilPrec(RR& z, const RR& a, long p); // z = a, truncated to +infinity
RR CeilPrec(const RR& a, long p);
void RoundPrec(RR& z, const RR& a, long p); // z = a,
// truncated to nearest integer,
// ties are roundec to an even
// integer
RR RoundPrec(const RR& a, long p);
void ConvPrec(RR& z, const RR& a, long p); // z = a
RR ConvPrec(const RR& a, long p);
void ConvPrec(RR& z, const ZZ& a, long p); // z = a
RR ConvPrec(const ZZ& a, long p);
void ConvPrec(RR& z, long a, long p); // z = a
RR ConvPrec(long a, long p);
void ConvPrec(RR& z, int a, long p); // z = a
RR ConvPrec(int a, long p);
void ConvPrec(RR& z, unsigned long a, long p); // z = a
RR ConvPrec(unsigned long a, long p);
void ConvPrec(RR& z, unsigned int a, long p); // z = a
RR ConvPrec(unsigned int a, long p);
void ConvPrec(RR& z, double a, long p); // z = a
RR ConvPrec(double a, long p);
void ConvPrec(RR& z, const xdouble& a, long p); // z = a
RR ConvPrec(const xdouble& a, long p);
void ConvPrec(RR& z, const quad_float& a, long p); // z = a
RR ConvPrec(const quad_float& a, long p);
void ConvPrec(RR& z, const char *s, long p); // read z from s
RR ConvPrec(const char *s, long p);
void InputPrec(RR& z, istream& s, long p); // read z from s
RR InputPrec(istream& s, long p);
void MakeRRPrec(RR& z, const ZZ& a, long e, long p); // z = a*2^e
RR MakeRRPrec(const ZZ& a, long e, long p);
/**************************************************************************\
COMPATABILITY NOTES:
(1) Prior to version 5.3, the documentation indicated that under certain
circumstances, the value of the current precision could be directly set
by setting the variable RR::prec. Such usage is now considered
obsolete. To perform computations using a precision of less than 53
bits, users should use the specialized routines AddPrec, SubPrec, etc.,
documented above.
(2) The routine RoundToPrecision is obsolete, although for backward
compatability, it is still declared (in both procedural and function
forms), and is equivalent to ConvPrec.
(3) In versions 2.0 and earlier, the assignment operator and copy
constructor for the class RR rounded their outputs to the current
precision. This is no longer the case: their outputs are now exact
copies of their inputs, regardless of the current precision.
\**************************************************************************/
|