| 1 |
/* |
| 2 |
* This file is a part of TTMath Bignum Library |
| 3 |
* and is distributed under the (new) BSD licence. |
| 4 |
* Author: Tomasz Sowa <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* Copyright (c) 2006-2012, Tomasz Sowa |
| 9 |
* All rights reserved. |
| 10 |
* |
| 11 |
* Redistribution and use in source and binary forms, with or without |
| 12 |
* modification, are permitted provided that the following conditions are met: |
| 13 |
* |
| 14 |
* * Redistributions of source code must retain the above copyright notice, |
| 15 |
* this list of conditions and the following disclaimer. |
| 16 |
* |
| 17 |
* * Redistributions in binary form must reproduce the above copyright |
| 18 |
* notice, this list of conditions and the following disclaimer in the |
| 19 |
* documentation and/or other materials provided with the distribution. |
| 20 |
* |
| 21 |
* * Neither the name Tomasz Sowa nor the names of contributors to this |
| 22 |
* project may be used to endorse or promote products derived |
| 23 |
* from this software without specific prior written permission. |
| 24 |
* |
| 25 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 29 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 35 |
* THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
*/ |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#ifndef headerfilettmathmathtt |
| 41 |
#define headerfilettmathmathtt |
| 42 |
|
| 43 |
/*! |
| 44 |
\file ttmath.h |
| 45 |
\brief Mathematics functions. |
| 46 |
*/ |
| 47 |
|
| 48 |
#ifdef _MSC_VER |
| 49 |
//warning C4127: conditional expression is constant |
| 50 |
#pragma warning( disable: 4127 ) |
| 51 |
//warning C4702: unreachable code |
| 52 |
#pragma warning( disable: 4702 ) |
| 53 |
//warning C4800: forcing value to bool 'true' or 'false' (performance warning) |
| 54 |
#pragma warning( disable: 4800 ) |
| 55 |
#endif |
| 56 |
|
| 57 |
#define TTMATH_DONT_USE_WCHAR |
| 58 |
|
| 59 |
#include "ttmathint.h" |
| 60 |
#include "ttmathobjects.h" |
| 61 |
|
| 62 |
|
| 63 |
namespace ttmath |
| 64 |
{ |
| 65 |
/* |
| 66 |
* |
| 67 |
* functions defined here are used only with Big<> types |
| 68 |
* |
| 69 |
* |
| 70 |
*/ |
| 71 |
|
| 72 |
|
| 73 |
/* |
| 74 |
* |
| 75 |
* functions for rounding |
| 76 |
* |
| 77 |
* |
| 78 |
*/ |
| 79 |
|
| 80 |
|
| 81 |
/*! |
| 82 |
this function skips the fraction from x |
| 83 |
e.g 2.2 = 2 |
| 84 |
2.7 = 2 |
| 85 |
-2.2 = 2 |
| 86 |
-2.7 = 2 |
| 87 |
*/ |
| 88 |
template<class ValueType> |
| 89 |
ValueType SkipFraction(const ValueType & x) |
| 90 |
{ |
| 91 |
ValueType result( x ); |
| 92 |
result.SkipFraction(); |
| 93 |
|
| 94 |
return result; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/*! |
| 99 |
this function rounds to the nearest integer value |
| 100 |
e.g 2.2 = 2 |
| 101 |
2.7 = 3 |
| 102 |
-2.2 = -2 |
| 103 |
-2.7 = -3 |
| 104 |
*/ |
| 105 |
template<class ValueType> |
| 106 |
ValueType Round(const ValueType & x, ErrorCode * err = 0) |
| 107 |
{ |
| 108 |
if( x.IsNan() ) |
| 109 |
{ |
| 110 |
if( err ) |
| 111 |
*err = err_improper_argument; |
| 112 |
|
| 113 |
return x; // NaN |
| 114 |
} |
| 115 |
|
| 116 |
ValueType result( x ); |
| 117 |
uint c = result.Round(); |
| 118 |
|
| 119 |
if( err ) |
| 120 |
*err = c ? err_overflow : err_ok; |
| 121 |
|
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
/*! |
| 128 |
this function returns a value representing the smallest integer |
| 129 |
that is greater than or equal to x |
| 130 |
|
| 131 |
Ceil(-3.7) = -3 |
| 132 |
Ceil(-3.1) = -3 |
| 133 |
Ceil(-3.0) = -3 |
| 134 |
Ceil(4.0) = 4 |
| 135 |
Ceil(4.2) = 5 |
| 136 |
Ceil(4.8) = 5 |
| 137 |
*/ |
| 138 |
template<class ValueType> |
| 139 |
ValueType Ceil(const ValueType & x, ErrorCode * err = 0) |
| 140 |
{ |
| 141 |
if( x.IsNan() ) |
| 142 |
{ |
| 143 |
if( err ) |
| 144 |
*err = err_improper_argument; |
| 145 |
|
| 146 |
return x; // NaN |
| 147 |
} |
| 148 |
|
| 149 |
ValueType result(x); |
| 150 |
uint c = 0; |
| 151 |
|
| 152 |
result.SkipFraction(); |
| 153 |
|
| 154 |
if( result != x ) |
| 155 |
{ |
| 156 |
// x is with fraction |
| 157 |
// if x is negative we don't have to do anything |
| 158 |
if( !x.IsSign() ) |
| 159 |
{ |
| 160 |
ValueType one; |
| 161 |
one.SetOne(); |
| 162 |
|
| 163 |
c += result.Add(one); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if( err ) |
| 168 |
*err = c ? err_overflow : err_ok; |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/*! |
| 175 |
this function returns a value representing the largest integer |
| 176 |
that is less than or equal to x |
| 177 |
|
| 178 |
Floor(-3.6) = -4 |
| 179 |
Floor(-3.1) = -4 |
| 180 |
Floor(-3) = -3 |
| 181 |
Floor(2) = 2 |
| 182 |
Floor(2.3) = 2 |
| 183 |
Floor(2.8) = 2 |
| 184 |
*/ |
| 185 |
template<class ValueType> |
| 186 |
ValueType Floor(const ValueType & x, ErrorCode * err = 0) |
| 187 |
{ |
| 188 |
if( x.IsNan() ) |
| 189 |
{ |
| 190 |
if( err ) |
| 191 |
*err = err_improper_argument; |
| 192 |
|
| 193 |
return x; // NaN |
| 194 |
} |
| 195 |
|
| 196 |
ValueType result(x); |
| 197 |
uint c = 0; |
| 198 |
|
| 199 |
result.SkipFraction(); |
| 200 |
|
| 201 |
if( result != x ) |
| 202 |
{ |
| 203 |
// x is with fraction |
| 204 |
// if x is positive we don't have to do anything |
| 205 |
if( x.IsSign() ) |
| 206 |
{ |
| 207 |
ValueType one; |
| 208 |
one.SetOne(); |
| 209 |
|
| 210 |
c += result.Sub(one); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if( err ) |
| 215 |
*err = c ? err_overflow : err_ok; |
| 216 |
|
| 217 |
return result; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
/* |
| 223 |
* |
| 224 |
* logarithms and the exponent |
| 225 |
* |
| 226 |
* |
| 227 |
*/ |
| 228 |
|
| 229 |
|
| 230 |
/*! |
| 231 |
this function calculates the natural logarithm (logarithm with the base 'e') |
| 232 |
*/ |
| 233 |
template<class ValueType> |
| 234 |
ValueType Ln(const ValueType & x, ErrorCode * err = 0) |
| 235 |
{ |
| 236 |
if( x.IsNan() ) |
| 237 |
{ |
| 238 |
if( err ) |
| 239 |
*err = err_improper_argument; |
| 240 |
|
| 241 |
return x; // NaN |
| 242 |
} |
| 243 |
|
| 244 |
ValueType result; |
| 245 |
uint state = result.Ln(x); |
| 246 |
|
| 247 |
if( err ) |
| 248 |
{ |
| 249 |
switch( state ) |
| 250 |
{ |
| 251 |
case 0: |
| 252 |
*err = err_ok; |
| 253 |
break; |
| 254 |
case 1: |
| 255 |
*err = err_overflow; |
| 256 |
break; |
| 257 |
case 2: |
| 258 |
*err = err_improper_argument; |
| 259 |
break; |
| 260 |
default: |
| 261 |
*err = err_internal_error; |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
return result; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/*! |
| 272 |
this function calculates the logarithm |
| 273 |
*/ |
| 274 |
template<class ValueType> |
| 275 |
ValueType Log(const ValueType & x, const ValueType & base, ErrorCode * err = 0) |
| 276 |
{ |
| 277 |
if( x.IsNan() ) |
| 278 |
{ |
| 279 |
if( err ) *err = err_improper_argument; |
| 280 |
return x; |
| 281 |
} |
| 282 |
|
| 283 |
if( base.IsNan() ) |
| 284 |
{ |
| 285 |
if( err ) *err = err_improper_argument; |
| 286 |
return base; |
| 287 |
} |
| 288 |
|
| 289 |
ValueType result; |
| 290 |
uint state = result.Log(x, base); |
| 291 |
|
| 292 |
if( err ) |
| 293 |
{ |
| 294 |
switch( state ) |
| 295 |
{ |
| 296 |
case 0: |
| 297 |
*err = err_ok; |
| 298 |
break; |
| 299 |
case 1: |
| 300 |
*err = err_overflow; |
| 301 |
break; |
| 302 |
case 2: |
| 303 |
case 3: |
| 304 |
*err = err_improper_argument; |
| 305 |
break; |
| 306 |
default: |
| 307 |
*err = err_internal_error; |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return result; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/*! |
| 317 |
this function calculates the expression e^x |
| 318 |
*/ |
| 319 |
template<class ValueType> |
| 320 |
ValueType Exp(const ValueType & x, ErrorCode * err = 0) |
| 321 |
{ |
| 322 |
if( x.IsNan() ) |
| 323 |
{ |
| 324 |
if( err ) |
| 325 |
*err = err_improper_argument; |
| 326 |
|
| 327 |
return x; // NaN |
| 328 |
} |
| 329 |
|
| 330 |
ValueType result; |
| 331 |
uint c = result.Exp(x); |
| 332 |
|
| 333 |
if( err ) |
| 334 |
*err = c ? err_overflow : err_ok; |
| 335 |
|
| 336 |
return result; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/*! |
| 341 |
* |
| 342 |
* trigonometric functions |
| 343 |
* |
| 344 |
*/ |
| 345 |
|
| 346 |
|
| 347 |
/* |
| 348 |
this namespace consists of auxiliary functions |
| 349 |
(something like 'private' in a class) |
| 350 |
*/ |
| 351 |
namespace auxiliaryfunctions |
| 352 |
{ |
| 353 |
|
| 354 |
/*! |
| 355 |
an auxiliary function for calculating the Sine |
| 356 |
(you don't have to call this function) |
| 357 |
*/ |
| 358 |
template<class ValueType> |
| 359 |
uint PrepareSin(ValueType & x, bool & change_sign) |
| 360 |
{ |
| 361 |
ValueType temp; |
| 362 |
|
| 363 |
change_sign = false; |
| 364 |
|
| 365 |
if( x.IsSign() ) |
| 366 |
{ |
| 367 |
// we're using the formula 'sin(-x) = -sin(x)' |
| 368 |
change_sign = !change_sign; |
| 369 |
x.ChangeSign(); |
| 370 |
} |
| 371 |
|
| 372 |
// we're reducing the period 2*PI |
| 373 |
// (for big values there'll always be zero) |
| 374 |
temp.Set2Pi(); |
| 375 |
|
| 376 |
if( x.Mod(temp) ) |
| 377 |
return 1; |
| 378 |
|
| 379 |
|
| 380 |
// we're setting 'x' as being in the range of <0, 0.5PI> |
| 381 |
|
| 382 |
temp.SetPi(); |
| 383 |
|
| 384 |
if( x > temp ) |
| 385 |
{ |
| 386 |
// x is in (pi, 2*pi> |
| 387 |
x.Sub( temp ); |
| 388 |
change_sign = !change_sign; |
| 389 |
} |
| 390 |
|
| 391 |
temp.Set05Pi(); |
| 392 |
|
| 393 |
if( x > temp ) |
| 394 |
{ |
| 395 |
// x is in (0.5pi, pi> |
| 396 |
x.Sub( temp ); |
| 397 |
x = temp - x; |
| 398 |
} |
| 399 |
|
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/*! |
| 405 |
an auxiliary function for calculating the Sine |
| 406 |
(you don't have to call this function) |
| 407 |
|
| 408 |
it returns Sin(x) where 'x' is from <0, PI/2> |
| 409 |
we're calculating the Sin with using Taylor series in zero or PI/2 |
| 410 |
(depending on which point of these two points is nearer to the 'x') |
| 411 |
|
| 412 |
Taylor series: |
| 413 |
sin(x) = sin(a) + cos(a)*(x-a)/(1!) |
| 414 |
- sin(a)*((x-a)^2)/(2!) - cos(a)*((x-a)^3)/(3!) |
| 415 |
+ sin(a)*((x-a)^4)/(4!) + ... |
| 416 |
|
| 417 |
when a=0 it'll be: |
| 418 |
sin(x) = (x)/(1!) - (x^3)/(3!) + (x^5)/(5!) - (x^7)/(7!) + (x^9)/(9!) ... |
| 419 |
|
| 420 |
and when a=PI/2: |
| 421 |
sin(x) = 1 - ((x-PI/2)^2)/(2!) + ((x-PI/2)^4)/(4!) - ((x-PI/2)^6)/(6!) ... |
| 422 |
*/ |
| 423 |
template<class ValueType> |
| 424 |
ValueType Sin0pi05(const ValueType & x) |
| 425 |
{ |
| 426 |
ValueType result; |
| 427 |
ValueType numerator, denominator; |
| 428 |
ValueType d_numerator, d_denominator; |
| 429 |
ValueType one, temp, old_result; |
| 430 |
|
| 431 |
// temp = pi/4 |
| 432 |
temp.Set05Pi(); |
| 433 |
temp.exponent.SubOne(); |
| 434 |
|
| 435 |
one.SetOne(); |
| 436 |
|
| 437 |
if( x < temp ) |
| 438 |
{ |
| 439 |
// we're using the Taylor series with a=0 |
| 440 |
result = x; |
| 441 |
numerator = x; |
| 442 |
denominator = one; |
| 443 |
|
| 444 |
// d_numerator = x^2 |
| 445 |
d_numerator = x; |
| 446 |
d_numerator.Mul(x); |
| 447 |
|
| 448 |
d_denominator = 2; |
| 449 |
} |
| 450 |
else |
| 451 |
{ |
| 452 |
// we're using the Taylor series with a=PI/2 |
| 453 |
result = one; |
| 454 |
numerator = one; |
| 455 |
denominator = one; |
| 456 |
|
| 457 |
// d_numerator = (x-pi/2)^2 |
| 458 |
ValueType pi05; |
| 459 |
pi05.Set05Pi(); |
| 460 |
|
| 461 |
temp = x; |
| 462 |
temp.Sub( pi05 ); |
| 463 |
d_numerator = temp; |
| 464 |
d_numerator.Mul( temp ); |
| 465 |
|
| 466 |
d_denominator = one; |
| 467 |
} |
| 468 |
|
| 469 |
uint c = 0; |
| 470 |
bool addition = false; |
| 471 |
|
| 472 |
old_result = result; |
| 473 |
for(uint i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i) |
| 474 |
{ |
| 475 |
// we're starting from a second part of the formula |
| 476 |
c += numerator. Mul( d_numerator ); |
| 477 |
c += denominator. Mul( d_denominator ); |
| 478 |
c += d_denominator.Add( one ); |
| 479 |
c += denominator. Mul( d_denominator ); |
| 480 |
c += d_denominator.Add( one ); |
| 481 |
temp = numerator; |
| 482 |
c += temp.Div(denominator); |
| 483 |
|
| 484 |
if( c ) |
| 485 |
// Sin is from <-1,1> and cannot make an overflow |
| 486 |
// but the carry can be from the Taylor series |
| 487 |
// (then we only break our calculations) |
| 488 |
break; |
| 489 |
|
| 490 |
if( addition ) |
| 491 |
result.Add( temp ); |
| 492 |
else |
| 493 |
result.Sub( temp ); |
| 494 |
|
| 495 |
|
| 496 |
addition = !addition; |
| 497 |
|
| 498 |
// we're testing whether the result has changed after adding |
| 499 |
// the next part of the Taylor formula, if not we end the loop |
| 500 |
// (it means 'x' is zero or 'x' is PI/2 or this part of the formula |
| 501 |
// is too small) |
| 502 |
if( result == old_result ) |
| 503 |
break; |
| 504 |
|
| 505 |
old_result = result; |
| 506 |
} |
| 507 |
|
| 508 |
return result; |
| 509 |
} |
| 510 |
|
| 511 |
} // namespace auxiliaryfunctions |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
/*! |
| 516 |
this function calculates the Sine |
| 517 |
*/ |
| 518 |
template<class ValueType> |
| 519 |
ValueType Sin(ValueType x, ErrorCode * err = 0) |
| 520 |
{ |
| 521 |
using namespace auxiliaryfunctions; |
| 522 |
|
| 523 |
ValueType one, result; |
| 524 |
bool change_sign; |
| 525 |
|
| 526 |
if( x.IsNan() ) |
| 527 |
{ |
| 528 |
if( err ) |
| 529 |
*err = err_improper_argument; |
| 530 |
|
| 531 |
return x; |
| 532 |
} |
| 533 |
|
| 534 |
if( err ) |
| 535 |
*err = err_ok; |
| 536 |
|
| 537 |
if( PrepareSin( x, change_sign ) ) |
| 538 |
{ |
| 539 |
// x is too big, we cannnot reduce the 2*PI period |
| 540 |
// prior to version 0.8.5 the result was zero |
| 541 |
|
| 542 |
// result has NaN flag set by default |
| 543 |
|
| 544 |
if( err ) |
| 545 |
*err = err_overflow; // maybe another error code? err_improper_argument? |
| 546 |
|
| 547 |
return result; // NaN is set by default |
| 548 |
} |
| 549 |
|
| 550 |
result = Sin0pi05( x ); |
| 551 |
|
| 552 |
one.SetOne(); |
| 553 |
|
| 554 |
// after calculations there can be small distortions in the result |
| 555 |
if( result > one ) |
| 556 |
result = one; |
| 557 |
else |
| 558 |
if( result.IsSign() ) |
| 559 |
// we've calculated the sin from <0, pi/2> and the result |
| 560 |
// should be positive |
| 561 |
result.SetZero(); |
| 562 |
|
| 563 |
if( change_sign ) |
| 564 |
result.ChangeSign(); |
| 565 |
|
| 566 |
return result; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
/*! |
| 571 |
this function calulates the Cosine |
| 572 |
we're using the formula cos(x) = sin(x + PI/2) |
| 573 |
*/ |
| 574 |
template<class ValueType> |
| 575 |
ValueType Cos(ValueType x, ErrorCode * err = 0) |
| 576 |
{ |
| 577 |
if( x.IsNan() ) |
| 578 |
{ |
| 579 |
if( err ) |
| 580 |
*err = err_improper_argument; |
| 581 |
|
| 582 |
return x; // NaN |
| 583 |
} |
| 584 |
|
| 585 |
ValueType pi05; |
| 586 |
pi05.Set05Pi(); |
| 587 |
|
| 588 |
uint c = x.Add( pi05 ); |
| 589 |
|
| 590 |
if( c ) |
| 591 |
{ |
| 592 |
if( err ) |
| 593 |
*err = err_overflow; |
| 594 |
|
| 595 |
return ValueType(); // result is undefined (NaN is set by default) |
| 596 |
} |
| 597 |
|
| 598 |
return Sin(x, err); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
/*! |
| 603 |
this function calulates the Tangent |
| 604 |
we're using the formula tan(x) = sin(x) / cos(x) |
| 605 |
|
| 606 |
it takes more time than calculating the Tan directly |
| 607 |
from for example Taylor series but should be a bit preciser |
| 608 |
because Tan receives its values from -infinity to +infinity |
| 609 |
and when we calculate it from any series then we can make |
| 610 |
a greater mistake than calculating 'sin/cos' |
| 611 |
*/ |
| 612 |
template<class ValueType> |
| 613 |
ValueType Tan(const ValueType & x, ErrorCode * err = 0) |
| 614 |
{ |
| 615 |
ValueType result = Cos(x, err); |
| 616 |
|
| 617 |
if( err && *err != err_ok ) |
| 618 |
return result; |
| 619 |
|
| 620 |
if( result.IsZero() ) |
| 621 |
{ |
| 622 |
if( err ) |
| 623 |
*err = err_improper_argument; |
| 624 |
|
| 625 |
result.SetNan(); |
| 626 |
|
| 627 |
return result; |
| 628 |
} |
| 629 |
|
| 630 |
return Sin(x, err) / result; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
/*! |
| 635 |
this function calulates the Tangent |
| 636 |
look at the description of Tan(...) |
| 637 |
|
| 638 |
(the abbreviation of Tangent can be 'tg' as well) |
| 639 |
*/ |
| 640 |
template<class ValueType> |
| 641 |
ValueType Tg(const ValueType & x, ErrorCode * err = 0) |
| 642 |
{ |
| 643 |
return Tan(x, err); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
/*! |
| 648 |
this function calulates the Cotangent |
| 649 |
we're using the formula tan(x) = cos(x) / sin(x) |
| 650 |
|
| 651 |
(why do we make it in this way? |
| 652 |
look at information in Tan() function) |
| 653 |
*/ |
| 654 |
template<class ValueType> |
| 655 |
ValueType Cot(const ValueType & x, ErrorCode * err = 0) |
| 656 |
{ |
| 657 |
ValueType result = Sin(x, err); |
| 658 |
|
| 659 |
if( err && *err != err_ok ) |
| 660 |
return result; |
| 661 |
|
| 662 |
if( result.IsZero() ) |
| 663 |
{ |
| 664 |
if( err ) |
| 665 |
*err = err_improper_argument; |
| 666 |
|
| 667 |
result.SetNan(); |
| 668 |
|
| 669 |
return result; |
| 670 |
} |
| 671 |
|
| 672 |
return Cos(x, err) / result; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
/*! |
| 677 |
this function calulates the Cotangent |
| 678 |
look at the description of Cot(...) |
| 679 |
|
| 680 |
(the abbreviation of Cotangent can be 'ctg' as well) |
| 681 |
*/ |
| 682 |
template<class ValueType> |
| 683 |
ValueType Ctg(const ValueType & x, ErrorCode * err = 0) |
| 684 |
{ |
| 685 |
return Cot(x, err); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
/* |
| 690 |
* |
| 691 |
* inverse trigonometric functions |
| 692 |
* |
| 693 |
* |
| 694 |
*/ |
| 695 |
|
| 696 |
namespace auxiliaryfunctions |
| 697 |
{ |
| 698 |
|
| 699 |
/*! |
| 700 |
an auxiliary function for calculating the Arc Sine |
| 701 |
|
| 702 |
we're calculating asin from the following formula: |
| 703 |
asin(x) = x + (1*x^3)/(2*3) + (1*3*x^5)/(2*4*5) + (1*3*5*x^7)/(2*4*6*7) + ... |
| 704 |
where abs(x) <= 1 |
| 705 |
|
| 706 |
we're using this formula when x is from <0, 1/2> |
| 707 |
*/ |
| 708 |
template<class ValueType> |
| 709 |
ValueType ASin_0(const ValueType & x) |
| 710 |
{ |
| 711 |
ValueType nominator, denominator, nominator_add, nominator_x, denominator_add, denominator_x; |
| 712 |
ValueType two, result(x), x2(x); |
| 713 |
ValueType nominator_temp, denominator_temp, old_result = result; |
| 714 |
uint c = 0; |
| 715 |
|
| 716 |
x2.Mul(x); |
| 717 |
two = 2; |
| 718 |
|
| 719 |
nominator.SetOne(); |
| 720 |
denominator = two; |
| 721 |
nominator_add = nominator; |
| 722 |
denominator_add = denominator; |
| 723 |
nominator_x = x; |
| 724 |
denominator_x = 3; |
| 725 |
|
| 726 |
for(uint i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i) |
| 727 |
{ |
| 728 |
c += nominator_x.Mul(x2); |
| 729 |
nominator_temp = nominator_x; |
| 730 |
c += nominator_temp.Mul(nominator); |
| 731 |
denominator_temp = denominator; |
| 732 |
c += denominator_temp.Mul(denominator_x); |
| 733 |
c += nominator_temp.Div(denominator_temp); |
| 734 |
|
| 735 |
// if there is a carry somewhere we only break the calculating |
| 736 |
// the result should be ok -- it's from <-pi/2, pi/2> |
| 737 |
if( c ) |
| 738 |
break; |
| 739 |
|
| 740 |
result.Add(nominator_temp); |
| 741 |
|
| 742 |
if( result == old_result ) |
| 743 |
// there's no sense to calculate more |
| 744 |
break; |
| 745 |
|
| 746 |
old_result = result; |
| 747 |
|
| 748 |
|
| 749 |
c += nominator_add.Add(two); |
| 750 |
c += denominator_add.Add(two); |
| 751 |
c += nominator.Mul(nominator_add); |
| 752 |
c += denominator.Mul(denominator_add); |
| 753 |
c += denominator_x.Add(two); |
| 754 |
} |
| 755 |
|
| 756 |
return result; |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
/*! |
| 762 |
an auxiliary function for calculating the Arc Sine |
| 763 |
|
| 764 |
we're calculating asin from the following formula: |
| 765 |
asin(x) = pi/2 - sqrt(2)*sqrt(1-x) * asin_temp |
| 766 |
asin_temp = 1 + (1*(1-x))/((2*3)*(2)) + (1*3*(1-x)^2)/((2*4*5)*(4)) + (1*3*5*(1-x)^3)/((2*4*6*7)*(8)) + ... |
| 767 |
|
| 768 |
where abs(x) <= 1 |
| 769 |
|
| 770 |
we're using this formula when x is from (1/2, 1> |
| 771 |
*/ |
| 772 |
template<class ValueType> |
| 773 |
ValueType ASin_1(const ValueType & x) |
| 774 |
{ |
| 775 |
ValueType nominator, denominator, nominator_add, nominator_x, nominator_x_add, denominator_add, denominator_x; |
| 776 |
ValueType denominator2; |
| 777 |
ValueType one, two, result; |
| 778 |
ValueType nominator_temp, denominator_temp, old_result; |
| 779 |
uint c = 0; |
| 780 |
|
| 781 |
two = 2; |
| 782 |
|
| 783 |
one.SetOne(); |
| 784 |
nominator = one; |
| 785 |
result = one; |
| 786 |
old_result = result; |
| 787 |
denominator = two; |
| 788 |
nominator_add = nominator; |
| 789 |
denominator_add = denominator; |
| 790 |
nominator_x = one; |
| 791 |
nominator_x.Sub(x); |
| 792 |
nominator_x_add = nominator_x; |
| 793 |
denominator_x = 3; |
| 794 |
denominator2 = two; |
| 795 |
|
| 796 |
|
| 797 |
for(uint i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i) |
| 798 |
{ |
| 799 |
nominator_temp = nominator_x; |
| 800 |
c += nominator_temp.Mul(nominator); |
| 801 |
denominator_temp = denominator; |
| 802 |
c += denominator_temp.Mul(denominator_x); |
| 803 |
c += denominator_temp.Mul(denominator2); |
| 804 |
c += nominator_temp.Div(denominator_temp); |
| 805 |
|
| 806 |
// if there is a carry somewhere we only break the calculating |
| 807 |
// the result should be ok -- it's from <-pi/2, pi/2> |
| 808 |
if( c ) |
| 809 |
break; |
| 810 |
|
| 811 |
result.Add(nominator_temp); |
| 812 |
|
| 813 |
if( result == old_result ) |
| 814 |
// there's no sense to calculate more |
| 815 |
break; |
| 816 |
|
| 817 |
old_result = result; |
| 818 |
|
| 819 |
c += nominator_x.Mul(nominator_x_add); |
| 820 |
c += nominator_add.Add(two); |
| 821 |
c += denominator_add.Add(two); |
| 822 |
c += nominator.Mul(nominator_add); |
| 823 |
c += denominator.Mul(denominator_add); |
| 824 |
c += denominator_x.Add(two); |
| 825 |
c += denominator2.Mul(two); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
nominator_x_add.exponent.AddOne(); // *2 |
| 830 |
one.exponent.SubOne(); // =0.5 |
| 831 |
nominator_x_add.Pow(one); // =sqrt(nominator_x_add) |
| 832 |
result.Mul(nominator_x_add); |
| 833 |
|
| 834 |
one.Set05Pi(); |
| 835 |
one.Sub(result); |
| 836 |
|
| 837 |
return one; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
} // namespace auxiliaryfunctions |
| 842 |
|
| 843 |
|
| 844 |
/*! |
| 845 |
this function calculates the Arc Sine |
| 846 |
x is from <-1,1> |
| 847 |
*/ |
| 848 |
template<class ValueType> |
| 849 |
ValueType ASin(ValueType x, ErrorCode * err = 0) |
| 850 |
{ |
| 851 |
using namespace auxiliaryfunctions; |
| 852 |
|
| 853 |
ValueType result, one; |
| 854 |
one.SetOne(); |
| 855 |
bool change_sign = false; |
| 856 |
|
| 857 |
if( x.IsNan() ) |
| 858 |
{ |
| 859 |
if( err ) |
| 860 |
*err = err_improper_argument; |
| 861 |
|
| 862 |
return x; |
| 863 |
} |
| 864 |
|
| 865 |
if( x.GreaterWithoutSignThan(one) ) |
| 866 |
{ |
| 867 |
if( err ) |
| 868 |
*err = err_improper_argument; |
| 869 |
|
| 870 |
return result; // NaN is set by default |
| 871 |
} |
| 872 |
|
| 873 |
if( x.IsSign() ) |
| 874 |
{ |
| 875 |
change_sign = true; |
| 876 |
x.Abs(); |
| 877 |
} |
| 878 |
|
| 879 |
one.exponent.SubOne(); // =0.5 |
| 880 |
|
| 881 |
// asin(-x) = -asin(x) |
| 882 |
if( x.GreaterWithoutSignThan(one) ) |
| 883 |
result = ASin_1(x); |
| 884 |
else |
| 885 |
result = ASin_0(x); |
| 886 |
|
| 887 |
if( change_sign ) |
| 888 |
result.ChangeSign(); |
| 889 |
|
| 890 |
if( err ) |
| 891 |
*err = err_ok; |
| 892 |
|
| 893 |
return result; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
/*! |
| 898 |
this function calculates the Arc Cosine |
| 899 |
|
| 900 |
we're using the formula: |
| 901 |
acos(x) = pi/2 - asin(x) |
| 902 |
*/ |
| 903 |
template<class ValueType> |
| 904 |
ValueType ACos(const ValueType & x, ErrorCode * err = 0) |
| 905 |
{ |
| 906 |
ValueType temp; |
| 907 |
|
| 908 |
temp.Set05Pi(); |
| 909 |
temp.Sub(ASin(x, err)); |
| 910 |
|
| 911 |
return temp; |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
namespace auxiliaryfunctions |
| 917 |
{ |
| 918 |
|
| 919 |
/*! |
| 920 |
an auxiliary function for calculating the Arc Tangent |
| 921 |
|
| 922 |
arc tan (x) where x is in <0; 0.5) |
| 923 |
(x can be in (-0.5 ; 0.5) too) |
| 924 |
|
| 925 |
we're using the Taylor series expanded in zero: |
| 926 |
atan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + ... |
| 927 |
*/ |
| 928 |
template<class ValueType> |
| 929 |
ValueType ATan0(const ValueType & x) |
| 930 |
{ |
| 931 |
ValueType nominator, denominator, nominator_add, denominator_add, temp; |
| 932 |
ValueType result, old_result; |
| 933 |
bool adding = false; |
| 934 |
uint c = 0; |
| 935 |
|
| 936 |
result = x; |
| 937 |
old_result = result; |
| 938 |
nominator = x; |
| 939 |
nominator_add = x; |
| 940 |
nominator_add.Mul(x); |
| 941 |
|
| 942 |
denominator.SetOne(); |
| 943 |
denominator_add = 2; |
| 944 |
|
| 945 |
for(uint i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i) |
| 946 |
{ |
| 947 |
c += nominator.Mul(nominator_add); |
| 948 |
c += denominator.Add(denominator_add); |
| 949 |
|
| 950 |
temp = nominator; |
| 951 |
c += temp.Div(denominator); |
| 952 |
|
| 953 |
if( c ) |
| 954 |
// the result should be ok |
| 955 |
break; |
| 956 |
|
| 957 |
if( adding ) |
| 958 |
result.Add(temp); |
| 959 |
else |
| 960 |
result.Sub(temp); |
| 961 |
|
| 962 |
if( result == old_result ) |
| 963 |
// there's no sense to calculate more |
| 964 |
break; |
| 965 |
|
| 966 |
old_result = result; |
| 967 |
adding = !adding; |
| 968 |
} |
| 969 |
|
| 970 |
return result; |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
/*! |
| 975 |
an auxiliary function for calculating the Arc Tangent |
| 976 |
|
| 977 |
where x is in <0 ; 1> |
| 978 |
*/ |
| 979 |
template<class ValueType> |
| 980 |
ValueType ATan01(const ValueType & x) |
| 981 |
{ |
| 982 |
ValueType half; |
| 983 |
half.Set05(); |
| 984 |
|
| 985 |
/* |
| 986 |
it would be better if we chose about sqrt(2)-1=0.41... instead of 0.5 here |
| 987 |
|
| 988 |
because as you can see below: |
| 989 |
when x = sqrt(2)-1 |
| 990 |
abs(x) = abs( (x-1)/(1+x) ) |
| 991 |
so when we're calculating values around x |
| 992 |
then they will be better converged to each other |
| 993 |
|
| 994 |
for example if we have x=0.4999 then during calculating ATan0(0.4999) |
| 995 |
we have to make about 141 iterations but when we have x=0.5 |
| 996 |
then during calculating ATan0( (x-1)/(1+x) ) we have to make |
| 997 |
only about 89 iterations (both for Big<3,9>) |
| 998 |
|
| 999 |
in the future this 0.5 can be changed |
| 1000 |
*/ |
| 1001 |
if( x.SmallerWithoutSignThan(half) ) |
| 1002 |
return ATan0(x); |
| 1003 |
|
| 1004 |
|
| 1005 |
/* |
| 1006 |
x>=0.5 and x<=1 |
| 1007 |
(x can be even smaller than 0.5) |
| 1008 |
|
| 1009 |
y = atac(x) |
| 1010 |
x = tan(y) |
| 1011 |
|
| 1012 |
tan(y-b) = (tan(y)-tab(b)) / (1+tan(y)*tan(b)) |
| 1013 |
y-b = atan( (tan(y)-tab(b)) / (1+tan(y)*tan(b)) ) |
| 1014 |
y = b + atan( (x-tab(b)) / (1+x*tan(b)) ) |
| 1015 |
|
| 1016 |
let b = pi/4 |
| 1017 |
tan(b) = tan(pi/4) = 1 |
| 1018 |
y = pi/4 + atan( (x-1)/(1+x) ) |
| 1019 |
|
| 1020 |
so |
| 1021 |
atac(x) = pi/4 + atan( (x-1)/(1+x) ) |
| 1022 |
when x->1 (x converges to 1) the (x-1)/(1+x) -> 0 |
| 1023 |
and we can use ATan0() function here |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
ValueType n(x),d(x),one,result; |
| 1027 |
|
| 1028 |
one.SetOne(); |
| 1029 |
n.Sub(one); |
| 1030 |
d.Add(one); |
| 1031 |
n.Div(d); |
| 1032 |
|
| 1033 |
result = ATan0(n); |
| 1034 |
|
| 1035 |
n.Set05Pi(); |
| 1036 |
n.exponent.SubOne(); // =pi/4 |
| 1037 |
result.Add(n); |
| 1038 |
|
| 1039 |
return result; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
/*! |
| 1044 |
an auxiliary function for calculating the Arc Tangent |
| 1045 |
where x > 1 |
| 1046 |
|
| 1047 |
we're using the formula: |
| 1048 |
atan(x) = pi/2 - atan(1/x) for x>0 |
| 1049 |
*/ |
| 1050 |
template<class ValueType> |
| 1051 |
ValueType ATanGreaterThanPlusOne(const ValueType & x) |
| 1052 |
{ |
| 1053 |
ValueType temp, atan; |
| 1054 |
|
| 1055 |
temp.SetOne(); |
| 1056 |
|
| 1057 |
if( temp.Div(x) ) |
| 1058 |
{ |
| 1059 |
// if there was a carry here that means x is very big |
| 1060 |
// and atan(1/x) fast converged to 0 |
| 1061 |
atan.SetZero(); |
| 1062 |
} |
| 1063 |
else |
| 1064 |
atan = ATan01(temp); |
| 1065 |
|
| 1066 |
temp.Set05Pi(); |
| 1067 |
temp.Sub(atan); |
| 1068 |
|
| 1069 |
return temp; |
| 1070 |
} |
| 1071 |
|
| 1072 |
} // namespace auxiliaryfunctions |
| 1073 |
|
| 1074 |
|
| 1075 |
/*! |
| 1076 |
this function calculates the Arc Tangent |
| 1077 |
*/ |
| 1078 |
template<class ValueType> |
| 1079 |
ValueType ATan(ValueType x) |
| 1080 |
{ |
| 1081 |
using namespace auxiliaryfunctions; |
| 1082 |
|
| 1083 |
ValueType one, result; |
| 1084 |
one.SetOne(); |
| 1085 |
bool change_sign = false; |
| 1086 |
|
| 1087 |
if( x.IsNan() ) |
| 1088 |
return x; |
| 1089 |
|
| 1090 |
// if x is negative we're using the formula: |
| 1091 |
// atan(-x) = -atan(x) |
| 1092 |
if( x.IsSign() ) |
| 1093 |
{ |
| 1094 |
change_sign = true; |
| 1095 |
x.Abs(); |
| 1096 |
} |
| 1097 |
|
| 1098 |
if( x.GreaterWithoutSignThan(one) ) |
| 1099 |
result = ATanGreaterThanPlusOne(x); |
| 1100 |
else |
| 1101 |
result = ATan01(x); |
| 1102 |
|
| 1103 |
if( change_sign ) |
| 1104 |
result.ChangeSign(); |
| 1105 |
|
| 1106 |
return result; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
/*! |
| 1111 |
this function calculates the Arc Tangent |
| 1112 |
look at the description of ATan(...) |
| 1113 |
|
| 1114 |
(the abbreviation of Arc Tangent can be 'atg' as well) |
| 1115 |
*/ |
| 1116 |
template<class ValueType> |
| 1117 |
ValueType ATg(const ValueType & x) |
| 1118 |
{ |
| 1119 |
return ATan(x); |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
/*! |
| 1124 |
this function calculates the Arc Cotangent |
| 1125 |
|
| 1126 |
we're using the formula: |
| 1127 |
actan(x) = pi/2 - atan(x) |
| 1128 |
*/ |
| 1129 |
template<class ValueType> |
| 1130 |
ValueType ACot(const ValueType & x) |
| 1131 |
{ |
| 1132 |
ValueType result; |
| 1133 |
|
| 1134 |
result.Set05Pi(); |
| 1135 |
result.Sub(ATan(x)); |
| 1136 |
|
| 1137 |
return result; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
/*! |
| 1142 |
this function calculates the Arc Cotangent |
| 1143 |
look at the description of ACot(...) |
| 1144 |
|
| 1145 |
(the abbreviation of Arc Cotangent can be 'actg' as well) |
| 1146 |
*/ |
| 1147 |
template<class ValueType> |
| 1148 |
ValueType ACtg(const ValueType & x) |
| 1149 |
{ |
| 1150 |
return ACot(x); |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
/* |
| 1155 |
* |
| 1156 |
* hyperbolic functions |
| 1157 |
* |
| 1158 |
* |
| 1159 |
*/ |
| 1160 |
|
| 1161 |
|
| 1162 |
/*! |
| 1163 |
this function calculates the Hyperbolic Sine |
| 1164 |
|
| 1165 |
we're using the formula sinh(x)= ( e^x - e^(-x) ) / 2 |
| 1166 |
*/ |
| 1167 |
template<class ValueType> |
| 1168 |
ValueType Sinh(const ValueType & x, ErrorCode * err = 0) |
| 1169 |
{ |
| 1170 |
if( x.IsNan() ) |
| 1171 |
{ |
| 1172 |
if( err ) |
| 1173 |
*err = err_improper_argument; |
| 1174 |
|
| 1175 |
return x; // NaN |
| 1176 |
} |
| 1177 |
|
| 1178 |
ValueType ex, emx; |
| 1179 |
uint c = 0; |
| 1180 |
|
| 1181 |
c += ex.Exp(x); |
| 1182 |
c += emx.Exp(-x); |
| 1183 |
|
| 1184 |
c += ex.Sub(emx); |
| 1185 |
c += ex.exponent.SubOne(); |
| 1186 |
|
| 1187 |
if( err ) |
| 1188 |
*err = c ? err_overflow : err_ok; |
| 1189 |
|
| 1190 |
return ex; |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
/*! |
| 1195 |
this function calculates the Hyperbolic Cosine |
| 1196 |
|
| 1197 |
we're using the formula cosh(x)= ( e^x + e^(-x) ) / 2 |
| 1198 |
*/ |
| 1199 |
template<class ValueType> |
| 1200 |
ValueType Cosh(const ValueType & x, ErrorCode * err = 0) |
| 1201 |
{ |
| 1202 |
if( x.IsNan() ) |
| 1203 |
{ |
| 1204 |
if( err ) |
| 1205 |
*err = err_improper_argument; |
| 1206 |
|
| 1207 |
return x; // NaN |
| 1208 |
} |
| 1209 |
|
| 1210 |
ValueType ex, emx; |
| 1211 |
uint c = 0; |
| 1212 |
|
| 1213 |
c += ex.Exp(x); |
| 1214 |
c += emx.Exp(-x); |
| 1215 |
|
| 1216 |
c += ex.Add(emx); |
| 1217 |
c += ex.exponent.SubOne(); |
| 1218 |
|
| 1219 |
if( err ) |
| 1220 |
*err = c ? err_overflow : err_ok; |
| 1221 |
|
| 1222 |
return ex; |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
/*! |
| 1227 |
this function calculates the Hyperbolic Tangent |
| 1228 |
|
| 1229 |
we're using the formula tanh(x)= ( e^x - e^(-x) ) / ( e^x + e^(-x) ) |
| 1230 |
*/ |
| 1231 |
template<class ValueType> |
| 1232 |
ValueType Tanh(const ValueType & x, ErrorCode * err = 0) |
| 1233 |
{ |
| 1234 |
if( x.IsNan() ) |
| 1235 |
{ |
| 1236 |
if( err ) |
| 1237 |
*err = err_improper_argument; |
| 1238 |
|
| 1239 |
return x; // NaN |
| 1240 |
} |
| 1241 |
|
| 1242 |
ValueType ex, emx, nominator, denominator; |
| 1243 |
uint c = 0; |
| 1244 |
|
| 1245 |
c += ex.Exp(x); |
| 1246 |
c += emx.Exp(-x); |
| 1247 |
|
| 1248 |
nominator = ex; |
| 1249 |
c += nominator.Sub(emx); |
| 1250 |
denominator = ex; |
| 1251 |
c += denominator.Add(emx); |
| 1252 |
|
| 1253 |
c += nominator.Div(denominator); |
| 1254 |
|
| 1255 |
if( err ) |
| 1256 |
*err = c ? err_overflow : err_ok; |
| 1257 |
|
| 1258 |
return nominator; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
/*! |
| 1263 |
this function calculates the Hyperbolic Tangent |
| 1264 |
look at the description of Tanh(...) |
| 1265 |
|
| 1266 |
(the abbreviation of Hyperbolic Tangent can be 'tgh' as well) |
| 1267 |
*/ |
| 1268 |
template<class ValueType> |
| 1269 |
ValueType Tgh(const ValueType & x, ErrorCode * err = 0) |
| 1270 |
{ |
| 1271 |
return Tanh(x, err); |
| 1272 |
} |
| 1273 |
|
| 1274 |
/*! |
| 1275 |
this function calculates the Hyperbolic Cotangent |
| 1276 |
|
| 1277 |
we're using the formula coth(x)= ( e^x + e^(-x) ) / ( e^x - e^(-x) ) |
| 1278 |
*/ |
| 1279 |
template<class ValueType> |
| 1280 |
ValueType Coth(const ValueType & x, ErrorCode * err = 0) |
| 1281 |
{ |
| 1282 |
if( x.IsNan() ) |
| 1283 |
{ |
| 1284 |
if( err ) |
| 1285 |
*err = err_improper_argument; |
| 1286 |
|
| 1287 |
return x; // NaN |
| 1288 |
} |
| 1289 |
|
| 1290 |
if( x.IsZero() ) |
| 1291 |
{ |
| 1292 |
if( err ) |
| 1293 |
*err = err_improper_argument; |
| 1294 |
|
| 1295 |
return ValueType(); // NaN is set by default |
| 1296 |
} |
| 1297 |
|
| 1298 |
ValueType ex, emx, nominator, denominator; |
| 1299 |
uint c = 0; |
| 1300 |
|
| 1301 |
c += ex.Exp(x); |
| 1302 |
c += emx.Exp(-x); |
| 1303 |
|
| 1304 |
nominator = ex; |
| 1305 |
c += nominator.Add(emx); |
| 1306 |
denominator = ex; |
| 1307 |
c += denominator.Sub(emx); |
| 1308 |
|
| 1309 |
c += nominator.Div(denominator); |
| 1310 |
|
| 1311 |
if( err ) |
| 1312 |
*err = c ? err_overflow : err_ok; |
| 1313 |
|
| 1314 |
return nominator; |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
/*! |
| 1319 |
this function calculates the Hyperbolic Cotangent |
| 1320 |
look at the description of Coth(...) |
| 1321 |
|
| 1322 |
(the abbreviation of Hyperbolic Cotangent can be 'ctgh' as well) |
| 1323 |
*/ |
| 1324 |
template<class ValueType> |
| 1325 |
ValueType Ctgh(const ValueType & x, ErrorCode * err = 0) |
| 1326 |
{ |
| 1327 |
return Coth(x, err); |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
/* |
| 1332 |
* |
| 1333 |
* inverse hyperbolic functions |
| 1334 |
* |
| 1335 |
* |
| 1336 |
*/ |
| 1337 |
|
| 1338 |
|
| 1339 |
/*! |
| 1340 |
inverse hyperbolic sine |
| 1341 |
|
| 1342 |
asinh(x) = ln( x + sqrt(x^2 + 1) ) |
| 1343 |
*/ |
| 1344 |
template<class ValueType> |
| 1345 |
ValueType ASinh(const ValueType & x, ErrorCode * err = 0) |
| 1346 |
{ |
| 1347 |
if( x.IsNan() ) |
| 1348 |
{ |
| 1349 |
if( err ) |
| 1350 |
*err = err_improper_argument; |
| 1351 |
|
| 1352 |
return x; // NaN |
| 1353 |
} |
| 1354 |
|
| 1355 |
ValueType xx(x), one, result; |
| 1356 |
uint c = 0; |
| 1357 |
one.SetOne(); |
| 1358 |
|
| 1359 |
c += xx.Mul(x); |
| 1360 |
c += xx.Add(one); |
| 1361 |
one.exponent.SubOne(); // one=0.5 |
| 1362 |
// xx is >= 1 |
| 1363 |
c += xx.PowFrac(one); // xx=sqrt(xx) |
| 1364 |
c += xx.Add(x); |
| 1365 |
c += result.Ln(xx); // xx > 0 |
| 1366 |
|
| 1367 |
// here can only be a carry |
| 1368 |
if( err ) |
| 1369 |
*err = c ? err_overflow : err_ok; |
| 1370 |
|
| 1371 |
return result; |
| 1372 |
} |
| 1373 |
|
| 1374 |
|
| 1375 |
/*! |
| 1376 |
inverse hyperbolic cosine |
| 1377 |
|
| 1378 |
acosh(x) = ln( x + sqrt(x^2 - 1) ) x in <1, infinity) |
| 1379 |
*/ |
| 1380 |
template<class ValueType> |
| 1381 |
ValueType ACosh(const ValueType & x, ErrorCode * err = 0) |
| 1382 |
{ |
| 1383 |
if( x.IsNan() ) |
| 1384 |
{ |
| 1385 |
if( err ) |
| 1386 |
*err = err_improper_argument; |
| 1387 |
|
| 1388 |
return x; // NaN |
| 1389 |
} |
| 1390 |
|
| 1391 |
ValueType xx(x), one, result; |
| 1392 |
uint c = 0; |
| 1393 |
one.SetOne(); |
| 1394 |
|
| 1395 |
if( x < one ) |
| 1396 |
{ |
| 1397 |
if( err ) |
| 1398 |
*err = err_improper_argument; |
| 1399 |
|
| 1400 |
return result; // NaN is set by default |
| 1401 |
} |
| 1402 |
|
| 1403 |
c += xx.Mul(x); |
| 1404 |
c += xx.Sub(one); |
| 1405 |
// xx is >= 0 |
| 1406 |
// we can't call a PowFrac when the 'x' is zero |
| 1407 |
// if x is 0 the sqrt(0) is 0 |
| 1408 |
if( !xx.IsZero() ) |
| 1409 |
{ |
| 1410 |
one.exponent.SubOne(); // one=0.5 |
| 1411 |
c += xx.PowFrac(one); // xx=sqrt(xx) |
| 1412 |
} |
| 1413 |
c += xx.Add(x); |
| 1414 |
c += result.Ln(xx); // xx >= 1 |
| 1415 |
|
| 1416 |
// here can only be a carry |
| 1417 |
if( err ) |
| 1418 |
*err = c ? err_overflow : err_ok; |
| 1419 |
|
| 1420 |
return result; |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
/*! |
| 1425 |
inverse hyperbolic tangent |
| 1426 |
|
| 1427 |
atanh(x) = 0.5 * ln( (1+x) / (1-x) ) x in (-1, 1) |
| 1428 |
*/ |
| 1429 |
template<class ValueType> |
| 1430 |
ValueType ATanh(const ValueType & x, ErrorCode * err = 0) |
| 1431 |
{ |
| 1432 |
if( x.IsNan() ) |
| 1433 |
{ |
| 1434 |
if( err ) |
| 1435 |
*err = err_improper_argument; |
| 1436 |
|
| 1437 |
return x; // NaN |
| 1438 |
} |
| 1439 |
|
| 1440 |
ValueType nominator(x), denominator, one, result; |
| 1441 |
uint c = 0; |
| 1442 |
one.SetOne(); |
| 1443 |
|
| 1444 |
if( !x.SmallerWithoutSignThan(one) ) |
| 1445 |
{ |
| 1446 |
if( err ) |
| 1447 |
*err = err_improper_argument; |
| 1448 |
|
| 1449 |
return result; // NaN is set by default |
| 1450 |
} |
| 1451 |
|
| 1452 |
c += nominator.Add(one); |
| 1453 |
denominator = one; |
| 1454 |
c += denominator.Sub(x); |
| 1455 |
c += nominator.Div(denominator); |
| 1456 |
c += result.Ln(nominator); |
| 1457 |
c += result.exponent.SubOne(); |
| 1458 |
|
| 1459 |
// here can only be a carry |
| 1460 |
if( err ) |
| 1461 |
*err = c ? err_overflow : err_ok; |
| 1462 |
|
| 1463 |
return result; |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
/*! |
| 1468 |
inverse hyperbolic tantent |
| 1469 |
*/ |
| 1470 |
template<class ValueType> |
| 1471 |
ValueType ATgh(const ValueType & x, ErrorCode * err = 0) |
| 1472 |
{ |
| 1473 |
return ATanh(x, err); |
| 1474 |
} |
| 1475 |
|
| 1476 |
|
| 1477 |
/*! |
| 1478 |
inverse hyperbolic cotangent |
| 1479 |
|
| 1480 |
acoth(x) = 0.5 * ln( (x+1) / (x-1) ) x in (-infinity, -1) or (1, infinity) |
| 1481 |
*/ |
| 1482 |
template<class ValueType> |
| 1483 |
ValueType ACoth(const ValueType & x, ErrorCode * err = 0) |
| 1484 |
{ |
| 1485 |
if( x.IsNan() ) |
| 1486 |
{ |
| 1487 |
if( err ) |
| 1488 |
*err = err_improper_argument; |
| 1489 |
|
| 1490 |
return x; // NaN |
| 1491 |
} |
| 1492 |
|
| 1493 |
ValueType nominator(x), denominator(x), one, result; |
| 1494 |
uint c = 0; |
| 1495 |
one.SetOne(); |
| 1496 |
|
| 1497 |
if( !x.GreaterWithoutSignThan(one) ) |
| 1498 |
{ |
| 1499 |
if( err ) |
| 1500 |
*err = err_improper_argument; |
| 1501 |
|
| 1502 |
return result; // NaN is set by default |
| 1503 |
} |
| 1504 |
|
| 1505 |
c += nominator.Add(one); |
| 1506 |
c += denominator.Sub(one); |
| 1507 |
c += nominator.Div(denominator); |
| 1508 |
c += result.Ln(nominator); |
| 1509 |
c += result.exponent.SubOne(); |
| 1510 |
|
| 1511 |
// here can only be a carry |
| 1512 |
if( err ) |
| 1513 |
*err = c ? err_overflow : err_ok; |
| 1514 |
|
| 1515 |
return result; |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
/*! |
| 1520 |
inverse hyperbolic cotantent |
| 1521 |
*/ |
| 1522 |
template<class ValueType> |
| 1523 |
ValueType ACtgh(const ValueType & x, ErrorCode * err = 0) |
| 1524 |
{ |
| 1525 |
return ACoth(x, err); |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
/* |
| 1533 |
* |
| 1534 |
* functions for converting between degrees, radians and gradians |
| 1535 |
* |
| 1536 |
* |
| 1537 |
*/ |
| 1538 |
|
| 1539 |
|
| 1540 |
/*! |
| 1541 |
this function converts degrees to radians |
| 1542 |
|
| 1543 |
it returns: x * pi / 180 |
| 1544 |
*/ |
| 1545 |
template<class ValueType> |
| 1546 |
ValueType DegToRad(const ValueType & x, ErrorCode * err = 0) |
| 1547 |
{ |
| 1548 |
ValueType result, temp; |
| 1549 |
uint c = 0; |
| 1550 |
|
| 1551 |
if( x.IsNan() ) |
| 1552 |
{ |
| 1553 |
if( err ) |
| 1554 |
*err = err_improper_argument; |
| 1555 |
|
| 1556 |
return x; |
| 1557 |
} |
| 1558 |
|
| 1559 |
result = x; |
| 1560 |
|
| 1561 |
// it is better to make division first and then multiplication |
| 1562 |
// the result is more accurate especially when x is: 90,180,270 or 360 |
| 1563 |
temp = 180; |
| 1564 |
c += result.Div(temp); |
| 1565 |
|
| 1566 |
temp.SetPi(); |
| 1567 |
c += result.Mul(temp); |
| 1568 |
|
| 1569 |
if( err ) |
| 1570 |
*err = c ? err_overflow : err_ok; |
| 1571 |
|
| 1572 |
return result; |
| 1573 |
} |
| 1574 |
|
| 1575 |
|
| 1576 |
/*! |
| 1577 |
this function converts radians to degrees |
| 1578 |
|
| 1579 |
it returns: x * 180 / pi |
| 1580 |
*/ |
| 1581 |
template<class ValueType> |
| 1582 |
ValueType RadToDeg(const ValueType & x, ErrorCode * err = 0) |
| 1583 |
{ |
| 1584 |
ValueType result, delimiter; |
| 1585 |
uint c = 0; |
| 1586 |
|
| 1587 |
if( x.IsNan() ) |
| 1588 |
{ |
| 1589 |
if( err ) |
| 1590 |
*err = err_improper_argument; |
| 1591 |
|
| 1592 |
return x; |
| 1593 |
} |
| 1594 |
|
| 1595 |
result = 180; |
| 1596 |
c += result.Mul(x); |
| 1597 |
|
| 1598 |
delimiter.SetPi(); |
| 1599 |
c += result.Div(delimiter); |
| 1600 |
|
| 1601 |
if( err ) |
| 1602 |
*err = c ? err_overflow : err_ok; |
| 1603 |
|
| 1604 |
return result; |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
/*! |
| 1609 |
this function converts degrees in the long format into one value |
| 1610 |
|
| 1611 |
long format: (degrees, minutes, seconds) |
| 1612 |
minutes and seconds must be greater than or equal zero |
| 1613 |
|
| 1614 |
result: |
| 1615 |
if d>=0 : result= d + ((s/60)+m)/60 |
| 1616 |
if d<0 : result= d - ((s/60)+m)/60 |
| 1617 |
|
| 1618 |
((s/60)+m)/60 = (s+60*m)/3600 (second version is faster because |
| 1619 |
there's only one division) |
| 1620 |
|
| 1621 |
for example: |
| 1622 |
DegToDeg(10, 30, 0) = 10.5 |
| 1623 |
DegToDeg(10, 24, 35.6)=10.4098(8) |
| 1624 |
*/ |
| 1625 |
template<class ValueType> |
| 1626 |
ValueType DegToDeg( const ValueType & d, const ValueType & m, const ValueType & s, |
| 1627 |
ErrorCode * err = 0) |
| 1628 |
{ |
| 1629 |
ValueType delimiter, multipler; |
| 1630 |
uint c = 0; |
| 1631 |
|
| 1632 |
if( d.IsNan() || m.IsNan() || s.IsNan() || m.IsSign() || s.IsSign() ) |
| 1633 |
{ |
| 1634 |
if( err ) |
| 1635 |
*err = err_improper_argument; |
| 1636 |
|
| 1637 |
delimiter.SetZeroNan(); // not needed, only to get rid of GCC warning about an uninitialized variable |
| 1638 |
|
| 1639 |
return delimiter; |
| 1640 |
} |
| 1641 |
|
| 1642 |
multipler = 60; |
| 1643 |
delimiter = 3600; |
| 1644 |
|
| 1645 |
c += multipler.Mul(m); |
| 1646 |
c += multipler.Add(s); |
| 1647 |
c += multipler.Div(delimiter); |
| 1648 |
|
| 1649 |
if( d.IsSign() ) |
| 1650 |
multipler.ChangeSign(); |
| 1651 |
|
| 1652 |
c += multipler.Add(d); |
| 1653 |
|
| 1654 |
if( err ) |
| 1655 |
*err = c ? err_overflow : err_ok; |
| 1656 |
|
| 1657 |
return multipler; |
| 1658 |
} |
| 1659 |
|
| 1660 |
|
| 1661 |
/*! |
| 1662 |
this function converts degrees in the long format to radians |
| 1663 |
*/ |
| 1664 |
template<class ValueType> |
| 1665 |
ValueType DegToRad( const ValueType & d, const ValueType & m, const ValueType & s, |
| 1666 |
ErrorCode * err = 0) |
| 1667 |
{ |
| 1668 |
ValueType temp_deg = DegToDeg(d,m,s,err); |
| 1669 |
|
| 1670 |
if( err && *err!=err_ok ) |
| 1671 |
return temp_deg; |
| 1672 |
|
| 1673 |
return DegToRad(temp_deg, err); |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
/*! |
| 1678 |
this function converts gradians to radians |
| 1679 |
|
| 1680 |
it returns: x * pi / 200 |
| 1681 |
*/ |
| 1682 |
template<class ValueType> |
| 1683 |
ValueType GradToRad(const ValueType & x, ErrorCode * err = 0) |
| 1684 |
{ |
| 1685 |
ValueType result, temp; |
| 1686 |
uint c = 0; |
| 1687 |
|
| 1688 |
if( x.IsNan() ) |
| 1689 |
{ |
| 1690 |
if( err ) |
| 1691 |
*err = err_improper_argument; |
| 1692 |
|
| 1693 |
return x; |
| 1694 |
} |
| 1695 |
|
| 1696 |
result = x; |
| 1697 |
|
| 1698 |
// it is better to make division first and then multiplication |
| 1699 |
// the result is more accurate especially when x is: 100,200,300 or 400 |
| 1700 |
temp = 200; |
| 1701 |
c += result.Div(temp); |
| 1702 |
|
| 1703 |
temp.SetPi(); |
| 1704 |
c += result.Mul(temp); |
| 1705 |
|
| 1706 |
if( err ) |
| 1707 |
*err = c ? err_overflow : err_ok; |
| 1708 |
|
| 1709 |
return result; |
| 1710 |
} |
| 1711 |
|
| 1712 |
|
| 1713 |
/*! |
| 1714 |
this function converts radians to gradians |
| 1715 |
|
| 1716 |
it returns: x * 200 / pi |
| 1717 |
*/ |
| 1718 |
template<class ValueType> |
| 1719 |
ValueType RadToGrad(const ValueType & x, ErrorCode * err = 0) |
| 1720 |
{ |
| 1721 |
ValueType result, delimiter; |
| 1722 |
uint c = 0; |
| 1723 |
|
| 1724 |
if( x.IsNan() ) |
| 1725 |
{ |
| 1726 |
if( err ) |
| 1727 |
*err = err_improper_argument; |
| 1728 |
|
| 1729 |
return x; |
| 1730 |
} |
| 1731 |
|
| 1732 |
result = 200; |
| 1733 |
c += result.Mul(x); |
| 1734 |
|
| 1735 |
delimiter.SetPi(); |
| 1736 |
c += result.Div(delimiter); |
| 1737 |
|
| 1738 |
if( err ) |
| 1739 |
*err = c ? err_overflow : err_ok; |
| 1740 |
|
| 1741 |
return result; |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
/*! |
| 1746 |
this function converts degrees to gradians |
| 1747 |
|
| 1748 |
it returns: x * 200 / 180 |
| 1749 |
*/ |
| 1750 |
template<class ValueType> |
| 1751 |
ValueType DegToGrad(const ValueType & x, ErrorCode * err = 0) |
| 1752 |
{ |
| 1753 |
ValueType result, temp; |
| 1754 |
uint c = 0; |
| 1755 |
|
| 1756 |
if( x.IsNan() ) |
| 1757 |
{ |
| 1758 |
if( err ) |
| 1759 |
*err = err_improper_argument; |
| 1760 |
|
| 1761 |
return x; |
| 1762 |
} |
| 1763 |
|
| 1764 |
result = x; |
| 1765 |
|
| 1766 |
temp = 200; |
| 1767 |
c += result.Mul(temp); |
| 1768 |
|
| 1769 |
temp = 180; |
| 1770 |
c += result.Div(temp); |
| 1771 |
|
| 1772 |
if( err ) |
| 1773 |
*err = c ? err_overflow : err_ok; |
| 1774 |
|
| 1775 |
return result; |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
/*! |
| 1780 |
this function converts degrees in the long format to gradians |
| 1781 |
*/ |
| 1782 |
template<class ValueType> |
| 1783 |
ValueType DegToGrad( const ValueType & d, const ValueType & m, const ValueType & s, |
| 1784 |
ErrorCode * err = 0) |
| 1785 |
{ |
| 1786 |
ValueType temp_deg = DegToDeg(d,m,s,err); |
| 1787 |
|
| 1788 |
if( err && *err!=err_ok ) |
| 1789 |
return temp_deg; |
| 1790 |
|
| 1791 |
return DegToGrad(temp_deg, err); |
| 1792 |
} |
| 1793 |
|
| 1794 |
|
| 1795 |
/*! |
| 1796 |
this function converts degrees to gradians |
| 1797 |
|
| 1798 |
it returns: x * 180 / 200 |
| 1799 |
*/ |
| 1800 |
template<class ValueType> |
| 1801 |
ValueType GradToDeg(const ValueType & x, ErrorCode * err = 0) |
| 1802 |
{ |
| 1803 |
ValueType result, temp; |
| 1804 |
uint c = 0; |
| 1805 |
|
| 1806 |
if( x.IsNan() ) |
| 1807 |
{ |
| 1808 |
if( err ) |
| 1809 |
*err = err_improper_argument; |
| 1810 |
|
| 1811 |
return x; |
| 1812 |
} |
| 1813 |
|
| 1814 |
result = x; |
| 1815 |
|
| 1816 |
temp = 180; |
| 1817 |
c += result.Mul(temp); |
| 1818 |
|
| 1819 |
temp = 200; |
| 1820 |
c += result.Div(temp); |
| 1821 |
|
| 1822 |
if( err ) |
| 1823 |
*err = c ? err_overflow : err_ok; |
| 1824 |
|
| 1825 |
return result; |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
/* |
| 1832 |
* |
| 1833 |
* another functions |
| 1834 |
* |
| 1835 |
* |
| 1836 |
*/ |
| 1837 |
|
| 1838 |
|
| 1839 |
/*! |
| 1840 |
this function calculates the square root |
| 1841 |
|
| 1842 |
Sqrt(9) = 3 |
| 1843 |
*/ |
| 1844 |
template<class ValueType> |
| 1845 |
ValueType Sqrt(ValueType x, ErrorCode * err = 0) |
| 1846 |
{ |
| 1847 |
if( x.IsNan() || x.IsSign() ) |
| 1848 |
{ |
| 1849 |
if( err ) |
| 1850 |
*err = err_improper_argument; |
| 1851 |
|
| 1852 |
x.SetNan(); |
| 1853 |
|
| 1854 |
return x; |
| 1855 |
} |
| 1856 |
|
| 1857 |
uint c = x.Sqrt(); |
| 1858 |
|
| 1859 |
if( err ) |
| 1860 |
*err = c ? err_overflow : err_ok; |
| 1861 |
|
| 1862 |
return x; |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
namespace auxiliaryfunctions |
| 1868 |
{ |
| 1869 |
|
| 1870 |
template<class ValueType> |
| 1871 |
bool RootCheckIndexSign(ValueType & x, const ValueType & index, ErrorCode * err) |
| 1872 |
{ |
| 1873 |
if( index.IsSign() ) |
| 1874 |
{ |
| 1875 |
// index cannot be negative |
| 1876 |
if( err ) |
| 1877 |
*err = err_improper_argument; |
| 1878 |
|
| 1879 |
x.SetNan(); |
| 1880 |
|
| 1881 |
return true; |
| 1882 |
} |
| 1883 |
|
| 1884 |
return false; |
| 1885 |
} |
| 1886 |
|
| 1887 |
|
| 1888 |
template<class ValueType> |
| 1889 |
bool RootCheckIndexZero(ValueType & x, const ValueType & index, ErrorCode * err) |
| 1890 |
{ |
| 1891 |
if( index.IsZero() ) |
| 1892 |
{ |
| 1893 |
if( x.IsZero() ) |
| 1894 |
{ |
| 1895 |
// there isn't root(0;0) - we assume it's not defined |
| 1896 |
if( err ) |
| 1897 |
*err = err_improper_argument; |
| 1898 |
|
| 1899 |
x.SetNan(); |
| 1900 |
|
| 1901 |
return true; |
| 1902 |
} |
| 1903 |
|
| 1904 |
// root(x;0) is 1 (if x!=0) |
| 1905 |
x.SetOne(); |
| 1906 |
|
| 1907 |
if( err ) |
| 1908 |
*err = err_ok; |
| 1909 |
|
| 1910 |
return true; |
| 1911 |
} |
| 1912 |
|
| 1913 |
return false; |
| 1914 |
} |
| 1915 |
|
| 1916 |
|
| 1917 |
template<class ValueType> |
| 1918 |
bool RootCheckIndexOne(const ValueType & index, ErrorCode * err) |
| 1919 |
{ |
| 1920 |
ValueType one; |
| 1921 |
one.SetOne(); |
| 1922 |
|
| 1923 |
if( index == one ) |
| 1924 |
{ |
| 1925 |
//root(x;1) is x |
| 1926 |
// we do it because if we used the PowFrac function |
| 1927 |
// we would lose the precision |
| 1928 |
if( err ) |
| 1929 |
*err = err_ok; |
| 1930 |
|
| 1931 |
return true; |
| 1932 |
} |
| 1933 |
|
| 1934 |
return false; |
| 1935 |
} |
| 1936 |
|
| 1937 |
|
| 1938 |
template<class ValueType> |
| 1939 |
bool RootCheckIndexTwo(ValueType & x, const ValueType & index, ErrorCode * err) |
| 1940 |
{ |
| 1941 |
if( index == 2 ) |
| 1942 |
{ |
| 1943 |
x = Sqrt(x, err); |
| 1944 |
|
| 1945 |
return true; |
| 1946 |
} |
| 1947 |
|
| 1948 |
return false; |
| 1949 |
} |
| 1950 |
|
| 1951 |
|
| 1952 |
template<class ValueType> |
| 1953 |
bool RootCheckIndexFrac(ValueType & x, const ValueType & index, ErrorCode * err) |
| 1954 |
{ |
| 1955 |
if( !index.IsInteger() ) |
| 1956 |
{ |
| 1957 |
// index must be integer |
| 1958 |
if( err ) |
| 1959 |
*err = err_improper_argument; |
| 1960 |
|
| 1961 |
x.SetNan(); |
| 1962 |
|
| 1963 |
return true; |
| 1964 |
} |
| 1965 |
|
| 1966 |
return false; |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
template<class ValueType> |
| 1971 |
bool RootCheckXZero(ValueType & x, ErrorCode * err) |
| 1972 |
{ |
| 1973 |
if( x.IsZero() ) |
| 1974 |
{ |
| 1975 |
// root(0;index) is zero (if index!=0) |
| 1976 |
// RootCheckIndexZero() must be called beforehand |
| 1977 |
x.SetZero(); |
| 1978 |
|
| 1979 |
if( err ) |
| 1980 |
*err = err_ok; |
| 1981 |
|
| 1982 |
return true; |
| 1983 |
} |
| 1984 |
|
| 1985 |
return false; |
| 1986 |
} |
| 1987 |
|
| 1988 |
|
| 1989 |
template<class ValueType> |
| 1990 |
bool RootCheckIndex(ValueType & x, const ValueType & index, ErrorCode * err, bool * change_sign) |
| 1991 |
{ |
| 1992 |
*change_sign = false; |
| 1993 |
|
| 1994 |
if( index.Mod2() ) |
| 1995 |
{ |
| 1996 |
// index is odd (1,3,5...) |
| 1997 |
if( x.IsSign() ) |
| 1998 |
{ |
| 1999 |
*change_sign = true; |
| 2000 |
x.Abs(); |
| 2001 |
} |
| 2002 |
} |
| 2003 |
else |
| 2004 |
{ |
| 2005 |
// index is even |
| 2006 |
// x cannot be negative |
| 2007 |
if( x.IsSign() ) |
| 2008 |
{ |
| 2009 |
if( err ) |
| 2010 |
*err = err_improper_argument; |
| 2011 |
|
| 2012 |
x.SetNan(); |
| 2013 |
|
| 2014 |
return true; |
| 2015 |
} |
| 2016 |
} |
| 2017 |
|
| 2018 |
return false; |
| 2019 |
} |
| 2020 |
|
| 2021 |
|
| 2022 |
template<class ValueType> |
| 2023 |
uint RootCorrectInteger(ValueType & old_x, ValueType & x, const ValueType & index) |
| 2024 |
{ |
| 2025 |
if( !old_x.IsInteger() || x.IsInteger() || !index.exponent.IsSign() ) |
| 2026 |
return 0; |
| 2027 |
|
| 2028 |
// old_x is integer, |
| 2029 |
// x is not integer, |
| 2030 |
// index is relatively small (index.exponent<0 or index.exponent<=0) |
| 2031 |
// (because we're using a special powering algorithm Big::PowUInt()) |
| 2032 |
|
| 2033 |
uint c = 0; |
| 2034 |
|
| 2035 |
ValueType temp(x); |
| 2036 |
c += temp.Round(); |
| 2037 |
|
| 2038 |
ValueType temp_round(temp); |
| 2039 |
c += temp.PowUInt(index); |
| 2040 |
|
| 2041 |
if( temp == old_x ) |
| 2042 |
x = temp_round; |
| 2043 |
|
| 2044 |
return (c==0)? 0 : 1; |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
} // namespace auxiliaryfunctions |
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
/*! |
| 2054 |
indexth Root of x |
| 2055 |
index must be integer and not negative <0;1;2;3....) |
| 2056 |
|
| 2057 |
if index==0 the result is one |
| 2058 |
if x==0 the result is zero and we assume root(0;0) is not defined |
| 2059 |
|
| 2060 |
if index is even (2;4;6...) the result is x^(1/index) and x>0 |
| 2061 |
if index is odd (1;2;3;...) the result is either |
| 2062 |
-(abs(x)^(1/index)) if x<0 or |
| 2063 |
x^(1/index)) if x>0 |
| 2064 |
|
| 2065 |
(for index==1 the result is equal x) |
| 2066 |
*/ |
| 2067 |
template<class ValueType> |
| 2068 |
ValueType Root(ValueType x, const ValueType & index, ErrorCode * err = 0) |
| 2069 |
{ |
| 2070 |
using namespace auxiliaryfunctions; |
| 2071 |
|
| 2072 |
if( x.IsNan() || index.IsNan() ) |
| 2073 |
{ |
| 2074 |
if( err ) |
| 2075 |
*err = err_improper_argument; |
| 2076 |
|
| 2077 |
x.SetNan(); |
| 2078 |
|
| 2079 |
return x; |
| 2080 |
} |
| 2081 |
|
| 2082 |
if( RootCheckIndexSign(x, index, err) ) return x; |
| 2083 |
if( RootCheckIndexZero(x, index, err) ) return x; |
| 2084 |
if( RootCheckIndexOne ( index, err) ) return x; |
| 2085 |
if( RootCheckIndexTwo (x, index, err) ) return x; |
| 2086 |
if( RootCheckIndexFrac(x, index, err) ) return x; |
| 2087 |
if( RootCheckXZero (x, err) ) return x; |
| 2088 |
|
| 2089 |
// index integer and index!=0 |
| 2090 |
// x!=0 |
| 2091 |
|
| 2092 |
ValueType old_x(x); |
| 2093 |
bool change_sign; |
| 2094 |
|
| 2095 |
if( RootCheckIndex(x, index, err, &change_sign ) ) return x; |
| 2096 |
|
| 2097 |
ValueType temp; |
| 2098 |
uint c = 0; |
| 2099 |
|
| 2100 |
// we're using the formula: root(x ; n) = exp( ln(x) / n ) |
| 2101 |
c += temp.Ln(x); |
| 2102 |
c += temp.Div(index); |
| 2103 |
c += x.Exp(temp); |
| 2104 |
|
| 2105 |
if( change_sign ) |
| 2106 |
{ |
| 2107 |
// x is different from zero |
| 2108 |
x.SetSign(); |
| 2109 |
} |
| 2110 |
|
| 2111 |
c += RootCorrectInteger(old_x, x, index); |
| 2112 |
|
| 2113 |
if( err ) |
| 2114 |
*err = c ? err_overflow : err_ok; |
| 2115 |
|
| 2116 |
return x; |
| 2117 |
} |
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
/*! |
| 2122 |
absolute value of x |
| 2123 |
e.g. -2 = 2 |
| 2124 |
2 = 2 |
| 2125 |
*/ |
| 2126 |
template<class ValueType> |
| 2127 |
ValueType Abs(const ValueType & x) |
| 2128 |
{ |
| 2129 |
ValueType result( x ); |
| 2130 |
result.Abs(); |
| 2131 |
|
| 2132 |
return result; |
| 2133 |
} |
| 2134 |
|
| 2135 |
|
| 2136 |
/*! |
| 2137 |
it returns the sign of the value |
| 2138 |
e.g. -2 = -1 |
| 2139 |
0 = 0 |
| 2140 |
10 = 1 |
| 2141 |
*/ |
| 2142 |
template<class ValueType> |
| 2143 |
ValueType Sgn(ValueType x) |
| 2144 |
{ |
| 2145 |
x.Sgn(); |
| 2146 |
|
| 2147 |
return x; |
| 2148 |
} |
| 2149 |
|
| 2150 |
|
| 2151 |
/*! |
| 2152 |
the remainder from a division |
| 2153 |
|
| 2154 |
e.g. |
| 2155 |
mod( 12.6 ; 3) = 0.6 because 12.6 = 3*4 + 0.6 |
| 2156 |
mod(-12.6 ; 3) = -0.6 bacause -12.6 = 3*(-4) + (-0.6) |
| 2157 |
mod( 12.6 ; -3) = 0.6 |
| 2158 |
mod(-12.6 ; -3) = -0.6 |
| 2159 |
*/ |
| 2160 |
template<class ValueType> |
| 2161 |
ValueType Mod(ValueType a, const ValueType & b, ErrorCode * err = 0) |
| 2162 |
{ |
| 2163 |
if( a.IsNan() || b.IsNan() ) |
| 2164 |
{ |
| 2165 |
if( err ) |
| 2166 |
*err = err_improper_argument; |
| 2167 |
|
| 2168 |
a.SetNan(); |
| 2169 |
|
| 2170 |
return a; |
| 2171 |
} |
| 2172 |
|
| 2173 |
uint c = a.Mod(b); |
| 2174 |
|
| 2175 |
if( err ) |
| 2176 |
*err = c ? err_overflow : err_ok; |
| 2177 |
|
| 2178 |
return a; |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
namespace auxiliaryfunctions |
| 2184 |
{ |
| 2185 |
|
| 2186 |
/*! |
| 2187 |
this function is used to store factorials in a given container |
| 2188 |
'more' means how many values should be added at the end |
| 2189 |
|
| 2190 |
e.g. |
| 2191 |
std::vector<ValueType> fact; |
| 2192 |
SetFactorialSequence(fact, 3); |
| 2193 |
// now the container has three values: 1 1 2 |
| 2194 |
|
| 2195 |
SetFactorialSequence(fact, 2); |
| 2196 |
// now the container has five values: 1 1 2 6 24 |
| 2197 |
*/ |
| 2198 |
template<class ValueType> |
| 2199 |
void SetFactorialSequence(std::vector<ValueType> & fact, uint more = 20) |
| 2200 |
{ |
| 2201 |
if( more == 0 ) |
| 2202 |
more = 1; |
| 2203 |
|
| 2204 |
uint start = static_cast<uint>(fact.size()); |
| 2205 |
fact.resize(fact.size() + more); |
| 2206 |
|
| 2207 |
if( start == 0 ) |
| 2208 |
{ |
| 2209 |
fact[0] = 1; |
| 2210 |
++start; |
| 2211 |
} |
| 2212 |
|
| 2213 |
for(uint i=start ; i<fact.size() ; ++i) |
| 2214 |
{ |
| 2215 |
fact[i] = fact[i-1]; |
| 2216 |
fact[i].MulInt(i); |
| 2217 |
} |
| 2218 |
} |
| 2219 |
|
| 2220 |
|
| 2221 |
/*! |
| 2222 |
an auxiliary function used to calculate Bernoulli numbers |
| 2223 |
|
| 2224 |
this function returns a sum: |
| 2225 |
sum(m) = sum_{k=0}^{m-1} {2^k * (m k) * B(k)} k in [0, m-1] (m k) means binomial coefficient = (m! / (k! * (m-k)!)) |
| 2226 |
|
| 2227 |
you should have sufficient factorials in cgamma.fact |
| 2228 |
(cgamma.fact should have at least m items) |
| 2229 |
|
| 2230 |
n_ should be equal 2 |
| 2231 |
*/ |
| 2232 |
template<class ValueType> |
| 2233 |
ValueType SetBernoulliNumbersSum(CGamma<ValueType> & cgamma, const ValueType & n_, uint m, |
| 2234 |
const volatile StopCalculating * stop = 0) |
| 2235 |
{ |
| 2236 |
ValueType k_, temp, temp2, temp3, sum; |
| 2237 |
|
| 2238 |
sum.SetZero(); |
| 2239 |
|
| 2240 |
for(uint k=0 ; k<m ; ++k) // k<m means k<=m-1 |
| 2241 |
{ |
| 2242 |
if( stop && (k & 15)==0 ) // means: k % 16 == 0 |
| 2243 |
if( stop->WasStopSignal() ) |
| 2244 |
return ValueType(); // NaN |
| 2245 |
|
| 2246 |
if( k>1 && (k & 1) == 1 ) // for that k the Bernoulli number is zero |
| 2247 |
continue; |
| 2248 |
|
| 2249 |
k_ = k; |
| 2250 |
|
| 2251 |
temp = n_; // n_ is equal 2 |
| 2252 |
temp.Pow(k_); |
| 2253 |
// temp = 2^k |
| 2254 |
|
| 2255 |
temp2 = cgamma.fact[m]; |
| 2256 |
temp3 = cgamma.fact[k]; |
| 2257 |
temp3.Mul(cgamma.fact[m-k]); |
| 2258 |
temp2.Div(temp3); |
| 2259 |
// temp2 = (m k) = m! / ( k! * (m-k)! ) |
| 2260 |
|
| 2261 |
temp.Mul(temp2); |
| 2262 |
temp.Mul(cgamma.bern[k]); |
| 2263 |
|
| 2264 |
sum.Add(temp); |
| 2265 |
// sum += 2^k * (m k) * B(k) |
| 2266 |
|
| 2267 |
if( sum.IsNan() ) |
| 2268 |
break; |
| 2269 |
} |
| 2270 |
|
| 2271 |
return sum; |
| 2272 |
} |
| 2273 |
|
| 2274 |
|
| 2275 |
/*! |
| 2276 |
an auxiliary function used to calculate Bernoulli numbers |
| 2277 |
start is >= 2 |
| 2278 |
|
| 2279 |
we use the recurrence formula: |
| 2280 |
B(m) = 1 / (2*(1 - 2^m)) * sum(m) |
| 2281 |
where sum(m) is calculated by SetBernoulliNumbersSum() |
| 2282 |
*/ |
| 2283 |
template<class ValueType> |
| 2284 |
bool SetBernoulliNumbersMore(CGamma<ValueType> & cgamma, uint start, const volatile StopCalculating * stop = 0) |
| 2285 |
{ |
| 2286 |
ValueType denominator, temp, temp2, temp3, m_, sum, sum2, n_, k_; |
| 2287 |
|
| 2288 |
const uint n = 2; |
| 2289 |
n_ = n; |
| 2290 |
|
| 2291 |
// start is >= 2 |
| 2292 |
for(uint m=start ; m<cgamma.bern.size() ; ++m) |
| 2293 |
{ |
| 2294 |
if( (m & 1) == 1 ) |
| 2295 |
{ |
| 2296 |
cgamma.bern[m].SetZero(); |
| 2297 |
} |
| 2298 |
else |
| 2299 |
{ |
| 2300 |
m_ = m; |
| 2301 |
|
| 2302 |
temp = n_; // n_ = 2 |
| 2303 |
temp.Pow(m_); |
| 2304 |
// temp = 2^m |
| 2305 |
|
| 2306 |
denominator.SetOne(); |
| 2307 |
denominator.Sub(temp); |
| 2308 |
if( denominator.exponent.AddOne() ) // it means: denominator.MulInt(2) |
| 2309 |
denominator.SetNan(); |
| 2310 |
|
| 2311 |
// denominator = 2 * (1 - 2^m) |
| 2312 |
|
| 2313 |
cgamma.bern[m] = SetBernoulliNumbersSum(cgamma, n_, m, stop); |
| 2314 |
|
| 2315 |
if( stop && stop->WasStopSignal() ) |
| 2316 |
{ |
| 2317 |
cgamma.bern.resize(m); // valid numbers are in [0, m-1] |
| 2318 |
return false; |
| 2319 |
} |
| 2320 |
|
| 2321 |
cgamma.bern[m].Div(denominator); |
| 2322 |
} |
| 2323 |
} |
| 2324 |
|
| 2325 |
return true; |
| 2326 |
} |
| 2327 |
|
| 2328 |
|
| 2329 |
/*! |
| 2330 |
this function is used to calculate Bernoulli numbers, |
| 2331 |
returns false if there was a stop signal, |
| 2332 |
'more' means how many values should be added at the end |
| 2333 |
|
| 2334 |
e.g. |
| 2335 |
typedef Big<1,2> MyBig; |
| 2336 |
CGamma<MyBig> cgamma; |
| 2337 |
SetBernoulliNumbers(cgamma, 3); |
| 2338 |
// now we have three first Bernoulli numbers: 1 -0.5 0.16667 |
| 2339 |
|
| 2340 |
SetBernoulliNumbers(cgamma, 4); |
| 2341 |
// now we have 7 Bernoulli numbers: 1 -0.5 0.16667 0 -0.0333 0 0.0238 |
| 2342 |
*/ |
| 2343 |
template<class ValueType> |
| 2344 |
bool SetBernoulliNumbers(CGamma<ValueType> & cgamma, uint more = 20, const volatile StopCalculating * stop = 0) |
| 2345 |
{ |
| 2346 |
if( more == 0 ) |
| 2347 |
more = 1; |
| 2348 |
|
| 2349 |
uint start = static_cast<uint>(cgamma.bern.size()); |
| 2350 |
cgamma.bern.resize(cgamma.bern.size() + more); |
| 2351 |
|
| 2352 |
if( start == 0 ) |
| 2353 |
{ |
| 2354 |
cgamma.bern[0].SetOne(); |
| 2355 |
++start; |
| 2356 |
} |
| 2357 |
|
| 2358 |
if( cgamma.bern.size() == 1 ) |
| 2359 |
return true; |
| 2360 |
|
| 2361 |
if( start == 1 ) |
| 2362 |
{ |
| 2363 |
cgamma.bern[1].Set05(); |
| 2364 |
cgamma.bern[1].ChangeSign(); |
| 2365 |
++start; |
| 2366 |
} |
| 2367 |
|
| 2368 |
// we should have sufficient factorials in cgamma.fact |
| 2369 |
if( cgamma.fact.size() < cgamma.bern.size() ) |
| 2370 |
SetFactorialSequence(cgamma.fact, static_cast<uint>(cgamma.bern.size() - cgamma.fact.size())); |
| 2371 |
|
| 2372 |
|
| 2373 |
return SetBernoulliNumbersMore(cgamma, start, stop); |
| 2374 |
} |
| 2375 |
|
| 2376 |
|
| 2377 |
/*! |
| 2378 |
an auxiliary function used to calculate the Gamma() function |
| 2379 |
|
| 2380 |
we calculate a sum: |
| 2381 |
sum(n) = sum_{m=2} { B(m) / ( (m^2 - m) * n^(m-1) ) } = 1/(12*n) - 1/(360*n^3) + 1/(1260*n^5) + ... |
| 2382 |
B(m) means a mth Bernoulli number |
| 2383 |
the sum starts from m=2, we calculate as long as the value will not change after adding a next part |
| 2384 |
*/ |
| 2385 |
template<class ValueType> |
| 2386 |
ValueType GammaFactorialHighSum(const ValueType & n, CGamma<ValueType> & cgamma, ErrorCode & err, |
| 2387 |
const volatile StopCalculating * stop) |
| 2388 |
{ |
| 2389 |
ValueType temp, temp2, denominator, sum, oldsum; |
| 2390 |
|
| 2391 |
sum.SetZero(); |
| 2392 |
|
| 2393 |
for(uint m=2 ; m<TTMATH_ARITHMETIC_MAX_LOOP ; m+=2) |
| 2394 |
{ |
| 2395 |
if( stop && (m & 3)==0 ) // (m & 3)==0 means: (m % 4)==0 |
| 2396 |
if( stop->WasStopSignal() ) |
| 2397 |
{ |
| 2398 |
err = err_interrupt; |
| 2399 |
return ValueType(); // NaN |
| 2400 |
} |
| 2401 |
|
| 2402 |
temp = (m-1); |
| 2403 |
denominator = n; |
| 2404 |
denominator.Pow(temp); |
| 2405 |
// denominator = n ^ (m-1) |
| 2406 |
|
| 2407 |
temp = m; |
| 2408 |
temp2 = temp; |
| 2409 |
temp.Mul(temp2); |
| 2410 |
temp.Sub(temp2); |
| 2411 |
// temp = m^2 - m |
| 2412 |
|
| 2413 |
denominator.Mul(temp); |
| 2414 |
// denominator = (m^2 - m) * n ^ (m-1) |
| 2415 |
|
| 2416 |
if( m >= cgamma.bern.size() ) |
| 2417 |
{ |
| 2418 |
if( !SetBernoulliNumbers(cgamma, m - cgamma.bern.size() + 1 + 3, stop) ) // 3 more than needed |
| 2419 |
{ |
| 2420 |
// there was the stop signal |
| 2421 |
err = err_interrupt; |
| 2422 |
return ValueType(); // NaN |
| 2423 |
} |
| 2424 |
} |
| 2425 |
|
| 2426 |
temp = cgamma.bern[m]; |
| 2427 |
temp.Div(denominator); |
| 2428 |
|
| 2429 |
oldsum = sum; |
| 2430 |
sum.Add(temp); |
| 2431 |
|
| 2432 |
if( sum.IsNan() || oldsum==sum ) |
| 2433 |
break; |
| 2434 |
} |
| 2435 |
|
| 2436 |
return sum; |
| 2437 |
} |
| 2438 |
|
| 2439 |
|
| 2440 |
/*! |
| 2441 |
an auxiliary function used to calculate the Gamma() function |
| 2442 |
|
| 2443 |
we calculate a helper function GammaFactorialHigh() by using Stirling's series: |
| 2444 |
n! = (n/e)^n * sqrt(2*pi*n) * exp( sum(n) ) |
| 2445 |
where n is a real number (not only an integer) and is sufficient large (greater than TTMATH_GAMMA_BOUNDARY) |
| 2446 |
and sum(n) is calculated by GammaFactorialHighSum() |
| 2447 |
*/ |
| 2448 |
template<class ValueType> |
| 2449 |
ValueType GammaFactorialHigh(const ValueType & n, CGamma<ValueType> & cgamma, ErrorCode & err, |
| 2450 |
const volatile StopCalculating * stop) |
| 2451 |
{ |
| 2452 |
ValueType temp, temp2, temp3, denominator, sum; |
| 2453 |
|
| 2454 |
temp.Set2Pi(); |
| 2455 |
temp.Mul(n); |
| 2456 |
temp2 = Sqrt(temp); |
| 2457 |
// temp2 = sqrt(2*pi*n) |
| 2458 |
|
| 2459 |
temp = n; |
| 2460 |
temp3.SetE(); |
| 2461 |
temp.Div(temp3); |
| 2462 |
temp.Pow(n); |
| 2463 |
// temp = (n/e)^n |
| 2464 |
|
| 2465 |
sum = GammaFactorialHighSum(n, cgamma, err, stop); |
| 2466 |
temp3.Exp(sum); |
| 2467 |
// temp3 = exp(sum) |
| 2468 |
|
| 2469 |
temp.Mul(temp2); |
| 2470 |
temp.Mul(temp3); |
| 2471 |
|
| 2472 |
return temp; |
| 2473 |
} |
| 2474 |
|
| 2475 |
|
| 2476 |
/*! |
| 2477 |
an auxiliary function used to calculate the Gamma() function |
| 2478 |
|
| 2479 |
Gamma(x) = GammaFactorialHigh(x-1) |
| 2480 |
*/ |
| 2481 |
template<class ValueType> |
| 2482 |
ValueType GammaPlusHigh(ValueType n, CGamma<ValueType> & cgamma, ErrorCode & err, const volatile StopCalculating * stop) |
| 2483 |
{ |
| 2484 |
ValueType one; |
| 2485 |
|
| 2486 |
one.SetOne(); |
| 2487 |
n.Sub(one); |
| 2488 |
|
| 2489 |
return GammaFactorialHigh(n, cgamma, err, stop); |
| 2490 |
} |
| 2491 |
|
| 2492 |
|
| 2493 |
/*! |
| 2494 |
an auxiliary function used to calculate the Gamma() function |
| 2495 |
|
| 2496 |
we use this function when n is integer and a small value (from 0 to TTMATH_GAMMA_BOUNDARY] |
| 2497 |
we use the formula: |
| 2498 |
gamma(n) = (n-1)! = 1 * 2 * 3 * ... * (n-1) |
| 2499 |
*/ |
| 2500 |
template<class ValueType> |
| 2501 |
ValueType GammaPlusLowIntegerInt(uint n, CGamma<ValueType> & cgamma) |
| 2502 |
{ |
| 2503 |
TTMATH_ASSERT( n > 0 ) |
| 2504 |
|
| 2505 |
if( n - 1 < static_cast<uint>(cgamma.fact.size()) ) |
| 2506 |
return cgamma.fact[n - 1]; |
| 2507 |
|
| 2508 |
ValueType res; |
| 2509 |
uint start = 2; |
| 2510 |
|
| 2511 |
if( cgamma.fact.size() < 2 ) |
| 2512 |
{ |
| 2513 |
res.SetOne(); |
| 2514 |
} |
| 2515 |
else |
| 2516 |
{ |
| 2517 |
start = static_cast<uint>(cgamma.fact.size()); |
| 2518 |
res = cgamma.fact[start-1]; |
| 2519 |
} |
| 2520 |
|
| 2521 |
for(uint i=start ; i<n ; ++i) |
| 2522 |
res.MulInt(i); |
| 2523 |
|
| 2524 |
return res; |
| 2525 |
} |
| 2526 |
|
| 2527 |
|
| 2528 |
/*! |
| 2529 |
an auxiliary function used to calculate the Gamma() function |
| 2530 |
|
| 2531 |
we use this function when n is integer and a small value (from 0 to TTMATH_GAMMA_BOUNDARY] |
| 2532 |
*/ |
| 2533 |
template<class ValueType> |
| 2534 |
ValueType GammaPlusLowInteger(const ValueType & n, CGamma<ValueType> & cgamma) |
| 2535 |
{ |
| 2536 |
sint n_; |
| 2537 |
|
| 2538 |
n.ToInt(n_); |
| 2539 |
|
| 2540 |
return GammaPlusLowIntegerInt(n_, cgamma); |
| 2541 |
} |
| 2542 |
|
| 2543 |
|
| 2544 |
/*! |
| 2545 |
an auxiliary function used to calculate the Gamma() function |
| 2546 |
|
| 2547 |
we use this function when n is a small value (from 0 to TTMATH_GAMMA_BOUNDARY] |
| 2548 |
we use a recurrence formula: |
| 2549 |
gamma(z+1) = z * gamma(z) |
| 2550 |
then: gamma(z) = gamma(z+1) / z |
| 2551 |
|
| 2552 |
e.g. |
| 2553 |
gamma(3.89) = gamma(2001.89) / ( 3.89 * 4.89 * 5.89 * ... * 1999.89 * 2000.89 ) |
| 2554 |
*/ |
| 2555 |
template<class ValueType> |
| 2556 |
ValueType GammaPlusLow(ValueType n, CGamma<ValueType> & cgamma, ErrorCode & err, const volatile StopCalculating * stop) |
| 2557 |
{ |
| 2558 |
ValueType one, denominator, temp, boundary; |
| 2559 |
|
| 2560 |
if( n.IsInteger() ) |
| 2561 |
return GammaPlusLowInteger(n, cgamma); |
| 2562 |
|
| 2563 |
one.SetOne(); |
| 2564 |
denominator = n; |
| 2565 |
boundary = TTMATH_GAMMA_BOUNDARY; |
| 2566 |
|
| 2567 |
while( n < boundary ) |
| 2568 |
{ |
| 2569 |
n.Add(one); |
| 2570 |
denominator.Mul(n); |
| 2571 |
} |
| 2572 |
|
| 2573 |
n.Add(one); |
| 2574 |
|
| 2575 |
// now n is sufficient big |
| 2576 |
temp = GammaPlusHigh(n, cgamma, err, stop); |
| 2577 |
temp.Div(denominator); |
| 2578 |
|
| 2579 |
return temp; |
| 2580 |
} |
| 2581 |
|
| 2582 |
|
| 2583 |
/*! |
| 2584 |
an auxiliary function used to calculate the Gamma() function |
| 2585 |
*/ |
| 2586 |
template<class ValueType> |
| 2587 |
ValueType GammaPlus(const ValueType & n, CGamma<ValueType> & cgamma, ErrorCode & err, const volatile StopCalculating * stop) |
| 2588 |
{ |
| 2589 |
if( n > TTMATH_GAMMA_BOUNDARY ) |
| 2590 |
return GammaPlusHigh(n, cgamma, err, stop); |
| 2591 |
|
| 2592 |
return GammaPlusLow(n, cgamma, err, stop); |
| 2593 |
} |
| 2594 |
|
| 2595 |
|
| 2596 |
/*! |
| 2597 |
an auxiliary function used to calculate the Gamma() function |
| 2598 |
|
| 2599 |
this function is used when n is negative |
| 2600 |
we use the reflection formula: |
| 2601 |
gamma(1-z) * gamma(z) = pi / sin(pi*z) |
| 2602 |
then: gamma(z) = pi / (sin(pi*z) * gamma(1-z)) |
| 2603 |
|
| 2604 |
*/ |
| 2605 |
template<class ValueType> |
| 2606 |
ValueType GammaMinus(const ValueType & n, CGamma<ValueType> & cgamma, ErrorCode & err, const volatile StopCalculating * stop) |
| 2607 |
{ |
| 2608 |
ValueType pi, denominator, temp, temp2; |
| 2609 |
|
| 2610 |
if( n.IsInteger() ) |
| 2611 |
{ |
| 2612 |
// gamma function is not defined when n is negative and integer |
| 2613 |
err = err_improper_argument; |
| 2614 |
return temp; // NaN |
| 2615 |
} |
| 2616 |
|
| 2617 |
pi.SetPi(); |
| 2618 |
|
| 2619 |
temp = pi; |
| 2620 |
temp.Mul(n); |
| 2621 |
temp2 = Sin(temp); |
| 2622 |
// temp2 = sin(pi * n) |
| 2623 |
|
| 2624 |
temp.SetOne(); |
| 2625 |
temp.Sub(n); |
| 2626 |
temp = GammaPlus(temp, cgamma, err, stop); |
| 2627 |
// temp = gamma(1 - n) |
| 2628 |
|
| 2629 |
temp.Mul(temp2); |
| 2630 |
pi.Div(temp); |
| 2631 |
|
| 2632 |
return pi; |
| 2633 |
} |
| 2634 |
|
| 2635 |
} // namespace auxiliaryfunctions |
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
/*! |
| 2640 |
this function calculates the Gamma function |
| 2641 |
|
| 2642 |
it's multithread safe, you should create a CGamma<> object and use it whenever you call the Gamma() |
| 2643 |
e.g. |
| 2644 |
typedef Big<1,2> MyBig; |
| 2645 |
MyBig x=234, y=345.53; |
| 2646 |
CGamma<MyBig> cgamma; |
| 2647 |
std::cout << Gamma(x, cgamma) << std::endl; |
| 2648 |
std::cout << Gamma(y, cgamma) << std::endl; |
| 2649 |
in the CGamma<> object the function stores some coefficients (factorials, Bernoulli numbers), |
| 2650 |
and they will be reused in next calls to the function |
| 2651 |
|
| 2652 |
each thread should have its own CGamma<> object, and you can use these objects with Factorial() function too |
| 2653 |
*/ |
| 2654 |
template<class ValueType> |
| 2655 |
ValueType Gamma(const ValueType & n, CGamma<ValueType> & cgamma, ErrorCode * err = 0, |
| 2656 |
const volatile StopCalculating * stop = 0) |
| 2657 |
{ |
| 2658 |
using namespace auxiliaryfunctions; |
| 2659 |
|
| 2660 |
ValueType result; |
| 2661 |
ErrorCode err_tmp; |
| 2662 |
|
| 2663 |
if( n.IsNan() ) |
| 2664 |
{ |
| 2665 |
if( err ) |
| 2666 |
*err = err_improper_argument; |
| 2667 |
|
| 2668 |
return n; |
| 2669 |
} |
| 2670 |
|
| 2671 |
if( cgamma.history.Get(n, result, err_tmp) ) |
| 2672 |
{ |
| 2673 |
if( err ) |
| 2674 |
*err = err_tmp; |
| 2675 |
|
| 2676 |
return result; |
| 2677 |
} |
| 2678 |
|
| 2679 |
err_tmp = err_ok; |
| 2680 |
|
| 2681 |
if( n.IsSign() ) |
| 2682 |
{ |
| 2683 |
result = GammaMinus(n, cgamma, err_tmp, stop); |
| 2684 |
} |
| 2685 |
else |
| 2686 |
if( n.IsZero() ) |
| 2687 |
{ |
| 2688 |
err_tmp = err_improper_argument; |
| 2689 |
result.SetNan(); |
| 2690 |
} |
| 2691 |
else |
| 2692 |
{ |
| 2693 |
result = GammaPlus(n, cgamma, err_tmp, stop); |
| 2694 |
} |
| 2695 |
|
| 2696 |
if( result.IsNan() && err_tmp==err_ok ) |
| 2697 |
err_tmp = err_overflow; |
| 2698 |
|
| 2699 |
if( err ) |
| 2700 |
*err = err_tmp; |
| 2701 |
|
| 2702 |
if( stop && !stop->WasStopSignal() ) |
| 2703 |
cgamma.history.Add(n, result, err_tmp); |
| 2704 |
|
| 2705 |
return result; |
| 2706 |
} |
| 2707 |
|
| 2708 |
|
| 2709 |
/*! |
| 2710 |
this function calculates the Gamma function |
| 2711 |
|
| 2712 |
note: this function should be used only in a single-thread environment |
| 2713 |
*/ |
| 2714 |
template<class ValueType> |
| 2715 |
ValueType Gamma(const ValueType & n, ErrorCode * err = 0) |
| 2716 |
{ |
| 2717 |
// warning: this static object is not thread safe |
| 2718 |
static CGamma<ValueType> cgamma; |
| 2719 |
|
| 2720 |
return Gamma(n, cgamma, err); |
| 2721 |
} |
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
namespace auxiliaryfunctions |
| 2726 |
{ |
| 2727 |
|
| 2728 |
/*! |
| 2729 |
an auxiliary function for calculating the factorial function |
| 2730 |
|
| 2731 |
we use the formula: |
| 2732 |
x! = gamma(x+1) |
| 2733 |
*/ |
| 2734 |
template<class ValueType> |
| 2735 |
ValueType Factorial2(ValueType x, |
| 2736 |
CGamma<ValueType> * cgamma = 0, |
| 2737 |
ErrorCode * err = 0, |
| 2738 |
const volatile StopCalculating * stop = 0) |
| 2739 |
{ |
| 2740 |
ValueType result, one; |
| 2741 |
|
| 2742 |
if( x.IsNan() || x.IsSign() || !x.IsInteger() ) |
| 2743 |
{ |
| 2744 |
if( err ) |
| 2745 |
*err = err_improper_argument; |
| 2746 |
|
| 2747 |
x.SetNan(); |
| 2748 |
|
| 2749 |
return x; |
| 2750 |
} |
| 2751 |
|
| 2752 |
one.SetOne(); |
| 2753 |
x.Add(one); |
| 2754 |
|
| 2755 |
if( cgamma ) |
| 2756 |
return Gamma(x, *cgamma, err, stop); |
| 2757 |
|
| 2758 |
return Gamma(x, err); |
| 2759 |
} |
| 2760 |
|
| 2761 |
} // namespace auxiliaryfunctions |
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
/*! |
| 2766 |
the factorial from given 'x' |
| 2767 |
e.g. |
| 2768 |
Factorial(4) = 4! = 1*2*3*4 |
| 2769 |
|
| 2770 |
it's multithread safe, you should create a CGamma<> object and use it whenever you call the Factorial() |
| 2771 |
e.g. |
| 2772 |
typedef Big<1,2> MyBig; |
| 2773 |
MyBig x=234, y=54345; |
| 2774 |
CGamma<MyBig> cgamma; |
| 2775 |
std::cout << Factorial(x, cgamma) << std::endl; |
| 2776 |
std::cout << Factorial(y, cgamma) << std::endl; |
| 2777 |
in the CGamma<> object the function stores some coefficients (factorials, Bernoulli numbers), |
| 2778 |
and they will be reused in next calls to the function |
| 2779 |
|
| 2780 |
each thread should have its own CGamma<> object, and you can use these objects with Gamma() function too |
| 2781 |
*/ |
| 2782 |
template<class ValueType> |
| 2783 |
ValueType Factorial(const ValueType & x, CGamma<ValueType> & cgamma, ErrorCode * err = 0, |
| 2784 |
const volatile StopCalculating * stop = 0) |
| 2785 |
{ |
| 2786 |
return auxiliaryfunctions::Factorial2(x, &cgamma, err, stop); |
| 2787 |
} |
| 2788 |
|
| 2789 |
|
| 2790 |
/*! |
| 2791 |
the factorial from given 'x' |
| 2792 |
e.g. |
| 2793 |
Factorial(4) = 4! = 1*2*3*4 |
| 2794 |
|
| 2795 |
note: this function should be used only in a single-thread environment |
| 2796 |
*/ |
| 2797 |
template<class ValueType> |
| 2798 |
ValueType Factorial(const ValueType & x, ErrorCode * err = 0) |
| 2799 |
{ |
| 2800 |
return auxiliaryfunctions::Factorial2(x, (CGamma<ValueType>*)0, err, 0); |
| 2801 |
} |
| 2802 |
|
| 2803 |
|
| 2804 |
/*! |
| 2805 |
this method prepares some coefficients: factorials and Bernoulli numbers |
| 2806 |
stored in 'fact' and 'bern' objects |
| 2807 |
|
| 2808 |
we're defining the method here because we're using Gamma() function which |
| 2809 |
is not available in ttmathobjects.h |
| 2810 |
|
| 2811 |
read the doc info in ttmathobjects.h file where CGamma<> struct is declared |
| 2812 |
*/ |
| 2813 |
template<class ValueType> |
| 2814 |
void CGamma<ValueType>::InitAll() |
| 2815 |
{ |
| 2816 |
ValueType x = TTMATH_GAMMA_BOUNDARY + 1; |
| 2817 |
|
| 2818 |
// history.Remove(x) removes only one object |
| 2819 |
// we must be sure that there are not others objects with the key 'x' |
| 2820 |
while( history.Remove(x) ) |
| 2821 |
{ |
| 2822 |
} |
| 2823 |
|
| 2824 |
// the simplest way to initialize is to call the Gamma function with (TTMATH_GAMMA_BOUNDARY + 1) |
| 2825 |
// when x is larger then fewer coefficients we need |
| 2826 |
Gamma(x, *this); |
| 2827 |
} |
| 2828 |
|
| 2829 |
|
| 2830 |
|
| 2831 |
} // namespace |
| 2832 |
|
| 2833 |
|
| 2834 |
#ifdef _MSC_VER |
| 2835 |
//warning C4127: conditional expression is constant |
| 2836 |
#pragma warning( default: 4127 ) |
| 2837 |
//warning C4702: unreachable code |
| 2838 |
#pragma warning( default: 4702 ) |
| 2839 |
//warning C4800: forcing value to bool 'true' or 'false' (performance warning) |
| 2840 |
#pragma warning( default: 4800 ) |
| 2841 |
#endif |
| 2842 |
|
| 2843 |
#endif |