001/** 002 * The MIT License (MIT) 003 * 004 * Copyright (c) 2015 decimal4j (tools4j), Marco Terzer 005 * 006 * Permission is hereby granted, free of charge, to any person obtaining a copy 007 * of this software and associated documentation files (the "Software"), to deal 008 * in the Software without restriction, including without limitation the rights 009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 010 * copies of the Software, and to permit persons to whom the Software is 011 * furnished to do so, subject to the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be included in all 014 * copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 022 * SOFTWARE. 023 */ 024package org.decimal4j.api; 025 026import java.math.BigDecimal; 027import java.math.BigInteger; 028import java.math.RoundingMode; 029 030import org.decimal4j.factory.DecimalFactory; 031import org.decimal4j.scale.ScaleMetrics; 032import org.decimal4j.truncate.OverflowMode; 033import org.decimal4j.truncate.TruncationPolicy; 034 035/** 036 * Signed fixed-precision decimal number similar to {@link BigDecimal}. A 037 * Decimal number can be immutable or mutable and it is based on an underlying 038 * <i>unscaled</i> long value and a {@link #getScale() scale}. The scale defines 039 * the number of digits to the right of the decimal point. If the scale is 040 * {@code f} then the value represented by a {@code Decimal} instance is 041 * <tt>(unscaledValue × 10<sup>-f</sup>)</tt>. 042 * <p> 043 * <i>Scale of Result and Operands</i> <br> 044 * The result of an arithmetic operation is generally of the same scale as this 045 * Decimal unless otherwise indicated. Decimal operands of arithmetic operations 046 * are typically also of the same scale as this Decimal. Scale compatibility of 047 * Decimal operands is enforced through the generic {@link ScaleMetrics} 048 * parameter {@code <S>}. 049 * <p> 050 * <i>Operands involving Type Conversion</i> <br> 051 * For convenience, arithmetic operations with other data types are sometimes 052 * also provided. Such operations usually perform a value conversion into a 053 * Decimal of the current scale before performing the actual operation. 054 * <p> 055 * <i>Rounding</i> <br> 056 * If the result of an arithmetic operation exceeds the precision of the current 057 * scale, {@link RoundingMode#HALF_UP HALF_UP} rounding is applied to the least 058 * significant digit of the result if no other rounding mode is explicitly 059 * specified. Note that in a few exceptional cases {@link RoundingMode#HALF_EVEN 060 * HALF_EVEN} rounding is used by default to comply with inherited specification 061 * constraints (e.g. see {@link #doubleValue()}, {@link #floatValue()} etc.). 062 * <p> 063 * <i>Overflows</i> <br> 064 * Operations with Decimal values can lead to overflows in marked contrast to 065 * the {@link BigDecimal}. This is a direct consequence of the construction of a 066 * Decimal value on the basis of a long value. Unless otherwise indicated 067 * operations silently truncate overflows by default. This choice has been made 068 * for performance reasons and because Java programmers are already familiar 069 * with this behavior from operations with primitive integer types. If this 070 * behavior is inappropriate for an application, exceptions in overflow 071 * situations can be enforced through an optional {@link OverflowMode} or 072 * {@link TruncationPolicy} argument. Some operations like conversion operations 073 * or arithmetic operations involving conversion <i>always</i> throw an 074 * exception if an overflow occurs. The documentation of operations which can 075 * cause an overflow always indicates the exact overflow behavior. 076 * <p> 077 * All methods for this interface throw {@code NullPointerException} when passed a 078 * {@code null} object reference for any input parameter. 079 * 080 * @param <S> 081 * the scale metrics type associated with this Decimal 082 */ 083public interface Decimal<S extends ScaleMetrics> extends Comparable<Decimal<S>> { 084 085 /** 086 * Returns the metrics associated with the scale of this Decimal. Scale 087 * defines the number of fraction digits and the scale factor applied to the 088 * {@code long} value underlying this {@code Decimal}. 089 * 090 * @return the scale metrics object 091 * @see ScaleMetrics#getScale() 092 * @see ScaleMetrics#getScaleFactor() 093 */ 094 S getScaleMetrics(); 095 096 /** 097 * Returns the scale associated with this Decimal. The scale defines the 098 * number of fraction digits and the scale factor applied to the 099 * {@code long} value underlying this {@code Decimal}. 100 * <p> 101 * If the scale is {@code f} then the value represented by a {@code Decimal} 102 * instance is <tt>(unscaledValue × 10<sup>-f</sup>)</tt>. 103 * <p> 104 * This method is a shortcut for {@code getScaleMetrics().getScale()}. 105 * 106 * @return the scale 107 * @see #getScaleMetrics() 108 * @see ScaleMetrics#getScale() 109 * @see #unscaledValue() 110 */ 111 int getScale(); 112 113 /** 114 * Returns the unscaled value underlying this {@code Decimal}. This 115 * {@code Decimal} is <tt>(unscaledValue × 10<sup>-f</sup>)</tt> with 116 * {@code f} representing the {@link #getScale() scale}, hence the returned 117 * value equals <tt>(10<sup>f</sup> × this)</tt>. 118 * 119 * @return the unscaled numeric value, the same as this Decimal but without 120 * applying the scale factor 121 * @see #getScale() 122 * @see ScaleMetrics#getScaleFactor() 123 */ 124 long unscaledValue(); 125 126 /** 127 * Returns the factory that can be used to create other Decimal values of 128 * the same scale as {@code this} Decimal. 129 * 130 * @return the factory to create other Decimal values of the same scale as 131 * this Decimal 132 */ 133 DecimalFactory<S> getFactory(); 134 135 /** 136 * Returns a {@code Decimal} whose value represents the integral part of 137 * {@code this} Decimal. The integral part corresponds to digits at the left 138 * of the decimal point. The result is {@code this} Decimal rounded to 139 * precision zero with {@link RoundingMode#DOWN}. 140 * <p> 141 * The returned value is a new instance if this Decimal is an 142 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 143 * internal state is altered and {@code this} is returned as result now 144 * representing the outcome of the operation. 145 * 146 * @return <code>⌊this⌋</code> for non-negative and 147 * <code>⌈this⌉</code> for negative values 148 * @see #fractionalPart() 149 * @see #isIntegral() 150 * @see #isIntegralPartZero() 151 * @see #round(int, RoundingMode) 152 */ 153 Decimal<S> integralPart(); 154 155 /** 156 * Returns a {@code Decimal} whose value represents the fractional part of 157 * {@code (this)} value. The fractional part corresponds to digits at the 158 * right of the decimal point. The result is {@code this} minus the integral 159 * part of this Decimal. 160 * <p> 161 * The returned value is a new instance if this Decimal is an 162 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 163 * internal state is altered and {@code this} is returned as result now 164 * representing the outcome of the operation. 165 * 166 * @return {@code this-integralPart()} which is always less than one and 167 * greater than minus one 168 * @see #integralPart() 169 * @see #isIntegral() 170 * @see #isIntegralPartZero() 171 */ 172 Decimal<S> fractionalPart(); 173 174 // some methods "inherited" from Number and BigDecimal 175 176 /** 177 * Returns the value of this {@code Decimal} as a {@code byte} after a 178 * narrowing primitive conversion. 179 * 180 * @return this {@code Decimal} converted to an {@code byte}. 181 * @see Number#byteValue() 182 * @see #byteValueExact() 183 */ 184 byte byteValue(); 185 186 /** 187 * Converts this {@code Decimal} to a {@code byte}, checking for lost 188 * information. If this {@code Decimal} has a nonzero fractional part or is 189 * out of the possible range for a {@code byte} result then an 190 * {@code ArithmeticException} is thrown. 191 * 192 * @return this {@code Decimal} converted to a {@code byte}. 193 * @throws ArithmeticException 194 * if {@code this} has a nonzero fractional part, or will not 195 * fit in a {@code byte}. 196 * @see #byteValue() 197 */ 198 byte byteValueExact(); 199 200 /** 201 * Returns the value of this {@code Decimal} as a {@code short} after a 202 * narrowing primitive conversion. 203 * 204 * @return this {@code Decimal} converted to an {@code short}. 205 * @see Number#shortValue() 206 * @see #shortValueExact() 207 */ 208 short shortValue(); 209 210 /** 211 * Converts this {@code Decimal} to a {@code short}, checking for lost 212 * information. If this {@code Decimal} has a nonzero fractional part or is 213 * out of the possible range for a {@code short} result then an 214 * {@code ArithmeticException} is thrown. 215 * 216 * @return this {@code Decimal} converted to a {@code short}. 217 * @throws ArithmeticException 218 * if {@code this} has a nonzero fractional part, or will not 219 * fit in a {@code short}. 220 * @see #shortValue() 221 */ 222 short shortValueExact(); 223 224 /** 225 * Converts this {@code Decimal} to an {@code int}. This conversion is 226 * analogous to the <i>narrowing primitive conversion</i> from 227 * {@code double} to {@code short} as defined in section 5.1.3 of <cite>The 228 * Java™ Language Specification</cite>: any fractional part of this 229 * {@code Decimal} will be discarded, and if the resulting "{@code long}" is 230 * too big to fit in an {@code int}, only the low-order 32 bits are 231 * returned. Note that this conversion can lose information about the 232 * overall magnitude and precision of this {@code Decimal} value as well as 233 * return a result with the opposite sign. 234 * 235 * @return this {@code Decimal} converted to an {@code int}. 236 * @see Number#intValue() 237 * @see #intValueExact() 238 */ 239 int intValue(); 240 241 /** 242 * Converts this {@code Decimal} to an {@code int}, checking for lost 243 * information. If this {@code Decimal} has a nonzero fractional part or is 244 * out of the possible range for an {@code int} result then an 245 * {@code ArithmeticException} is thrown. 246 * 247 * @return this {@code Decimal} converted to an {@code int}. 248 * @throws ArithmeticException 249 * if {@code this} has a nonzero fractional part, or will not 250 * fit in an {@code int}. 251 * @see #intValue() 252 */ 253 int intValueExact(); 254 255 /** 256 * Converts this {@code Decimal} to a {@code long}. This conversion is 257 * analogous to the <i>narrowing primitive conversion</i> from 258 * {@code double} to {@code short} as defined in section 5.1.3 of <cite>The 259 * Java™ Language Specification</cite>: any fractional part of this 260 * {@code Decimal} will be discarded. Note that this conversion can lose 261 * information about the precision of the {@code Decimal} value. 262 * <p> 263 * Note that this method uses {@link RoundingMode#DOWN} as defined by 264 * <cite>The Java™Language Specification</cite>. Other rounding modes 265 * are supported via {@link #longValue(RoundingMode)}. 266 * 267 * @return this {@code Decimal} converted to a {@code long}. 268 * @see Number#longValue() 269 * @see #longValue(RoundingMode) 270 * @see #longValueExact() 271 */ 272 long longValue(); 273 274 /** 275 * Converts this {@code Decimal} to a {@code long}, checking for lost 276 * information. If this {@code Decimal} has a nonzero fractional part or is 277 * out of the possible range for a {@code long} result then an 278 * {@code ArithmeticException} is thrown. 279 * 280 * @return this {@code Decimal} converted to a {@code long}. 281 * @throws ArithmeticException 282 * if {@code this} has a nonzero fractional part 283 * @see #longValue() 284 * @see #longValue(RoundingMode) 285 */ 286 long longValueExact(); 287 288 /** 289 * Converts this {@code Decimal} to a {@code float}. This conversion is 290 * similar to the <i>narrowing primitive conversion</i> from {@code double} 291 * to {@code float} as defined in section 5.1.3 of <cite>The Java™ 292 * Language Specification</cite>. Note that this conversion can lose 293 * information about the precision of the {@code Decimal} value. 294 * <p> 295 * Note that this method uses {@link RoundingMode#HALF_EVEN} as defined by 296 * <cite>The Java™Language Specification</cite>. Other rounding modes 297 * are supported via {@link #floatValue(RoundingMode)}. 298 * 299 * @return this {@code Decimal} converted to a {@code float}. 300 * @see Number#floatValue() 301 * @see #floatValue(RoundingMode) 302 */ 303 float floatValue(); 304 305 /** 306 * Converts this {@code Decimal} to a {@code double}. This conversion is 307 * similar to the <i>narrowing primitive conversion</i> from {@code double} 308 * to {@code float} as defined in section 5.1.3 of <cite>The Java™ 309 * Language Specification</cite>. Note that this conversion can lose 310 * information about the precision of the {@code Decimal} value. 311 * <p> 312 * Note that this method uses {@link RoundingMode#HALF_EVEN} as defined by 313 * <cite>The Java™Language Specification</cite>. Other rounding modes 314 * are supported via {@link #doubleValue(RoundingMode)}. 315 * 316 * @return this {@code Decimal} converted to a {@code double}. 317 * @see Number#doubleValue() 318 * @see #doubleValue(RoundingMode) 319 */ 320 double doubleValue(); 321 322 /** 323 * Converts this {@code Decimal} to a {@code BigInteger}. This conversion is 324 * analogous to the <i>narrowing primitive conversion</i> from 325 * {@code double} to {@code long} as defined in section 5.1.3 of <cite>The 326 * Java™ Language Specification</cite>: any fractional part of this 327 * {@code Decimal} will be discarded. Note that this conversion can lose 328 * information about the precision of the {@code Decimal} value. 329 * <p> 330 * To have an exception thrown if the conversion is inexact (in other words 331 * if a nonzero fractional part is discarded), use the 332 * {@link #toBigIntegerExact()} method. 333 * <p> 334 * Note that this method uses {@link RoundingMode#DOWN} to be consistent 335 * with other integer conversion methods as defined by <cite>The 336 * Java™Language Specification</cite>. Other rounding modes are 337 * supported via {@link #toBigInteger(RoundingMode)}. 338 * 339 * @return this {@code Decimal} converted to a {@code BigInteger}. 340 * @see #toBigIntegerExact() 341 * @see #toBigInteger(RoundingMode) 342 * @see #longValue() 343 */ 344 BigInteger toBigInteger(); 345 346 /** 347 * Converts this {@code Decimal} to a {@code BigInteger}, checking for lost 348 * information. An exception is thrown if this {@code Decimal} has a nonzero 349 * fractional part. 350 * 351 * @return this {@code Decimal} converted to a {@code BigInteger}. 352 * @throws ArithmeticException 353 * if {@code this} has a nonzero fractional part. 354 * @see #toBigInteger() 355 * @see #toBigInteger(RoundingMode) 356 * @see #longValueExact() 357 */ 358 BigInteger toBigIntegerExact(); 359 360 /** 361 * Converts this {@code Decimal} to a {@code BigDecimal} using the same 362 * {@link #getScale() scale} as this Decimal value. 363 * 364 * @return this {@code Decimal} converted to a {@code BigDecimal} with the 365 * same scale as this Decimal value. 366 * @see #toBigDecimal(int, RoundingMode) 367 */ 368 BigDecimal toBigDecimal(); 369 370 // mutable/immutable conversion methods 371 372 /** 373 * If this {@code Decimal} value is already an {@link ImmutableDecimal} it 374 * is simply returned. Otherwise a new immutable value with the same scale 375 * and numerical value as {@code this} Decimal is created and returned. 376 * 377 * @return {@code this} if immutable and a new {@link ImmutableDecimal} with 378 * the same scale and value as {@code this} Decimal otherwise 379 */ 380 ImmutableDecimal<S> toImmutableDecimal(); 381 382 /** 383 * If this {@code Decimal} value is already a {@link MutableDecimal} it is 384 * simply returned. Otherwise a new mutable value with the same scale and 385 * numerical value as {@code this} Decimal is created and returned. 386 * 387 * @return {@code this} if mutable and a new {@link MutableDecimal} with the 388 * same scale and value as {@code this} Decimal otherwise 389 */ 390 MutableDecimal<S> toMutableDecimal(); 391 392 // some conversion methods with rounding mode 393 394 /** 395 * Converts this {@code Decimal} to a {@code long} using the specified 396 * rounding mode if necessary. Rounding is applied if the Decimal value can 397 * not be represented as a long value, that is, if it has a nonzero 398 * fractional part. Note that this conversion can lose information about the 399 * precision of the {@code Decimal} value. 400 * 401 * @param roundingMode 402 * the rounding mode to apply when rounding is necessary to 403 * convert this Decimal into a long 404 * @return this {@code Decimal} converted to a {@code long}. 405 * @throws ArithmeticException 406 * if {@code roundingMode==UNNECESSARY} and rounding is 407 * necessary 408 * @see #longValue() 409 * @see #longValueExact() 410 */ 411 long longValue(RoundingMode roundingMode); 412 413 /** 414 * Converts this {@code Decimal} to a {@code float} using the specified 415 * rounding mode if the Decimal value can not be exactly represented as a 416 * float value. Note that this conversion can lose information about the 417 * precision of the {@code Decimal} value. 418 * 419 * @param roundingMode 420 * the rounding mode to apply when rounding is necessary to 421 * convert this Decimal into a float value 422 * @return this {@code Decimal} converted to a {@code float}. 423 * @throws ArithmeticException 424 * if {@code roundingMode==UNNECESSARY} and rounding is 425 * necessary 426 * @see #floatValue() 427 */ 428 float floatValue(RoundingMode roundingMode); 429 430 /** 431 * Converts this {@code Decimal} to a {@code double} using the specified 432 * rounding mode if the Decimal value can not be exactly represented as a 433 * double value. Note that this conversion can lose information about the 434 * precision of the {@code Decimal} value. 435 * 436 * @param roundingMode 437 * the rounding mode to apply when rounding is necessary to 438 * convert this Decimal into a double value 439 * @return this {@code Decimal} converted to a {@code double}. 440 * @throws ArithmeticException 441 * if {@code roundingMode==UNNECESSARY} and rounding is 442 * necessary 443 * @see #doubleValue() 444 */ 445 double doubleValue(RoundingMode roundingMode); 446 447 /** 448 * Converts this {@code Decimal} to a {@link BigInteger} value using the 449 * specified rounding mode if necessary. Rounding is applied if the Decimal 450 * value can not be represented as a {@code BigInteger}, that is, if it has 451 * a nonzero fractional part. Note that this conversion can lose information 452 * about the precision of the {@code Decimal} value. 453 * 454 * @param roundingMode 455 * the rounding mode to apply when rounding is necessary to 456 * convert this Decimal into a {@code BigInteger} 457 * @return this {@code Decimal} converted to a {@code BigInteger}. 458 * @throws ArithmeticException 459 * if {@code roundingMode==UNNECESSARY} and rounding is 460 * necessary 461 * @see #toBigInteger() 462 * @see #toBigIntegerExact() 463 */ 464 BigInteger toBigInteger(RoundingMode roundingMode); 465 466 /** 467 * Returns a {@code BigDecimal} value of the given scale using the specified 468 * rounding mode if necessary. 469 * 470 * @param scale 471 * the scale used for the returned {@code BigDecimal} 472 * @param roundingMode 473 * the rounding mode to apply when rounding is necessary to 474 * convert from the this Decimal's {@link #getScale() scale} to 475 * the target scale 476 * @return a {@code BigDecimal} instance of the specified scale 477 * @throws ArithmeticException 478 * if {@code roundingMode==UNNECESSARY} and rounding is 479 * necessary 480 * @see #toBigDecimal() 481 */ 482 BigDecimal toBigDecimal(int scale, RoundingMode roundingMode); 483 484 // methods to round and change the scale 485 486 /** 487 * Returns a {@code Decimal} value rounded to the specified 488 * {@code precision} using {@link RoundingMode#HALF_UP HALF_UP} rounding. If 489 * an overflow occurs due to the rounding operation, the result is silently 490 * truncated. 491 * <p> 492 * Note that contrary to the {@code scale(..)} operations this method does 493 * not change the scale of the value --- extra digits are simply zeroised. 494 * <p> 495 * The returned value is a new instance if this Decimal is an 496 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 497 * internal state is altered and {@code this} is returned as result now 498 * representing the rounded value. 499 * <p> 500 * <i>Examples and special cases:</i> 501 * <ul> 502 * <li><b>precision = 0</b><br> 503 * value is rounded to an integer value</li> 504 * <li><b>precision = 2</b><br> 505 * value is rounded to the second digit after the decimal point</li> 506 * <li><b>precision = -3</b><br> 507 * value is rounded to the thousands</li> 508 * <li><b>precision ≥ scale</b><br> 509 * values is returned unchanged</li> 510 * <li><b>precision < scale - 18</b><br> 511 * {@code IllegalArgumentException} is thrown</li> 512 * </ul> 513 * 514 * @param precision 515 * the precision to use for the rounding, for instance 2 to round 516 * to the second digit after the decimal point; must be at least 517 * {@code (scale - 18)} 518 * @return a Decimal instance rounded to the given precision 519 * @throws IllegalArgumentException 520 * if {@code precision < scale - 18} 521 * @see #round(int, RoundingMode) 522 * @see #round(int, TruncationPolicy) 523 * @see #scale(int) 524 */ 525 Decimal<S> round(int precision); 526 527 /** 528 * Returns a {@code Decimal} value rounded to the specified 529 * {@code precision} using the given rounding mode. If an overflow occurs 530 * due to the rounding operation, the result is silently truncated. 531 * <p> 532 * Note that contrary to the {@code scale(..)} operations this method does 533 * not change the scale of the value --- extra digits are simply zeroised. 534 * <p> 535 * The returned value is a new instance if this Decimal is an 536 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 537 * internal state is altered and {@code this} is returned as result now 538 * representing the rounded value. 539 * <p> 540 * <i>Examples and special cases:</i> 541 * <ul> 542 * <li><b>precision = 0</b><br> 543 * value is rounded to an integer value</li> 544 * <li><b>precision = 2</b><br> 545 * value is rounded to the second digit after the decimal point</li> 546 * <li><b>precision = -3</b><br> 547 * value is rounded to the thousands</li> 548 * <li><b>precision ≥ scale</b><br> 549 * values is returned unchanged</li> 550 * <li><b>precision < scale - 18</b><br> 551 * {@code IllegalArgumentException} is thrown</li> 552 * </ul> 553 * 554 * @param precision 555 * the precision to use for the rounding, for instance 2 to round 556 * to the second digit after the decimal point; must be at least 557 * {@code (scale - 18)} 558 * @param roundingMode 559 * the rounding mode to apply when rounding to the desired 560 * precision 561 * @return a Decimal instance rounded to the given precision 562 * @throws IllegalArgumentException 563 * if {@code precision < scale - 18} 564 * @throws ArithmeticException 565 * if {@code roundingMode==UNNECESSARY} and rounding is 566 * necessary 567 * @see #round(int) 568 * @see #round(int, TruncationPolicy) 569 * @see #scale(int, RoundingMode) 570 */ 571 Decimal<S> round(int precision, RoundingMode roundingMode); 572 573 /** 574 * Returns a {@code Decimal} value rounded to the specified 575 * {@code precision} using the given truncation policy. 576 * <p> 577 * Note that contrary to the {@code scale(..)} operations this method does 578 * not change the scale of the value --- extra digits are simply zeroised. 579 * <p> 580 * The returned value is a new instance if this Decimal is an 581 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 582 * internal state is altered and {@code this} is returned as result now 583 * representing the rounded value. 584 * <p> 585 * <i>Examples and special cases:</i> 586 * <ul> 587 * <li><b>precision = 0</b><br> 588 * value is rounded to an integer value</li> 589 * <li><b>precision = 2</b><br> 590 * value is rounded to the second digit after the decimal point</li> 591 * <li><b>precision = -3</b><br> 592 * value is rounded to the thousands</li> 593 * <li><b>precision ≥ scale</b><br> 594 * values is returned unchanged</li> 595 * <li><b>precision < scale - 18</b><br> 596 * {@code IllegalArgumentException} is thrown</li> 597 * </ul> 598 * 599 * @param precision 600 * the precision to use for the rounding, for instance 2 to round 601 * to the second digit after the decimal point; must be at least 602 * {@code (scale - 18)} 603 * @param truncationPolicy 604 * the truncation policy defining {@link RoundingMode} and 605 * {@link OverflowMode} for the rounding operation 606 * @return a Decimal instance rounded to the given precision 607 * @throws IllegalArgumentException 608 * if {@code precision < scale - 18} 609 * @throws ArithmeticException 610 * if {@code truncationPolicy} specifies 611 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 612 * if an overflow occurs and the policy declares 613 * {@link OverflowMode#CHECKED} 614 * @see #round(int) 615 * @see #round(int, RoundingMode) 616 * @see #scale(int, RoundingMode) 617 */ 618 Decimal<S> round(int precision, TruncationPolicy truncationPolicy); 619 620 /** 621 * Returns a {@code Decimal} value whose {@link #getScale() scale} is 622 * changed to the given {@code scale} value. {@link RoundingMode#HALF_UP 623 * HALF_UP} rounding is used if the scale change involves rounding. 624 * <p> 625 * An exception is thrown if the scale conversion leads to an overflow. 626 * 627 * @param scale 628 * the scale to use for the result, must be in {@code [0,18]} 629 * @return a Decimal instance with the given new scale 630 * @throws IllegalArgumentException 631 * if {@code scale < 0} or {@code scale > 18} 632 * @throws ArithmeticException 633 * if an overflow occurs during the scale conversion 634 * @see #scale(ScaleMetrics) 635 * @see #scale(int, RoundingMode) 636 * @see #round(int) 637 */ 638 Decimal<?> scale(int scale); 639 640 /** 641 * Returns a {@code Decimal} value whose {@link #getScale() scale} is 642 * changed to the scale given by the {@code scaleMetrics} argument. 643 * {@link RoundingMode#HALF_UP HALF_UP} rounding is used if the scale change 644 * involves rounding. 645 * <p> 646 * An exception is thrown if the scale conversion leads to an overflow. 647 * 648 * @param <S> 649 * the scale metrics type of the result 650 * @param scaleMetrics 651 * the scale metrics to use for the result 652 * @return a Decimal instance with the given new scale metrics 653 * @throws ArithmeticException 654 * if an overflow occurs during the scale conversion 655 * @see #scale(int) 656 * @see #scale(ScaleMetrics, RoundingMode) 657 * @see #round(int) 658 */ 659 @SuppressWarnings("hiding") 660 <S extends ScaleMetrics> Decimal<S> scale(S scaleMetrics); 661 662 /** 663 * Returns a {@code Decimal} value whose {@link #getScale() scale} is 664 * changed to the given {@code scale} value. The specified 665 * {@code roundingMode} is used if the scale change involves rounding. 666 * <p> 667 * An exception is thrown if the scale conversion leads to an overflow. 668 * 669 * @param scale 670 * the scale to use for the result, must be in {@code [0,18]} 671 * @param roundingMode 672 * the rounding mode to apply if the scale change involves 673 * rounding 674 * @return a Decimal instance with the given new scale 675 * @throws IllegalArgumentException 676 * if {@code scale < 0} or {@code scale > 18} 677 * @throws ArithmeticException 678 * if {@code roundingMode} is {@link RoundingMode#UNNECESSARY 679 * UNNESSESSARY} and rounding is necessary, or if an overflow 680 * occurs during the scale conversion 681 * @see #scale(int) 682 * @see #scale(ScaleMetrics, RoundingMode) 683 * @see #round(int, RoundingMode) 684 */ 685 Decimal<?> scale(int scale, RoundingMode roundingMode); 686 687 /** 688 * Returns a {@code Decimal} value whose {@link #getScale() scale} is 689 * changed to the scale given by the {@code scaleMetrics} argument. The 690 * specified {@code roundingMode} is used if the scale change involves 691 * rounding. 692 * <p> 693 * An exception is thrown if the scale conversion leads to an overflow. 694 * 695 * @param <S> 696 * the scale metrics type of the result 697 * @param scaleMetrics 698 * the scale metrics to use for the result 699 * @param roundingMode 700 * the rounding mode to apply if the scale change involves 701 * rounding 702 * @return a Decimal instance with the given new scale metrics 703 * @throws ArithmeticException 704 * if {@code roundingMode} is {@link RoundingMode#UNNECESSARY 705 * UNNESSESSARY} and rounding is necessary, or if an overflow 706 * occurs during the scale conversion 707 * @see #scale(ScaleMetrics) 708 * @see #scale(int, RoundingMode) 709 * @see #round(int, RoundingMode) 710 */ 711 @SuppressWarnings("hiding") 712 <S extends ScaleMetrics> Decimal<S> scale(S scaleMetrics, RoundingMode roundingMode); 713 714 // add 715 716 /** 717 * Returns a {@code Decimal} whose value is {@code (this + augend)}. If the 718 * addition causes an overflow, the result is silently truncated. 719 * <p> 720 * The returned value is a new instance if this Decimal is an 721 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 722 * internal state is altered and {@code this} is returned as result now 723 * representing the outcome of the addition. 724 * 725 * @param augend 726 * value to be added to this {@code Decimal} 727 * @return {@code this + augend} 728 */ 729 Decimal<S> add(Decimal<S> augend); 730 731 /** 732 * Returns a {@code Decimal} whose value is {@code (this + augend)}. The 733 * specified {@code overflowMode} determines whether to truncate the result 734 * silently or to throw an exception if an overflow occurs. 735 * <p> 736 * The returned value is a new instance if this Decimal is an 737 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 738 * internal state is altered and {@code this} is returned as result now 739 * representing the outcome of the addition. 740 * 741 * @param augend 742 * value to be added to this {@code Decimal} 743 * @param overflowMode 744 * the overflow mode to apply if the addition leads to an 745 * overflow 746 * @return {@code this + augend} 747 * @throws ArithmeticException 748 * if {@code overflowMode==CHECKED} and an overflow occurs 749 */ 750 Decimal<S> add(Decimal<S> augend, OverflowMode overflowMode); 751 752 /** 753 * Returns a {@code Decimal} whose value is {@code (this + augend)}. The 754 * result of the addition is rounded if necessary using the specified 755 * {@code roundingMode}. Overflows during scale conversion or subtraction 756 * are silently truncated. 757 * <p> 758 * The returned value is a new instance if this Decimal is an 759 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 760 * internal state is altered and {@code this} is returned as result now 761 * representing the outcome of the addition. 762 * 763 * @param augend 764 * value to be added to this {@code Decimal} 765 * @param roundingMode 766 * the rounding mode to apply if rounding is necessary 767 * @return <tt>round<sub>HALF_UP</sub>(this + augend)</tt> 768 * @throws ArithmeticException 769 * if {@code roundingMode==UNNECESSARY} and rounding is 770 * necessary 771 */ 772 Decimal<S> add(Decimal<?> augend, RoundingMode roundingMode); 773 774 /** 775 * Returns a {@code Decimal} whose value is {@code (this + augend)}. The 776 * result of the addition is rounded if necessary using the 777 * {@link RoundingMode} defined by the {@code truncationPolicy} argument. 778 * The {@code truncationPolicy} also defines the {@link OverflowMode} to 779 * apply if the operation causes an overflow. 780 * <p> 781 * The returned value is a new instance if this Decimal is an 782 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 783 * internal state is altered and {@code this} is returned as result now 784 * representing the outcome of the addition. 785 * 786 * @param augend 787 * value to be added to this {@code Decimal} 788 * @param truncationPolicy 789 * the truncation policy specifying {@link RoundingMode} and 790 * {@link OverflowMode} to apply if rounding is necessary or if 791 * an overflow occurs during the addition 792 * @return {@code round(this + augend)} 793 * @throws ArithmeticException 794 * if {@code truncationPolicy} specifies 795 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 796 * if an overflow occurs and the policy declares 797 * {@link OverflowMode#CHECKED} 798 */ 799 Decimal<S> add(Decimal<?> augend, TruncationPolicy truncationPolicy); 800 801 /** 802 * Returns a {@code Decimal} whose value is {@code (this + augend)} after 803 * converting the given {@code long} value to the scale of {@code this} 804 * Decimal. If the operation causes an overflow, the result is silently 805 * truncated. 806 * <p> 807 * The returned value is a new instance if this Decimal is an 808 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 809 * internal state is altered and {@code this} is returned as result now 810 * representing the outcome of the addition. 811 * 812 * @param augend 813 * long value to be added to this {@code Decimal} 814 * @return {@code this + augend} 815 */ 816 Decimal<S> add(long augend); 817 818 /** 819 * Returns a {@code Decimal} whose value is {@code (this + augend)} after 820 * converting the given {@code long} value to the scale of {@code this} 821 * Decimal. The specified {@code overflowMode} determines whether to 822 * truncate the result silently or to throw an exception if an overflow 823 * occurs. 824 * <p> 825 * The returned value is a new instance if this Decimal is an 826 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 827 * internal state is altered and {@code this} is returned as result now 828 * representing the outcome of the addition. 829 * 830 * @param augend 831 * long value to be added to this {@code Decimal} 832 * @param overflowMode 833 * the overflow mode to apply if the addition leads to an 834 * overflow 835 * @return {@code this + augend} 836 * @throws ArithmeticException 837 * if {@code overflowMode==CHECKED} and an overflow occurs 838 */ 839 Decimal<S> add(long augend, OverflowMode overflowMode); 840 841 /** 842 * Returns a {@code Decimal} whose value is {@code (this + augend)} after 843 * converting the given {@code double} argument into a Decimal value of the 844 * same scale as {@code this} Decimal. If rounding is necessary, 845 * {@link RoundingMode#HALF_UP HALF_UP} rounding mode is used and applied 846 * during the conversion step <i>before</i> the addition operation. 847 * Overflows due to conversion or addition result in an exception. 848 * <p> 849 * The returned value is a new instance if this Decimal is an 850 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 851 * internal state is altered and {@code this} is returned as result now 852 * representing the outcome of the addition. 853 * 854 * @param augend 855 * value to be added to this {@code Decimal} 856 * @return <tt>this + round<sub>HALF_UP</sub>(augend)</tt> 857 * @throws IllegalArgumentException 858 * if {@code augend} is NaN or infinite or if the magnitude is 859 * too large for the double to be represented as a 860 * {@code Decimal} 861 * @throws ArithmeticException 862 * if an overflow occurs during the addition operation 863 */ 864 Decimal<S> add(double augend); 865 866 /** 867 * Returns a {@code Decimal} whose value is {@code (this + augend)} after 868 * converting the given {@code double} argument into a Decimal value of the 869 * same scale as {@code this} Decimal. If rounding is necessary, the 870 * specified {@code roundingMode} is used and applied during the conversion 871 * step <i>before</i> the addition operation. Overflows due to conversion or 872 * addition result in an exception. 873 * <p> 874 * The returned value is a new instance if this Decimal is an 875 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 876 * internal state is altered and {@code this} is returned as result now 877 * representing the outcome of the addition. 878 * 879 * @param augend 880 * value to be added to this {@code Decimal} 881 * @param roundingMode 882 * the rounding mode to apply if the augend argument needs to be 883 * rounded when converted into a Decimal number of the same scale 884 * as {@code this} Decimal 885 * @return {@code this + round(augend)} 886 * @throws IllegalArgumentException 887 * if {@code augend} is NaN or infinite or if the magnitude is 888 * too large for the double to be represented as a 889 * {@code Decimal} 890 * @throws ArithmeticException 891 * if {@code roundingMode==UNNECESSARY} and rounding is 892 * necessary or if an overflow occurs during the addition 893 * operation 894 */ 895 Decimal<S> add(double augend, RoundingMode roundingMode); 896 897 /** 898 * Returns a {@code Decimal} whose value is 899 * <tt>(this + unscaledAugend × 10<sup>-scale</sup>)</tt> with the 900 * {@link #getScale() scale} of this Decimal. If the addition causes an 901 * overflow, the result is silently truncated. 902 * <p> 903 * The returned value is a new instance if this Decimal is an 904 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 905 * internal state is altered and {@code this} is returned as result now 906 * representing the outcome of the addition. 907 * 908 * @param unscaledAugend 909 * value to be added to this {@code Decimal} 910 * @return <tt>round<sub>HALF_UP</sub>(this + unscaledAugend × 10<sup>-scale</sup>)</tt> 911 */ 912 Decimal<S> addUnscaled(long unscaledAugend); 913 914 /** 915 * Returns a {@code Decimal} whose value is 916 * <tt>(this + unscaledAugend × 10<sup>-scale</sup>)</tt> with the 917 * {@link #getScale() scale} of this Decimal. The specified 918 * {@code overflowMode} determines whether to truncate the result silently 919 * or to throw an exception if an overflow occurs. 920 * <p> 921 * The returned value is a new instance if this Decimal is an 922 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 923 * internal state is altered and {@code this} is returned as result now 924 * representing the outcome of the addition. 925 * 926 * @param unscaledAugend 927 * value to be added to this {@code Decimal} 928 * @param overflowMode 929 * the overflow mode to apply if the addition leads to an 930 * overflow 931 * @return <tt>round(this + unscaledAugend × 10<sup>-scale</sup>)</tt> 932 * @throws ArithmeticException 933 * if {@code overflowMode==CHECKED} and an overflow occurs 934 */ 935 Decimal<S> addUnscaled(long unscaledAugend, OverflowMode overflowMode); 936 937 /** 938 * Returns a {@code Decimal} whose value is 939 * <tt>(this + unscaledAugend × 10<sup>-scale</sup>)</tt>. The result 940 * of the addition is rounded if necessary using {@link RoundingMode#HALF_UP 941 * HALF_UP} rounding. If the operation causes an overflow, the result is 942 * silently truncated. 943 * <p> 944 * The returned value is a new instance if this Decimal is an 945 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 946 * internal state is altered and {@code this} is returned as result now 947 * representing the outcome of the addition. 948 * 949 * @param unscaledAugend 950 * value to be added to this {@code Decimal} 951 * @param scale 952 * the scale to apply to {@code unscaledAugend}, positive to 953 * indicate the number of fraction digits to the right of the 954 * Decimal point and negative to indicate up-scaling with a power 955 * of ten 956 * @return <tt>round<sub>HALF_UP</sub>(this + unscaledAugend × 10<sup>-scale</sup>)</tt> 957 */ 958 Decimal<S> addUnscaled(long unscaledAugend, int scale); 959 960 /** 961 * Returns a {@code Decimal} whose value is 962 * <tt>(this + unscaledAugend × 10<sup>-scale</sup>)</tt>. The result 963 * of the addition is rounded if necessary using the specified 964 * {@code roundingMode}. If the operation causes an overflow, the result is 965 * silently truncated. 966 * <p> 967 * The returned value is a new instance if this Decimal is an 968 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 969 * internal state is altered and {@code this} is returned as result now 970 * representing the outcome of the addition. 971 * 972 * @param unscaledAugend 973 * value to be added to this {@code Decimal} 974 * @param scale 975 * the scale to apply to {@code unscaledAugend}, positive to 976 * indicate the number of fraction digits to the right of the 977 * Decimal point and negative to indicate up-scaling with a power 978 * of ten 979 * @param roundingMode 980 * the rounding mode to apply if rounding is necessary 981 * @return <tt>round(this + unscaledAugend × 10<sup>-scale</sup>)</tt> 982 * @throws ArithmeticException 983 * if {@code roundingMode==UNNECESSARY} and rounding is 984 * necessary 985 */ 986 Decimal<S> addUnscaled(long unscaledAugend, int scale, RoundingMode roundingMode); 987 988 /** 989 * Returns a {@code Decimal} whose value is 990 * <tt>(this + unscaledAugend × 10<sup>-scale</sup>)</tt>. The result 991 * of the addition is rounded if necessary using the {@link RoundingMode} 992 * defined by the {@code truncationPolicy} argument. The 993 * {@code truncationPolicy} also defines the {@link OverflowMode} to apply 994 * if the operation causes an overflow. 995 * <p> 996 * The returned value is a new instance if this Decimal is an 997 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 998 * internal state is altered and {@code this} is returned as result now 999 * representing the outcome of the addition. 1000 * 1001 * @param unscaledAugend 1002 * value to be added to this {@code Decimal} 1003 * @param scale 1004 * the scale to apply to {@code unscaledAugend}, positive to 1005 * indicate the number of fraction digits to the right of the 1006 * Decimal point and negative to indicate up-scaling with a power 1007 * of ten 1008 * @param truncationPolicy 1009 * the truncation policy specifying {@link RoundingMode} and 1010 * {@link OverflowMode} to apply if rounding is necessary or if 1011 * an overflow occurs during the addition 1012 * @return <tt>round(this + unscaledAugend × 10<sup>-scale</sup>)</tt> 1013 * @throws ArithmeticException 1014 * if {@code truncationPolicy} defines 1015 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1016 * if an overflow occurs and the policy declares 1017 * {@link OverflowMode#CHECKED} 1018 */ 1019 Decimal<S> addUnscaled(long unscaledAugend, int scale, TruncationPolicy truncationPolicy); 1020 1021 /** 1022 * Returns a {@code Decimal} whose value is 1023 * <tt>(this + value<sup>2</sup>)</tt>. The squared value is rounded 1024 * <i>before</i> the addition if necessary using default 1025 * {@link RoundingMode#HALF_UP HALF_UP} rounding. Overflows during squaring 1026 * or addition are silently truncated. 1027 * <p> 1028 * The returned value is a new instance if this Decimal is an 1029 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1030 * internal state is altered and {@code this} is returned as result now 1031 * representing the outcome of the addition. 1032 * 1033 * @param value 1034 * value to be squared and added to this {@code Decimal} 1035 * @return <tt>this + round<sub>HALF_UP</sub>(value*value)</tt> 1036 */ 1037 Decimal<S> addSquared(Decimal<S> value); 1038 1039 /** 1040 * Returns a {@code Decimal} whose value is 1041 * <tt>(this + value<sup>2</sup>)</tt>. The squared value is rounded 1042 * <i>before</i> the addition if necessary using the specified 1043 * {@code roundingMode}. Overflows during squaring or addition are silently 1044 * truncated. 1045 * <p> 1046 * The returned value is a new instance if this Decimal is an 1047 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1048 * internal state is altered and {@code this} is returned as result now 1049 * representing the outcome of the addition. 1050 * 1051 * @param value 1052 * value to be squared and added to this {@code Decimal} 1053 * @param roundingMode 1054 * the rounding mode to apply if necessary when squaring the 1055 * value 1056 * @return <tt>this + round(value*value)</tt> 1057 * @throws ArithmeticException 1058 * if {@code roundingMode==UNNECESSARY} and rounding is 1059 * necessary 1060 */ 1061 Decimal<S> addSquared(Decimal<S> value, RoundingMode roundingMode); 1062 1063 /** 1064 * Returns a {@code Decimal} whose value is 1065 * <tt>(this + value<sup>2</sup>)</tt>. The squared value is rounded 1066 * <i>before</i> the addition if necessary using the {@link RoundingMode} 1067 * specified by the {@code truncationPolicy} argument. The 1068 * {@code truncationPolicy} also defines the {@link OverflowMode} to apply 1069 * if an overflow occurs during square or add operation. 1070 * <p> 1071 * The returned value is a new instance if this Decimal is an 1072 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1073 * internal state is altered and {@code this} is returned as result now 1074 * representing the outcome of the addition. 1075 * 1076 * @param value 1077 * value to be squared and added to this {@code Decimal} 1078 * @param truncationPolicy 1079 * the truncation policy specifying {@link RoundingMode} and 1080 * {@link OverflowMode} to apply if rounding is necessary when 1081 * squaring the value or if an overflow occurs during the square 1082 * or add operation 1083 * @return <tt>this + round(value*value)</tt> 1084 * @throws ArithmeticException 1085 * if {@code truncationPolicy} defines 1086 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1087 * if an overflow occurs and the policy declares 1088 * {@link OverflowMode#CHECKED} 1089 */ 1090 Decimal<S> addSquared(Decimal<S> value, TruncationPolicy truncationPolicy); 1091 1092 // subtract 1093 1094 /** 1095 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)}. If 1096 * the subtraction causes an overflow, the result is silently truncated. 1097 * <p> 1098 * The returned value is a new instance if this Decimal is an 1099 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1100 * internal state is altered and {@code this} is returned as result now 1101 * representing the outcome of the subtraction. 1102 * 1103 * @param subtrahend 1104 * value to be subtracted from this {@code Decimal} 1105 * @return {@code this - subtrahend} 1106 */ 1107 Decimal<S> subtract(Decimal<S> subtrahend); 1108 1109 /** 1110 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)}. The 1111 * specified {@code overflowMode} determines whether to truncate the result 1112 * silently or to throw an exception if an overflow occurs. 1113 * <p> 1114 * The returned value is a new instance if this Decimal is an 1115 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1116 * internal state is altered and {@code this} is returned as result now 1117 * representing the outcome of the subtraction. 1118 * 1119 * @param subtrahend 1120 * value to be subtracted from this {@code Decimal} 1121 * @param overflowMode 1122 * the overflow mode to apply if the subtraction leads to an 1123 * overflow 1124 * @return {@code this - subtrahend} 1125 * @throws ArithmeticException 1126 * if {@code overflowMode==CHECKED} and an overflow occurs 1127 */ 1128 Decimal<S> subtract(Decimal<S> subtrahend, OverflowMode overflowMode); 1129 1130 /** 1131 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)} 1132 * after converting the given {@code subtrahend} argument to the scale of 1133 * {@code this} Decimal. The result of the subtraction is rounded if 1134 * necessary using the specified {@code roundingMode}. Overflows during 1135 * scale conversion or subtraction are silently truncated. 1136 * <p> 1137 * The returned value is a new instance if this Decimal is an 1138 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1139 * internal state is altered and {@code this} is returned as result now 1140 * representing the outcome of the subtraction. 1141 * 1142 * @param subtrahend 1143 * value to be subtracted from this {@code Decimal} 1144 * @param roundingMode 1145 * the rounding mode to apply if rounding is necessary 1146 * @return <tt>round<sub>HALF_UP</sub>(this - subtrahend)</tt> 1147 * @throws ArithmeticException 1148 * if {@code roundingMode==UNNECESSARY} and rounding is 1149 * necessary 1150 */ 1151 Decimal<S> subtract(Decimal<?> subtrahend, RoundingMode roundingMode); 1152 1153 /** 1154 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)} 1155 * after converting the given {@code subtrahend} argument to the scale of 1156 * {@code this} Decimal. The result of the subtraction is rounded if 1157 * necessary using the {@link RoundingMode} defined by the 1158 * {@code truncationPolicy} argument. The {@code truncationPolicy} also 1159 * defines the {@link OverflowMode} to apply if the operation causes an 1160 * overflow. 1161 * <p> 1162 * The returned value is a new instance if this Decimal is an 1163 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1164 * internal state is altered and {@code this} is returned as result now 1165 * representing the outcome of the subtraction. 1166 * 1167 * @param subtrahend 1168 * value to be subtracted from this {@code Decimal} 1169 * @param truncationPolicy 1170 * the truncation policy specifying {@link RoundingMode} and 1171 * {@link OverflowMode} to apply if rounding is necessary or if 1172 * an overflow occurs during the subtraction 1173 * @return <tt>round(this - subtrahend)</tt> 1174 * @throws ArithmeticException 1175 * if {@code truncationPolicy} specifies 1176 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1177 * if an overflow occurs and the policy declares 1178 * {@link OverflowMode#CHECKED} 1179 */ 1180 Decimal<S> subtract(Decimal<?> subtrahend, TruncationPolicy truncationPolicy); 1181 1182 /** 1183 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)}. If 1184 * the subtraction causes an overflow, the result is silently truncated. 1185 * <p> 1186 * The returned value is a new instance if this Decimal is an 1187 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1188 * internal state is altered and {@code this} is returned as result now 1189 * representing the outcome of the subtraction. 1190 * 1191 * @param subtrahend 1192 * long value to be subtracted from this {@code Decimal} 1193 * @return {@code this - subtrahend} 1194 */ 1195 Decimal<S> subtract(long subtrahend); 1196 1197 /** 1198 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)}. The 1199 * specified {@code overflowMode} determines whether to truncate the result 1200 * silently or to throw an exception if an overflow occurs. 1201 * <p> 1202 * The returned value is a new instance if this Decimal is an 1203 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1204 * internal state is altered and {@code this} is returned as result now 1205 * representing the outcome of the subtraction. 1206 * 1207 * @param subtrahend 1208 * long value to be subtracted from this {@code Decimal} 1209 * @param overflowMode 1210 * the overflow mode to apply if the subtraction leads to an 1211 * overflow 1212 * @return {@code this - subtrahend} 1213 * @throws ArithmeticException 1214 * if {@code overflowMode==CHECKED} and an overflow occurs 1215 */ 1216 Decimal<S> subtract(long subtrahend, OverflowMode overflowMode); 1217 1218 /** 1219 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)} 1220 * after converting the given {@code double} argument into a Decimal value 1221 * of the same scale as {@code this} Decimal. If rounding is necessary, 1222 * {@link RoundingMode#HALF_UP HALF_UP} rounding mode is used and applied 1223 * during the conversion step <i>before</i> the subtraction operation. 1224 * Overflows due to conversion or subtraction result in an exception. 1225 * <p> 1226 * The returned value is a new instance if this Decimal is an 1227 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1228 * internal state is altered and {@code this} is returned as result now 1229 * representing the outcome of the subtraction. 1230 * 1231 * @param subtrahend 1232 * value to be subtracted from this {@code Decimal} 1233 * @return <tt>this - round<sub>HALF_UP</sub>(subtrahend)</tt> 1234 * @throws IllegalArgumentException 1235 * if {@code subtrahend} is NaN or infinite or if the magnitude 1236 * is too large for the double to be represented as a 1237 * {@code Decimal} 1238 * @throws ArithmeticException 1239 * if an overflow occurs during the subtraction operation 1240 */ 1241 Decimal<S> subtract(double subtrahend); 1242 1243 /** 1244 * Returns a {@code Decimal} whose value is {@code (this - subtrahend)} 1245 * after converting the given {@code double} argument into a Decimal value 1246 * of the same scale as {@code this} Decimal. If rounding is necessary, the 1247 * specified {@code roundingMode} is used and applied during the conversion 1248 * step <i>before</i> the subtraction operation. Overflows due to conversion 1249 * or subtraction result in an exception. 1250 * <p> 1251 * The returned value is a new instance if this Decimal is an 1252 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1253 * internal state is altered and {@code this} is returned as result now 1254 * representing the outcome of the subtraction. 1255 * 1256 * @param subtrahend 1257 * value to be subtracted from this {@code Decimal} 1258 * @param roundingMode 1259 * the rounding mode to apply if the subtrahend argument needs to 1260 * be rounded when converted into a Decimal number of the same 1261 * scale as {@code this} Decimal 1262 * @return <tt>this - round(subtrahend)</tt> 1263 * @throws IllegalArgumentException 1264 * if {@code subtrahend} is NaN or infinite or if the magnitude 1265 * is too large for the double to be represented as a 1266 * {@code Decimal} 1267 * @throws ArithmeticException 1268 * if {@code roundingMode==UNNECESSARY} and rounding is 1269 * necessary or if an overflow occurs during the subtraction 1270 * operation 1271 */ 1272 Decimal<S> subtract(double subtrahend, RoundingMode roundingMode); 1273 1274 /** 1275 * Returns a {@code Decimal} whose value is 1276 * <tt>(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt> with the 1277 * {@link #getScale() scale} of this Decimal. If the subtraction causes an 1278 * overflow, the result is silently truncated. 1279 * <p> 1280 * The returned value is a new instance if this Decimal is an 1281 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1282 * internal state is altered and {@code this} is returned as result now 1283 * representing the outcome of the subtraction. 1284 * 1285 * @param unscaledSubtrahend 1286 * value to be subtracted from this {@code Decimal} 1287 * @return <tt>this - unscaledSubtrahend × 10<sup>-scale</sup></tt> 1288 */ 1289 Decimal<S> subtractUnscaled(long unscaledSubtrahend); 1290 1291 /** 1292 * Returns a {@code Decimal} whose value is 1293 * <tt>(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt> with the 1294 * {@link #getScale() scale} of this Decimal. The specified 1295 * {@code overflowMode} determines whether to truncate the result silently 1296 * or to throw an exception if an overflow occurs. 1297 * <p> 1298 * The returned value is a new instance if this Decimal is an 1299 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1300 * internal state is altered and {@code this} is returned as result now 1301 * representing the outcome of the subtraction. 1302 * 1303 * @param unscaledSubtrahend 1304 * value to be subtracted from this {@code Decimal} 1305 * @param overflowMode 1306 * the overflow mode to apply if the subtraction leads to an 1307 * overflow 1308 * @return <tt>this - unscaledSubtrahend × 10<sup>-scale</sup></tt> 1309 * @throws ArithmeticException 1310 * if {@code overflowMode==CHECKED} and an overflow occurs 1311 */ 1312 Decimal<S> subtractUnscaled(long unscaledSubtrahend, OverflowMode overflowMode); 1313 1314 /** 1315 * Returns a {@code Decimal} whose value is 1316 * <tt>(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt>. The 1317 * result of the subtraction is rounded if necessary using 1318 * {@link RoundingMode#HALF_UP HALF_UP} rounding. If the operation causes an 1319 * overflow, the result is silently truncated. 1320 * <p> 1321 * The returned value is a new instance if this Decimal is an 1322 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1323 * internal state is altered and {@code this} is returned as result now 1324 * representing the outcome of the subtraction. 1325 * 1326 * @param unscaledSubtrahend 1327 * value to be subtracted from this {@code Decimal} 1328 * @param scale 1329 * the scale to apply to {@code unscaledSubtrahend}, positive to 1330 * indicate the number of fraction digits to the right of the 1331 * Decimal point and negative to indicate up-scaling with a power 1332 * of ten 1333 * @return <tt>round<sub>HALF_UP</sub>(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt> 1334 */ 1335 Decimal<S> subtractUnscaled(long unscaledSubtrahend, int scale); 1336 1337 /** 1338 * Returns a {@code Decimal} whose value is 1339 * <tt>(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt>. The 1340 * result of the subtraction is rounded if necessary using the specified 1341 * {@code roundingMode}. If the operation causes an overflow, the result is 1342 * silently truncated. 1343 * <p> 1344 * The returned value is a new instance if this Decimal is an 1345 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1346 * internal state is altered and {@code this} is returned as result now 1347 * representing the outcome of the subtraction. 1348 * 1349 * @param unscaledSubtrahend 1350 * value to be subtracted from this {@code Decimal} 1351 * @param scale 1352 * the scale to apply to {@code unscaledSubtrahend}, positive to 1353 * indicate the number of fraction digits to the right of the 1354 * Decimal point and negative to indicate up-scaling with a power 1355 * of ten 1356 * @param roundingMode 1357 * the rounding mode to apply if rounding is necessary 1358 * @return <tt>round(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt> 1359 * @throws ArithmeticException 1360 * if {@code roundingMode==UNNECESSARY} and rounding is 1361 * necessary 1362 */ 1363 Decimal<S> subtractUnscaled(long unscaledSubtrahend, int scale, RoundingMode roundingMode); 1364 1365 /** 1366 * Returns a {@code Decimal} whose value is 1367 * <tt>(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt>. The 1368 * result of the subtraction is rounded if necessary using the 1369 * {@link RoundingMode} defined by the {@code truncationPolicy} argument. 1370 * The {@code truncationPolicy} also defines the {@link OverflowMode} to 1371 * apply if the operation causes an overflow. 1372 * <p> 1373 * The returned value is a new instance if this Decimal is an 1374 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1375 * internal state is altered and {@code this} is returned as result now 1376 * representing the outcome of the subtraction. 1377 * 1378 * @param unscaledSubtrahend 1379 * value to be subtracted from this {@code Decimal} 1380 * @param scale 1381 * the scale to apply to {@code unscaledSubtrahend}, positive to 1382 * indicate the number of fraction digits to the right of the 1383 * Decimal point and negative to indicate up-scaling with a power 1384 * of ten 1385 * @param truncationPolicy 1386 * the truncation policy specifying {@link RoundingMode} and 1387 * {@link OverflowMode} to apply if rounding is necessary or if 1388 * an overflow occurs during the subtraction 1389 * @return <tt>round(this - unscaledSubtrahend × 10<sup>-scale</sup>)</tt> 1390 * @throws ArithmeticException 1391 * if {@code truncationPolicy} defines 1392 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1393 * if an overflow occurs and the policy declares 1394 * {@link OverflowMode#CHECKED} 1395 */ 1396 Decimal<S> subtractUnscaled(long unscaledSubtrahend, int scale, TruncationPolicy truncationPolicy); 1397 1398 /** 1399 * Returns a {@code Decimal} whose value is 1400 * <tt>(this - value<sup>2</sup>)</tt>. The squared value is rounded 1401 * <i>before</i> the subtraction if necessary using default 1402 * {@link RoundingMode#HALF_UP HALF_UP} rounding. Overflows during squaring 1403 * or subtraction are silently truncated. 1404 * <p> 1405 * The returned value is a new instance if this Decimal is an 1406 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1407 * internal state is altered and {@code this} is returned as result now 1408 * representing the outcome of the subtraction. 1409 * 1410 * @param value 1411 * value to be squared and subtracted from this {@code Decimal} 1412 * @return <tt>this - round<sub>HALF_UP</sub>(value*value)</tt> 1413 */ 1414 Decimal<S> subtractSquared(Decimal<S> value); 1415 1416 /** 1417 * Returns a {@code Decimal} whose value is 1418 * <tt>(this - value<sup>2</sup>)</tt>. The squared value is rounded 1419 * <i>before</i> the subtraction if necessary using the specified 1420 * {@code roundingMode}. Overflows during squaring or subtraction are 1421 * silently truncated. 1422 * <p> 1423 * The returned value is a new instance if this Decimal is an 1424 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1425 * internal state is altered and {@code this} is returned as result now 1426 * representing the outcome of the subtraction. 1427 * 1428 * @param value 1429 * value to be squared and subtracted from this {@code Decimal} 1430 * @param roundingMode 1431 * the rounding mode to apply if necessary when squaring the 1432 * value 1433 * @return <tt>this - round(value*value)</tt> 1434 * @throws ArithmeticException 1435 * if {@code roundingMode==UNNECESSARY} and rounding is 1436 * necessary 1437 */ 1438 Decimal<S> subtractSquared(Decimal<S> value, RoundingMode roundingMode); 1439 1440 /** 1441 * Returns a {@code Decimal} whose value is 1442 * <tt>(this - value<sup>2</sup>)</tt>. The squared value is rounded 1443 * <i>before</i> the subtraction if necessary using the {@link RoundingMode} 1444 * specified by the {@code truncationPolicy} argument. The 1445 * {@code truncationPolicy} also defines the {@link OverflowMode} to apply 1446 * if an overflow occurs during square or subtract operation. 1447 * <p> 1448 * The returned value is a new instance if this Decimal is an 1449 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1450 * internal state is altered and {@code this} is returned as result now 1451 * representing the outcome of the subtraction. 1452 * 1453 * @param value 1454 * value to be squared and subtracted from this {@code Decimal} 1455 * @param truncationPolicy 1456 * the truncation policy specifying {@link RoundingMode} and 1457 * {@link OverflowMode} to apply if rounding is necessary when 1458 * squaring the value or if an overflow occurs during the square 1459 * or subtract operation 1460 * @return <tt>this - round(value*value)</tt> 1461 * @throws ArithmeticException 1462 * if {@code truncationPolicy} defines 1463 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1464 * if an overflow occurs and the policy declares 1465 * {@link OverflowMode#CHECKED} 1466 */ 1467 Decimal<S> subtractSquared(Decimal<S> value, TruncationPolicy truncationPolicy); 1468 1469 // multiply 1470 1471 /** 1472 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1473 * The result is rounded to the {@link #getScale() scale} of this Decimal 1474 * using default {@link RoundingMode#HALF_UP HALF_UP} rounding. If the 1475 * multiplication causes an overflow, the result is silently truncated. 1476 * <p> 1477 * The returned value is a new instance if this Decimal is an 1478 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1479 * internal state is altered and {@code this} is returned as result now 1480 * representing the outcome of the multiplication. 1481 * 1482 * @param multiplicand 1483 * factor to multiply with this {@code Decimal} 1484 * @return <tt>round<sub>HALF_UP</sub>(this * multiplicand)</tt> 1485 */ 1486 Decimal<S> multiply(Decimal<S> multiplicand); 1487 1488 /** 1489 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1490 * The result is rounded to the {@link #getScale() scale} of this Decimal 1491 * using the specified {@code roundingMode}. If the multiplication causes an 1492 * overflow, the result is silently truncated. 1493 * <p> 1494 * The returned value is a new instance if this Decimal is an 1495 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1496 * internal state is altered and {@code this} is returned as result now 1497 * representing the outcome of the multiplication. 1498 * 1499 * @param multiplicand 1500 * factor to multiply with this {@code Decimal} 1501 * @param roundingMode 1502 * the rounding mode to apply if the result needs to be rounded 1503 * @return <tt>round(this * multiplicand)</tt> 1504 * @throws ArithmeticException 1505 * if {@code roundingMode==UNNECESSARY} and rounding is 1506 * necessary 1507 */ 1508 Decimal<S> multiply(Decimal<S> multiplicand, RoundingMode roundingMode); 1509 1510 /** 1511 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1512 * The result is rounded to the {@link #getScale() scale} of this Decimal 1513 * using the {@link RoundingMode} specified by the {@code truncationPolicy} 1514 * argument. The {@code truncationPolicy} also defines the 1515 * {@link OverflowMode} to apply if an overflow occurs during the multiply 1516 * operation. 1517 * <p> 1518 * The returned value is a new instance if this Decimal is an 1519 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1520 * internal state is altered and {@code this} is returned as result now 1521 * representing the outcome of the multiplication. 1522 * 1523 * @param multiplicand 1524 * factor to multiply with this {@code Decimal} 1525 * @param truncationPolicy 1526 * the truncation policy specifying {@link RoundingMode} and 1527 * {@link OverflowMode} to apply if rounding is necessary or if 1528 * an overflow occurs 1529 * @return <tt>round(this * multiplicand)</tt> 1530 * @throws ArithmeticException 1531 * if {@code truncationPolicy} defines 1532 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1533 * if an overflow occurs and the policy declares 1534 * {@link OverflowMode#CHECKED} 1535 */ 1536 Decimal<S> multiply(Decimal<S> multiplicand, TruncationPolicy truncationPolicy); 1537 1538 /** 1539 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1540 * The result is rounded to the {@link #getScale() scale} of this Decimal 1541 * using {@link RoundingMode#HALF_UP HALF_UP} rounding. If the 1542 * multiplication causes an overflow, the result is silently truncated. 1543 * <p> 1544 * The returned value is a new instance if this Decimal is an 1545 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1546 * internal state is altered and {@code this} is returned as result now 1547 * representing the outcome of the multiplication. 1548 * 1549 * @param multiplicand 1550 * factor to multiply with this {@code Decimal} 1551 * @return <tt>round<sub>HALF_UP</sub>(this * multiplicand)</tt> 1552 */ 1553 Decimal<S> multiplyBy(Decimal<?> multiplicand); 1554 1555 /** 1556 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1557 * The result is rounded to the {@link #getScale() scale} of this Decimal 1558 * using the specified {@code roundingMode}. If the multiplication causes an 1559 * overflow, the result is silently truncated. 1560 * <p> 1561 * The returned value is a new instance if this Decimal is an 1562 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1563 * internal state is altered and {@code this} is returned as result now 1564 * representing the outcome of the multiplication. 1565 * 1566 * @param multiplicand 1567 * factor to multiply with this {@code Decimal} 1568 * @param roundingMode 1569 * the rounding mode to apply if rounding is necessary 1570 * @return <tt>round(this * multiplicand)</tt> 1571 * @throws ArithmeticException 1572 * if {@code roundingMode==UNNECESSARY} and rounding is 1573 * necessary 1574 */ 1575 Decimal<S> multiplyBy(Decimal<?> multiplicand, RoundingMode roundingMode); 1576 1577 /** 1578 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1579 * The result is rounded to the {@link #getScale() scale} of this Decimal 1580 * using the {@link RoundingMode} defined by the {@code truncationPolicy} 1581 * argument. The {@code truncationPolicy} also defines the 1582 * {@link OverflowMode} to apply if the operation causes an overflow. 1583 * <p> 1584 * The returned value is a new instance if this Decimal is an 1585 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1586 * internal state is altered and {@code this} is returned as result now 1587 * representing the outcome of the multiplication. 1588 * 1589 * @param multiplicand 1590 * factor to multiply with this {@code Decimal} 1591 * @param truncationPolicy 1592 * the truncation policy specifying {@link RoundingMode} and 1593 * {@link OverflowMode} to apply if rounding is necessary or if 1594 * an overflow occurs 1595 * @return <tt>round(this * multiplicand)</tt> 1596 * @throws ArithmeticException 1597 * if {@code truncationPolicy} defines 1598 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1599 * if an overflow occurs and the policy declares 1600 * {@link OverflowMode#CHECKED} 1601 */ 1602 Decimal<S> multiplyBy(Decimal<?> multiplicand, TruncationPolicy truncationPolicy); 1603 1604 /** 1605 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1606 * The scale of the returned value is the sum of the scales of {@code this} 1607 * Decimal and the {@code multiplicand} argument. If the result scale 1608 * exceeds 18, an {@link IllegalArgumentException} is thrown. An 1609 * {@link ArithmeticException} is thrown if the product is out of the 1610 * possible range for a Decimal with the result scale. 1611 * <p> 1612 * Note that the result is <i>always</i> a new instance --- immutable if 1613 * this Decimal is an {@link ImmutableDecimal} and mutable if it is a 1614 * {@link MutableDecimal}. 1615 * 1616 * @param multiplicand 1617 * factor to multiply with this {@code Decimal} 1618 * @return {@code (this * multiplicand)} with scale equal to the sum of 1619 * scales of {@code this} and {@code multiplicand} 1620 * @throws IllegalArgumentException 1621 * if the sum of the scales of {@code this} Decimal and the 1622 * {@code multiplicand} argument exceeds 18 1623 * @throws ArithmeticException 1624 * if an overflow occurs and product is out of the possible 1625 * range for a Decimal with the result scale 1626 */ 1627 Decimal<?> multiplyExact(Decimal<?> multiplicand); 1628 1629 /** 1630 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1631 * If the multiplication causes an overflow, the result is silently 1632 * truncated. 1633 * <p> 1634 * This method performs multiplication of this {@code Decimal} with a 1635 * {@code long} value which is usually more efficient than multiplication of 1636 * two Decimal values. 1637 * <p> 1638 * The returned value is a new instance if this Decimal is an 1639 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1640 * internal state is altered and {@code this} is returned as result now 1641 * representing the outcome of the multiplication. 1642 * 1643 * @param multiplicand 1644 * factor to multiply with this {@code Decimal} 1645 * @return {@code (this * multiplicand)} 1646 */ 1647 Decimal<S> multiply(long multiplicand); 1648 1649 /** 1650 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)}. 1651 * The specified {@code overflowMode} determines whether to truncate the 1652 * result silently or to throw an exception if an overflow occurs. 1653 * <p> 1654 * This method performs multiplication of this {@code Decimal} with a 1655 * {@code long} value which is usually more efficient than multiplication of 1656 * two Decimal values. 1657 * <p> 1658 * The returned value is a new instance if this Decimal is an 1659 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1660 * internal state is altered and {@code this} is returned as result now 1661 * representing the outcome of the multiplication. 1662 * 1663 * @param multiplicand 1664 * factor to multiply with this {@code Decimal} 1665 * @param overflowMode 1666 * the overflow mode to apply if the multiplication leads to an 1667 * overflow 1668 * @return {@code (this * multiplicand)} 1669 * @throws ArithmeticException 1670 * if {@code overflowMode==CHECKED} and an overflow occurs 1671 */ 1672 Decimal<S> multiply(long multiplicand, OverflowMode overflowMode); 1673 1674 /** 1675 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)} 1676 * after converting the given {@code double} argument into a Decimal value 1677 * of the same scale as {@code this} Decimal. {@link RoundingMode#HALF_UP 1678 * HALF_UP} rounding mode is used if necessary and applied twice during the 1679 * conversion step <i>before</i> the multiplication and again when rounding 1680 * the product to the {@link #getScale() scale} of this Decimal. Overflows 1681 * due to conversion or multiplication result in an exception. 1682 * <p> 1683 * The returned value is a new instance if this Decimal is an 1684 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1685 * internal state is altered and {@code this} is returned as result now 1686 * representing the outcome of the multiplication. 1687 * 1688 * @param multiplicand 1689 * factor to multiply with this {@code Decimal} 1690 * @return <tt>round<sub>HALF_UP</sub>(this * round<sub>HALF_UP</sub>(multiplicand))</tt> 1691 * @throws IllegalArgumentException 1692 * if {@code multiplicand} is NaN or infinite or if the 1693 * magnitude is too large for the double to be represented as a 1694 * {@code Decimal} 1695 * @throws ArithmeticException 1696 * if an overflow occurs during the multiply operation 1697 */ 1698 Decimal<S> multiply(double multiplicand); 1699 1700 /** 1701 * Returns a {@code Decimal} whose value is {@code (this * multiplicand)} 1702 * after converting the given {@code double} argument into a Decimal value 1703 * of the same scale as {@code this} Decimal. Rounding, if necessary, uses 1704 * the specified {@code roundingMode} and is applied during the conversion 1705 * step <i>before</i> the multiplication and again when rounding the product 1706 * to the {@link #getScale() scale} of this Decimal. Overflows due to 1707 * conversion or multiplication result in an exception. 1708 * <p> 1709 * The returned value is a new instance if this Decimal is an 1710 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1711 * internal state is altered and {@code this} is returned as result now 1712 * representing the outcome of the multiplication. 1713 * 1714 * @param multiplicand 1715 * factor to multiply with this {@code Decimal} 1716 * @param roundingMode 1717 * the rounding mode to apply if the converted multiplicand or 1718 * the resulting product needs to be rounded 1719 * @return {@code round(this * round(multiplicand))} 1720 * @throws IllegalArgumentException 1721 * if {@code multiplicand} is NaN or infinite or if the 1722 * magnitude is too large for the double to be represented as a 1723 * {@code Decimal} 1724 * @throws ArithmeticException 1725 * if {@code roundingMode==UNNECESSARY} and rounding is 1726 * necessary or if an overflow occurs during the multiply 1727 * operation 1728 */ 1729 Decimal<S> multiply(double multiplicand, RoundingMode roundingMode); 1730 1731 /** 1732 * Returns a {@code Decimal} whose value is 1733 * <tt>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> with 1734 * the {@link #getScale() scale} of this Decimal. The result is rounded to 1735 * the scale of this Decimal using default {@link RoundingMode#HALF_UP 1736 * HALF_UP} rounding. If the multiplication causes an overflow, the result 1737 * is silently truncated. 1738 * <p> 1739 * The returned value is a new instance if this Decimal is an 1740 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1741 * internal state is altered and {@code this} is returned as result now 1742 * representing the outcome of the multiplication. 1743 * 1744 * @param unscaledMultiplicand 1745 * factor to multiply with this {@code Decimal} 1746 * @return <tt>round<sub>HALF_UP</sub>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> 1747 */ 1748 Decimal<S> multiplyUnscaled(long unscaledMultiplicand); 1749 1750 /** 1751 * Returns a {@code Decimal} whose value is 1752 * <tt>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> with 1753 * the {@link #getScale() scale} of this Decimal. The result is rounded to 1754 * the scale of this Decimal using the specified {@code roundingMode}. If 1755 * the multiplication causes an overflow, the result is silently truncated. 1756 * <p> 1757 * The returned value is a new instance if this Decimal is an 1758 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1759 * internal state is altered and {@code this} is returned as result now 1760 * representing the outcome of the multiplication. 1761 * 1762 * @param unscaledMultiplicand 1763 * factor to multiply with this {@code Decimal} 1764 * @param roundingMode 1765 * the rounding mode to apply if the result needs to be rounded 1766 * @return <tt>round(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> 1767 * @throws ArithmeticException 1768 * if {@code roundingMode==UNNECESSARY} and rounding is 1769 * necessary 1770 */ 1771 Decimal<S> multiplyUnscaled(long unscaledMultiplicand, RoundingMode roundingMode); 1772 1773 /** 1774 * Returns a {@code Decimal} whose value is 1775 * <tt>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> with 1776 * the {@link #getScale() scale} of this Decimal. The result is rounded to 1777 * the scale of this Decimal using the using the {@link RoundingMode} 1778 * specified by the {@code truncationPolicy} argument. The 1779 * {@code truncationPolicy} also defines the {@link OverflowMode} to apply 1780 * if an overflow occurs during the multiplication. 1781 * <p> 1782 * The returned value is a new instance if this Decimal is an 1783 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1784 * internal state is altered and {@code this} is returned as result now 1785 * representing the outcome of the multiplication. 1786 * 1787 * @param unscaledMultiplicand 1788 * factor to multiply with this {@code Decimal} 1789 * @param truncationPolicy 1790 * the truncation policy specifying {@link RoundingMode} and 1791 * {@link OverflowMode} to apply if rounding is necessary or if 1792 * an overflow occurs 1793 * @return <tt>round(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> 1794 * @throws ArithmeticException 1795 * if {@code truncationPolicy} defines 1796 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1797 * if an overflow occurs and the policy declares 1798 * {@link OverflowMode#CHECKED} 1799 */ 1800 Decimal<S> multiplyUnscaled(long unscaledMultiplicand, TruncationPolicy truncationPolicy); 1801 1802 /** 1803 * Returns a {@code Decimal} whose value is 1804 * <tt>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt>. The 1805 * result of the multiplication is rounded to the {@link #getScale() scale} 1806 * of this Decimal using {@link RoundingMode#HALF_UP HALF_UP} rounding. If 1807 * the operation causes an overflow, the result is silently truncated. 1808 * <p> 1809 * The returned value is a new instance if this Decimal is an 1810 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1811 * internal state is altered and {@code this} is returned as result now 1812 * representing the outcome of the multiplication. 1813 * 1814 * @param unscaledMultiplicand 1815 * factor to multiply with this {@code Decimal} 1816 * @param scale 1817 * the scale to apply to {@code unscaledMultiplicand}, positive 1818 * to indicate the number of fraction digits to the right of the 1819 * Decimal point and negative to indicate up-scaling with a power 1820 * of ten 1821 * @return <tt>round<sub>HALF_UP</sub>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> 1822 */ 1823 Decimal<S> multiplyUnscaled(long unscaledMultiplicand, int scale); 1824 1825 /** 1826 * Returns a {@code Decimal} whose value is 1827 * <tt>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt>. The 1828 * result of the multiplication is rounded to the {@link #getScale() scale} 1829 * of this Decimal using the specified {@code roundingMode}. If the 1830 * operation causes an overflow, the result is silently truncated. 1831 * <p> 1832 * The returned value is a new instance if this Decimal is an 1833 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1834 * internal state is altered and {@code this} is returned as result now 1835 * representing the outcome of the multiplication. 1836 * 1837 * @param unscaledMultiplicand 1838 * factor to multiply with this {@code Decimal} 1839 * @param scale 1840 * the scale to apply to {@code unscaledMultiplicand}, positive 1841 * to indicate the number of fraction digits to the right of the 1842 * Decimal point and negative to indicate up-scaling with a power 1843 * of ten 1844 * @param roundingMode 1845 * the rounding mode to apply if rounding is necessary 1846 * @return <tt>round(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> 1847 * @throws ArithmeticException 1848 * if {@code roundingMode==UNNECESSARY} and rounding is 1849 * necessary 1850 */ 1851 Decimal<S> multiplyUnscaled(long unscaledMultiplicand, int scale, RoundingMode roundingMode); 1852 1853 /** 1854 * Returns a {@code Decimal} whose value is 1855 * <tt>(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt>. The 1856 * result of the multiplication is rounded to the {@link #getScale() scale} 1857 * of this Decimal using the {@link RoundingMode} defined by the 1858 * {@code truncationPolicy} argument. The {@code truncationPolicy} also 1859 * defines the {@link OverflowMode} to apply if the operation causes an 1860 * overflow. 1861 * <p> 1862 * The returned value is a new instance if this Decimal is an 1863 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1864 * internal state is altered and {@code this} is returned as result now 1865 * representing the outcome of the multiplication. 1866 * 1867 * @param unscaledMultiplicand 1868 * factor to multiply with this {@code Decimal} 1869 * @param scale 1870 * the scale to apply to {@code unscaledMultiplicand}, positive 1871 * to indicate the number of fraction digits to the right of the 1872 * Decimal point and negative to indicate up-scaling with a power 1873 * of ten 1874 * @param truncationPolicy 1875 * the truncation policy specifying {@link RoundingMode} and 1876 * {@link OverflowMode} to apply if rounding is necessary or if 1877 * an overflow occurs 1878 * @return <tt>round(this * unscaledMultiplicand × 10<sup>-scale</sup>)</tt> 1879 * @throws ArithmeticException 1880 * if {@code truncationPolicy} defines 1881 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 1882 * if an overflow occurs and the policy declares 1883 * {@link OverflowMode#CHECKED} 1884 */ 1885 Decimal<S> multiplyUnscaled(long unscaledMultiplicand, int scale, TruncationPolicy truncationPolicy); 1886 1887 /** 1888 * Returns a {@code Decimal} whose value is <tt>(this * 10<sup>n</sup>)</tt> 1889 * . For negative <tt>n</tt> the multiplication turns into a de-facto 1890 * division and the result is rounded to the {@link #getScale() scale} of 1891 * this Decimal using default {@link RoundingMode#HALF_UP HALF_UP} rounding. 1892 * If the multiplication causes an overflow, the result is silently 1893 * truncated. 1894 * <p> 1895 * The result of this operation is the same as for 1896 * {@link #divideByPowerOfTen(int) divideByPowerOfTen(-n)} given 1897 * {@code n > }{@link Integer#MIN_VALUE}. 1898 * <p> 1899 * The returned value is a new instance if this Decimal is an 1900 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1901 * internal state is altered and {@code this} is returned as result now 1902 * representing the outcome of the multiplication. 1903 * 1904 * @param n 1905 * the exponent of the power-of-ten factor to multiply with this 1906 * {@code Decimal} 1907 * @return <tt>round<sub>HALF_UP</sub>(this * 10<sup>n</sup>)</tt> 1908 */ 1909 Decimal<S> multiplyByPowerOfTen(int n); 1910 1911 /** 1912 * Returns a {@code Decimal} whose value is <tt>(this * 10<sup>n</sup>)</tt> 1913 * . For negative <tt>n</tt> the multiplication turns into a de-facto 1914 * division and the result is rounded to the {@link #getScale() scale} of 1915 * this Decimal using the specified {@code roundingMode}. If the 1916 * multiplication causes an overflow, the result is silently truncated. 1917 * <p> 1918 * The result of this operation is the same as for 1919 * {@link #divideByPowerOfTen(int, RoundingMode) divideByPowerOfTen(-n, 1920 * roundingMode)} given {@code n > }{@link Integer#MIN_VALUE}. 1921 * <p> 1922 * The returned value is a new instance if this Decimal is an 1923 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1924 * internal state is altered and {@code this} is returned as result now 1925 * representing the outcome of the multiplication. 1926 * 1927 * @param n 1928 * the exponent of the power-of-ten factor to multiply with this 1929 * {@code Decimal} 1930 * @param roundingMode 1931 * the rounding mode to apply if the result needs to be rounded 1932 * for the case {@code n < 0} 1933 * @return <tt>round(this * 10<sup>n</sup>)</tt> 1934 * @throws ArithmeticException 1935 * if {@code n < 0} and {@code roundingMode==UNNECESSARY} and 1936 * rounding is necessary 1937 */ 1938 Decimal<S> multiplyByPowerOfTen(int n, RoundingMode roundingMode); 1939 1940 /** 1941 * Returns a {@code Decimal} whose value is <tt>(this * 10<sup>n</sup>)</tt> 1942 * . For negative <tt>n</tt> the multiplication turns into a de-facto 1943 * division and the result is rounded to the {@link #getScale() scale} of 1944 * this Decimal using the {@link RoundingMode} specified by the 1945 * {@code truncationPolicy} argument. The {@code truncationPolicy} also 1946 * defines the {@link OverflowMode} to apply if an overflow occurs during 1947 * the multiplication. 1948 * <p> 1949 * The result of this operation is the same as for 1950 * {@link #divideByPowerOfTen(int, TruncationPolicy) divideByPowerOfTen(-n, 1951 * truncationPolicy)} given {@code n > }{@link Integer#MIN_VALUE}. 1952 * <p> 1953 * The returned value is a new instance if this Decimal is an 1954 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1955 * internal state is altered and {@code this} is returned as result now 1956 * representing the outcome of the multiplication. 1957 * 1958 * @param n 1959 * the exponent of the power-of-ten factor to multiply with this 1960 * {@code Decimal} 1961 * @param truncationPolicy 1962 * the truncation policy specifying {@link RoundingMode} to apply 1963 * if rounding is necessary when {@code n < 0} as well 1964 * {@link OverflowMode} to use if {@code n > 0} and an overflow 1965 * occurs during the multiplication 1966 * @return <tt>round(this * 10<sup>n</sup>)</tt> 1967 * @throws ArithmeticException 1968 * if {@code truncationPolicy} defines 1969 * {@link RoundingMode#UNNECESSARY} and rounding is necessary 1970 * when {@code n < 0}, or if an overflow occurs and the policy 1971 * declares {@link OverflowMode#CHECKED} for the case 1972 * {@code n > 0} 1973 */ 1974 Decimal<S> multiplyByPowerOfTen(int n, TruncationPolicy truncationPolicy); 1975 1976 // divide 1977 1978 /** 1979 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 1980 * result is rounded to the {@link #getScale() scale} of this Decimal using 1981 * {@link RoundingMode#HALF_UP HALF_UP} rounding. If the division causes an 1982 * overflow, the result is silently truncated. 1983 * <p> 1984 * The returned value is a new instance if this Decimal is an 1985 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 1986 * internal state is altered and {@code this} is returned as result now 1987 * representing the outcome of the division. 1988 * 1989 * @param divisor 1990 * divisor value by which this {@code Decimal} is to be divided 1991 * @return <tt>round<sub>HALF_UP</sub>(this / divisor)</tt> 1992 * @throws ArithmeticException 1993 * if {@code divisor==0} 1994 */ 1995 Decimal<S> divide(Decimal<S> divisor); 1996 1997 /** 1998 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 1999 * result is rounded to the {@link #getScale() scale} of this Decimal using 2000 * the specified {@code roundingMode}. If the division causes an overflow, 2001 * the result is silently truncated. 2002 * <p> 2003 * The returned value is a new instance if this Decimal is an 2004 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2005 * internal state is altered and {@code this} is returned as result now 2006 * representing the outcome of the division. 2007 * 2008 * @param divisor 2009 * divisor value by which this {@code Decimal} is to be divided 2010 * @param roundingMode 2011 * the rounding mode to apply if the result needs to be rounded 2012 * @return {@code round(this / divisor)} 2013 * @throws ArithmeticException 2014 * if {@code divisor==0} or if {@code roundingMode==UNNECESSARY} 2015 * and rounding is necessary 2016 */ 2017 Decimal<S> divide(Decimal<S> divisor, RoundingMode roundingMode); 2018 2019 /** 2020 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2021 * result is rounded to the {@link #getScale() scale} of this Decimal using 2022 * the {@link RoundingMode} specified by the {@code truncationPolicy} 2023 * argument. The {@code truncationPolicy} also defines the 2024 * {@link OverflowMode} to apply if an overflow occurs during the divide 2025 * operation. 2026 * <p> 2027 * The returned value is a new instance if this Decimal is an 2028 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2029 * internal state is altered and {@code this} is returned as result now 2030 * representing the outcome of the division. 2031 * 2032 * @param divisor 2033 * divisor value by which this {@code Decimal} is to be divided 2034 * @param truncationPolicy 2035 * the truncation policy specifying {@link RoundingMode} and 2036 * {@link OverflowMode} to apply if rounding is necessary or if 2037 * an overflow occurs 2038 * @return <tt>round(this / divisor)</tt> 2039 * @throws ArithmeticException 2040 * if {@code divisor==0}, or if {@code truncationPolicy} defines 2041 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 2042 * if an overflow occurs and the policy declares 2043 * {@link OverflowMode#CHECKED} 2044 */ 2045 Decimal<S> divide(Decimal<S> divisor, TruncationPolicy truncationPolicy); 2046 2047 /** 2048 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2049 * result is rounded to the {@link #getScale() scale} of this Decimal using 2050 * {@link RoundingMode#HALF_UP HALF_UP} rounding. If the division causes an 2051 * overflow, the result is silently truncated. 2052 * <p> 2053 * The returned value is a new instance if this Decimal is an 2054 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2055 * internal state is altered and {@code this} is returned as result now 2056 * representing the outcome of the division. 2057 * 2058 * @param divisor 2059 * divisor value by which this {@code Decimal} is to be divided 2060 * @return <tt>round<sub>HALF_UP</sub>(this / divisor)</tt> 2061 * @throws ArithmeticException 2062 * if {@code divisor==0} 2063 */ 2064 Decimal<S> divideBy(Decimal<?> divisor); 2065 2066 /** 2067 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2068 * result is rounded to the {@link #getScale() scale} of this Decimal using 2069 * the specified {@code roundingMode}. If the division causes an overflow, 2070 * the result is silently truncated. 2071 * <p> 2072 * The returned value is a new instance if this Decimal is an 2073 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2074 * internal state is altered and {@code this} is returned as result now 2075 * representing the outcome of the division. 2076 * 2077 * @param divisor 2078 * divisor value by which this {@code Decimal} is to be divided 2079 * @param roundingMode 2080 * the rounding mode to apply if rounding is necessary 2081 * @return {@code round(this / divisor)} 2082 * @throws ArithmeticException 2083 * if {@code divisor==0} or if {@code roundingMode==UNNECESSARY} 2084 * and rounding is necessary 2085 */ 2086 Decimal<S> divideBy(Decimal<?> divisor, RoundingMode roundingMode); 2087 2088 /** 2089 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2090 * result is rounded to the {@link #getScale() scale} of this Decimal using 2091 * the {@link RoundingMode} defined by the {@code truncationPolicy} 2092 * argument. The {@code truncationPolicy} also defines the 2093 * {@link OverflowMode} to apply if the operation causes an overflow. 2094 * <p> 2095 * The returned value is a new instance if this Decimal is an 2096 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2097 * internal state is altered and {@code this} is returned as result now 2098 * representing the outcome of the division. 2099 * 2100 * @param divisor 2101 * divisor value by which this {@code Decimal} is to be divided 2102 * @param truncationPolicy 2103 * the truncation policy specifying {@link RoundingMode} and 2104 * {@link OverflowMode} to apply if rounding is necessary or if 2105 * an overflow occurs 2106 * @return {@code round(this / divisor)} 2107 * @throws ArithmeticException 2108 * if {@code divisor==0}, or if {@code truncationPolicy} defines 2109 * {@link RoundingMode#UNNECESSARY} and rounding is necessary, 2110 * or if an overflow occurs and the policy declares 2111 * {@link OverflowMode#CHECKED} 2112 */ 2113 Decimal<S> divideBy(Decimal<?> divisor, TruncationPolicy truncationPolicy); 2114 2115 /** 2116 * Returns a {@code Decimal} whose value is {@code (this / divisor)} rounded 2117 * down. This method is a shortcut for calling 2118 * {@link #divide(Decimal,RoundingMode) divide(divisor, RoundingMode.DOWN)}. 2119 * If the division causes an overflow, the result is silently truncated. 2120 * <p> 2121 * The returned value is a new instance if this Decimal is an 2122 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2123 * internal state is altered and {@code this} is returned as result now 2124 * representing the outcome of the division. 2125 * 2126 * @param divisor 2127 * divisor value by which this {@code Decimal} is to be divided 2128 * @return <tt>round<sub>DOWN</sub>(this / divisor)</tt> 2129 * @throws ArithmeticException 2130 * if {@code divisor==0} 2131 */ 2132 Decimal<S> divideTruncate(Decimal<S> divisor); 2133 2134 /** 2135 * Returns a {@code Decimal} whose value is {@code (this / divisor)}, 2136 * checking for lost information. If the quotient cannot be represented 2137 * exactly with the {@link #getScale() scale} of this Decimal then an 2138 * {@code ArithmeticException} is thrown. 2139 * <p> 2140 * The returned value is a new instance if this Decimal is an 2141 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2142 * internal state is altered and {@code this} is returned as result now 2143 * representing the outcome of the division. 2144 * 2145 * @param divisor 2146 * divisor value by which this {@code Decimal} is to be divided 2147 * @return {@code this / divisor} 2148 * @throws ArithmeticException 2149 * if {@code divisor==0}, or if the result does not fit in a 2150 * Decimal with the scale of this Decimal without rounding and 2151 * without exceeding the the possible range of such a Decimal 2152 */ 2153 Decimal<S> divideExact(Decimal<S> divisor); 2154 2155 /** 2156 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2157 * result is rounded to the {@link #getScale() scale} of this Decimal using 2158 * {@link RoundingMode#HALF_UP HALF_UP} rounding. If the division causes an 2159 * overflow, the result is silently truncated. 2160 * <p> 2161 * The returned value is a new instance if this Decimal is an 2162 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2163 * internal state is altered and {@code this} is returned as result now 2164 * representing the outcome of the division. 2165 * 2166 * @param divisor 2167 * long value by which this {@code Decimal} is to be divided 2168 * @return <tt>round<sub>HALF_UP</sub>(this / divisor)</tt> 2169 * @throws ArithmeticException 2170 * if {@code divisor==0} 2171 */ 2172 Decimal<S> divide(long divisor); 2173 2174 /** 2175 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2176 * result is rounded to the {@link #getScale() scale} of this Decimal using 2177 * the specified {@code roundingMode}. If the division causes an overflow, 2178 * the result is silently truncated. 2179 * <p> 2180 * The returned value is a new instance if this Decimal is an 2181 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2182 * internal state is altered and {@code this} is returned as result now 2183 * representing the outcome of the division. 2184 * 2185 * @param divisor 2186 * long value by which this {@code Decimal} is to be divided 2187 * @param roundingMode 2188 * the rounding mode to apply if the result needs to be rounded 2189 * @return {@code round(this / divisor)} 2190 * @throws ArithmeticException 2191 * if {@code divisor==0} or if {@code roundingMode==UNNECESSARY} 2192 * and rounding is necessary 2193 */ 2194 Decimal<S> divide(long divisor, RoundingMode roundingMode); 2195 2196 /** 2197 * Returns a {@code Decimal} whose value is {@code (this / divisor)}. The 2198 * result is rounded to the {@link #getScale() scale} of this Decimal using 2199 * the {@link RoundingMode} specified by the {@code truncationPolicy} 2200 * argument. The {@code truncationPolicy} also defines the 2201 * {@link OverflowMode} to apply if an overflow occurs during the divide 2202 * operation. 2203 * <p> 2204 * The returned value is a new instance if this Decimal is an 2205 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2206 * internal state is altered and {@code this} is returned as result now 2207 * representing the outcome of the division. 2208 * 2209 * @param divisor 2210 * long value by which this {@code Decimal} is to be divided 2211 * @param truncationPolicy 2212 * the truncation policy specifying {@link RoundingMode} and 2213 * {@link OverflowMode} to apply if rounding is necessary or if 2214 * an overflow occurs 2215 * @return <tt>round(this / divisor)</tt> 2216 * @throws ArithmeticException 2217 * if {@code divisor==0}, or if {@code truncationPolicy} defines 2218 * {@link RoundingMode#UNNECESSARY} and rounding is necessary, 2219 * or if the policy declares {@link OverflowMode#CHECKED} an 2220 * overflow occurs 2221 */ 2222 Decimal<S> divide(long divisor, TruncationPolicy truncationPolicy); 2223 2224 /** 2225 * Returns a {@code Decimal} whose value is {@code (this / divisor)} after 2226 * converting the given {@code double} argument into a Decimal value of the 2227 * same scale as {@code this} Decimal. {@link RoundingMode#HALF_UP HALF_UP} 2228 * rounding mode is used if necessary and applied twice during the 2229 * conversion step <i>before</i> the division and again when rounding the 2230 * quotient to the {@link #getScale() scale} of this Decimal. Overflows due 2231 * to conversion or division result in an exception. 2232 * <p> 2233 * The returned value is a new instance if this Decimal is an 2234 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2235 * internal state is altered and {@code this} is returned as result now 2236 * representing the outcome of the division. 2237 * 2238 * @param divisor 2239 * divisor value by which this {@code Decimal} is to be divided 2240 * @return <tt>round<sub>HALF_UP</sub>(this / round<sub>HALF_UP</sub>(divisor))</tt> 2241 * @throws IllegalArgumentException 2242 * if {@code divisor} is NaN or infinite or if the magnitude is 2243 * too large for the double to be represented as a 2244 * {@code Decimal} 2245 * @throws ArithmeticException 2246 * if {@code divisor==0} or if an overflow occurs during the 2247 * divide operation 2248 */ 2249 Decimal<S> divide(double divisor); 2250 2251 /** 2252 * Returns a {@code Decimal} whose value is {@code (this / divisor)} after 2253 * converting the given {@code double} argument into a Decimal value of the 2254 * same scale as {@code this} Decimal. Rounding, if necessary, uses the 2255 * specified {@code roundingMode} and is applied during the conversion step 2256 * <i>before</i> the division and again when rounding the quotient to the 2257 * {@link #getScale() scale} of this Decimal. Overflows due to conversion or 2258 * division result in an exception. 2259 * <p> 2260 * The returned value is a new instance if this Decimal is an 2261 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2262 * internal state is altered and {@code this} is returned as result now 2263 * representing the outcome of the division. 2264 * 2265 * @param divisor 2266 * divisor value by which this {@code Decimal} is to be divided 2267 * @param roundingMode 2268 * the rounding mode to apply if the converted divisor or the 2269 * resulting quotient needs to be rounded 2270 * @return {@code round(this / round(divisor))} 2271 * @throws IllegalArgumentException 2272 * if {@code divisor} is NaN or infinite or if the magnitude is 2273 * too large for the double to be represented as a 2274 * {@code Decimal} 2275 * @throws ArithmeticException 2276 * if {@code divisor==0}, if {@code roundingMode==UNNECESSARY} 2277 * and rounding is necessary or if an overflow occurs during the 2278 * divide operation 2279 */ 2280 Decimal<S> divide(double divisor, RoundingMode roundingMode); 2281 2282 /** 2283 * Returns a {@code Decimal} whose value is 2284 * <tt>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> with the 2285 * {@link #getScale() scale} of this Decimal. The result is rounded to the 2286 * scale of this Decimal using default {@link RoundingMode#HALF_UP HALF_UP} 2287 * rounding. If the division causes an overflow, the result is silently 2288 * truncated. 2289 * <p> 2290 * The returned value is a new instance if this Decimal is an 2291 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2292 * internal state is altered and {@code this} is returned as result now 2293 * representing the outcome of the division. 2294 * 2295 * @param unscaledDivisor 2296 * divisor value by which this {@code Decimal} is to be divided 2297 * @return <tt>round<sub>HALF_UP</sub>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> 2298 * @throws ArithmeticException 2299 * if {@code unscaledDivisor==0} 2300 */ 2301 Decimal<S> divideUnscaled(long unscaledDivisor); 2302 2303 /** 2304 * Returns a {@code Decimal} whose value is 2305 * <tt>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> with the 2306 * {@link #getScale() scale} of this Decimal. The result is rounded to the 2307 * scale of this Decimal using the specified {@code roundingMode}. If the 2308 * division causes an overflow, the result is silently truncated. 2309 * <p> 2310 * The returned value is a new instance if this Decimal is an 2311 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2312 * internal state is altered and {@code this} is returned as result now 2313 * representing the outcome of the division. 2314 * 2315 * @param unscaledDivisor 2316 * divisor value by which this {@code Decimal} is to be divided 2317 * @param roundingMode 2318 * the rounding mode to apply if the result needs to be rounded 2319 * @return <tt>round(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> 2320 * @throws ArithmeticException 2321 * if {@code unscaledDivisor==0} or if 2322 * {@code roundingMode==UNNECESSARY} and rounding is necessary 2323 */ 2324 Decimal<S> divideUnscaled(long unscaledDivisor, RoundingMode roundingMode); 2325 2326 /** 2327 * Returns a {@code Decimal} whose value is 2328 * <tt>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> with the 2329 * {@link #getScale() scale} of this Decimal. The result is rounded to the 2330 * scale of this Decimal using the using the {@link RoundingMode} specified 2331 * by the {@code truncationPolicy} argument. The {@code truncationPolicy} 2332 * also defines the {@link OverflowMode} to apply if an overflow occurs 2333 * during the division. 2334 * <p> 2335 * The returned value is a new instance if this Decimal is an 2336 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2337 * internal state is altered and {@code this} is returned as result now 2338 * representing the outcome of the division. 2339 * 2340 * @param unscaledDivisor 2341 * divisor value by which this {@code Decimal} is to be divided 2342 * @param truncationPolicy 2343 * the truncation policy specifying {@link RoundingMode} and 2344 * {@link OverflowMode} to apply if rounding is necessary or if 2345 * an overflow occurs 2346 * @return <tt>round(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> 2347 * @throws ArithmeticException 2348 * if {@code unscaledDivisor==0}, if {@code truncationPolicy} 2349 * defines {@link RoundingMode#UNNECESSARY} and rounding is 2350 * necessary or if an overflow occurs and the policy declares 2351 * {@link OverflowMode#CHECKED} 2352 */ 2353 Decimal<S> divideUnscaled(long unscaledDivisor, TruncationPolicy truncationPolicy); 2354 2355 /** 2356 * Returns a {@code Decimal} whose value is 2357 * <tt>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt>. The 2358 * result is rounded to the scale of this Decimal using 2359 * {@link RoundingMode#HALF_UP HALF_UP} rounding. If the operation causes an 2360 * overflow, the result is silently truncated. 2361 * <p> 2362 * The returned value is a new instance if this Decimal is an 2363 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2364 * internal state is altered and {@code this} is returned as result now 2365 * representing the outcome of the division. 2366 * 2367 * @param unscaledDivisor 2368 * divisor value by which this {@code Decimal} is to be divided 2369 * @param scale 2370 * the scale to apply to {@code unscaledDivisor}, positive to 2371 * indicate the number of fraction digits to the right of the 2372 * Decimal point and negative to indicate up-scaling with a power 2373 * of ten 2374 * @return <tt>round<sub>HALF_UP</sub>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> 2375 * @throws ArithmeticException 2376 * if {@code unscaledDivisor==0} 2377 */ 2378 Decimal<S> divideUnscaled(long unscaledDivisor, int scale); 2379 2380 /** 2381 * Returns a {@code Decimal} whose value is 2382 * <tt>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt>. The 2383 * result is rounded to the scale of this Decimal using the specified 2384 * {@code roundingMode}. If the operation causes an overflow, the result is 2385 * silently truncated. 2386 * <p> 2387 * The returned value is a new instance if this Decimal is an 2388 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2389 * internal state is altered and {@code this} is returned as result now 2390 * representing the outcome of the division. 2391 * 2392 * @param unscaledDivisor 2393 * divisor value by which this {@code Decimal} is to be divided 2394 * @param scale 2395 * the scale to apply to {@code unscaledDivisor}, positive to 2396 * indicate the number of fraction digits to the right of the 2397 * Decimal point and negative to indicate up-scaling with a power 2398 * of ten 2399 * @param roundingMode 2400 * the rounding mode to apply if the result needs to be rounded 2401 * @return <tt>round(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> 2402 * @throws ArithmeticException 2403 * if {@code unscaledDivisor==0} or if 2404 * {@code roundingMode==UNNECESSARY} and rounding is necessary 2405 */ 2406 Decimal<S> divideUnscaled(long unscaledDivisor, int scale, RoundingMode roundingMode); 2407 2408 /** 2409 * Returns a {@code Decimal} whose value is 2410 * <tt>(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt>. The 2411 * result is rounded to the scale of this Decimal using the 2412 * {@link RoundingMode} defined by the {@code truncationPolicy} argument. 2413 * The {@code truncationPolicy} also defines the {@link OverflowMode} to 2414 * apply if the operation causes an overflow. 2415 * <p> 2416 * The returned value is a new instance if this Decimal is an 2417 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2418 * internal state is altered and {@code this} is returned as result now 2419 * representing the outcome of the division. 2420 * 2421 * @param unscaledDivisor 2422 * divisor value by which this {@code Decimal} is to be divided 2423 * @param scale 2424 * the scale to apply to {@code unscaledDivisor}, positive to 2425 * indicate the number of fraction digits to the right of the 2426 * Decimal point and negative to indicate up-scaling with a power 2427 * of ten 2428 * @param truncationPolicy 2429 * the truncation policy specifying {@link RoundingMode} and 2430 * {@link OverflowMode} to apply if rounding is necessary or if 2431 * an overflow occurs 2432 * @return <tt>round(this / (unscaledDivisor × 10<sup>-scale</sup>))</tt> 2433 * @throws ArithmeticException 2434 * if {@code unscaledDivisor==0}, if {@code truncationPolicy} 2435 * defines {@link RoundingMode#UNNECESSARY} and rounding is 2436 * necessary or if an overflow occurs and the policy declares 2437 * {@link OverflowMode#CHECKED} 2438 */ 2439 Decimal<S> divideUnscaled(long unscaledDivisor, int scale, TruncationPolicy truncationPolicy); 2440 2441 /** 2442 * Returns a {@code Decimal} whose value is <tt>(this / 10<sup>n</sup>)</tt> 2443 * . The result is rounded to the {@link #getScale() scale} of this Decimal 2444 * using {@link RoundingMode#HALF_UP HALF_UP} rounding. 2445 * <p> 2446 * For negative <tt>n</tt> the division turns into a de-facto 2447 * multiplication. If the multiplication causes an overflow, the result is 2448 * silently truncated. 2449 * <p> 2450 * The result of this operation is the same as for 2451 * {@link #multiplyByPowerOfTen(int) multiplyByPowerOfTen(-n)} (unless 2452 * {@code n == } {@link Integer#MIN_VALUE}). 2453 * <p> 2454 * The returned value is a new instance if this Decimal is an 2455 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2456 * internal state is altered and {@code this} is returned as result now 2457 * representing the outcome of the division. 2458 * 2459 * @param n 2460 * the exponent of the power-of-ten divisor by which this 2461 * {@code Decimal} is to be divided 2462 * @return <tt>round<sub>HALF_UP</sub>(this / 10<sup>n</sup>)</tt> 2463 */ 2464 Decimal<S> divideByPowerOfTen(int n); 2465 2466 /** 2467 * Returns a {@code Decimal} whose value is <tt>(this / 10<sup>n</sup>)</tt> 2468 * . The result is rounded to the {@link #getScale() scale} of this Decimal 2469 * using the specified {@code roudningMode}. 2470 * <p> 2471 * For negative <tt>n</tt> the division turns into a de-facto 2472 * multiplication. If the multiplication causes an overflow, the result is 2473 * silently truncated. 2474 * <p> 2475 * The result of this operation is the same as for 2476 * {@link #multiplyByPowerOfTen(int, RoundingMode) multiplyByPowerOfTen(-n, 2477 * roundingMode)} (unless {@code n == } {@link Integer#MIN_VALUE}). 2478 * <p> 2479 * The returned value is a new instance if this Decimal is an 2480 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2481 * internal state is altered and {@code this} is returned as result now 2482 * representing the outcome of the division. 2483 * 2484 * @param n 2485 * the exponent of the power-of-ten divisor by which this 2486 * {@code Decimal} is to be divided 2487 * @param roundingMode 2488 * the rounding mode to apply if the result needs to be rounded 2489 * for the case {@code n > 0} 2490 * @return <tt>round(this / 10<sup>n</sup>)</tt> 2491 * @throws ArithmeticException 2492 * if {@code n > 0} and {@code roundingMode==UNNECESSARY} and 2493 * rounding is necessary 2494 */ 2495 Decimal<S> divideByPowerOfTen(int n, RoundingMode roundingMode); 2496 2497 /** 2498 * Returns a {@code Decimal} whose value is <tt>(this / 10<sup>n</sup>)</tt> 2499 * . The result is rounded to the {@link #getScale() scale} of this Decimal 2500 * using the {@link RoundingMode} specified by the {@code truncationPolicy} 2501 * argument. 2502 * <p> 2503 * For negative <tt>n</tt> the division turns into a de-facto multiplication 2504 * and {@code truncationPolicy} defines the {@link OverflowMode} to apply if 2505 * an overflow occurs during the multiplication. 2506 * <p> 2507 * The result of this operation is the same as for 2508 * {@link #multiplyByPowerOfTen(int, TruncationPolicy) 2509 * multiplyByPowerOfTen(-n, truncationPolicy)} (unless {@code n == } 2510 * {@link Integer#MIN_VALUE}). 2511 * <p> 2512 * The returned value is a new instance if this Decimal is an 2513 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2514 * internal state is altered and {@code this} is returned as result now 2515 * representing the outcome of the division. 2516 * 2517 * @param n 2518 * the exponent of the power-of-ten divisor by which this 2519 * {@code Decimal} is to be divided 2520 * @param truncationPolicy 2521 * the truncation policy specifying {@link RoundingMode} to apply 2522 * if rounding is necessary when {@code n > 0} as well 2523 * {@link OverflowMode} to use if {@code n < 0} and an overflow 2524 * occurs during the de-facto multiplication 2525 * @return <tt>round(this / 10<sup>n</sup>)</tt> 2526 * @throws ArithmeticException 2527 * if {@code truncationPolicy} defines 2528 * {@link RoundingMode#UNNECESSARY} and rounding is necessary 2529 * when {@code n > 0}, or if an overflow occurs and the policy 2530 * declares {@link OverflowMode#CHECKED} for the case 2531 * {@code n < 0} 2532 */ 2533 Decimal<S> divideByPowerOfTen(int n, TruncationPolicy truncationPolicy); 2534 2535 /** 2536 * Returns a {@code Decimal} whose value is {@code (this / divisor)} rounded 2537 * down to the next integer. If the division causes an overflow, the result 2538 * is silently truncated. 2539 * <p> 2540 * The returned value is a new instance if this Decimal is an 2541 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2542 * internal state is altered and {@code this} is returned as result now 2543 * representing the outcome of the division. 2544 * 2545 * @param divisor 2546 * value by which this {@code Decimal} is to be divided. 2547 * @return The integer part of {@code (this / divisor)}. 2548 * @throws ArithmeticException 2549 * if {@code divisor==0} 2550 * @see #divideToIntegralValue(Decimal, OverflowMode) 2551 * @see #divideToLongValue(Decimal) 2552 * @see #remainder(Decimal) 2553 */ 2554 Decimal<S> divideToIntegralValue(Decimal<S> divisor); 2555 2556 /** 2557 * Returns a {@code Decimal} whose value is {@code (this / divisor)} rounded 2558 * down to the next integer. The specified {@code overflowMode} determines 2559 * whether to truncate the result silently or to throw an exception if an 2560 * overflow occurs. 2561 * <p> 2562 * The returned value is a new instance if this Decimal is an 2563 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2564 * internal state is altered and {@code this} is returned as result now 2565 * representing the outcome of the division. 2566 * 2567 * @param divisor 2568 * value by which this {@code Decimal} is to be divided. 2569 * @param overflowMode 2570 * the overflow mode to apply if the division leads to an 2571 * overflow 2572 * @return The integer part of {@code (this / divisor)}. 2573 * @throws ArithmeticException 2574 * if {@code divisor==0} or if {@code overflowMode==CHECKED} and 2575 * an overflow occurs 2576 * @see #divideToIntegralValue(Decimal) 2577 * @see #divideToLongValue(Decimal) 2578 * @see #remainder(Decimal) 2579 */ 2580 Decimal<S> divideToIntegralValue(Decimal<S> divisor, OverflowMode overflowMode); 2581 2582 /** 2583 * Returns a {@code Decimal} whose value {@code (this / divisor)} rounded 2584 * down to the next {@code long} value. If the division causes an overflow, 2585 * the result is silently truncated. 2586 * 2587 * @param divisor 2588 * value by which this {@code Decimal} is to be divided. 2589 * @return The integer part of {@code (this / divisor)} returned as 2590 * {@code long} 2591 * @throws ArithmeticException 2592 * if {@code divisor==0} 2593 * @see #divideToLongValue(Decimal, OverflowMode) 2594 * @see #divideToIntegralValue(Decimal) 2595 * @see #remainder(Decimal) 2596 */ 2597 long divideToLongValue(Decimal<S> divisor); 2598 2599 /** 2600 * Returns a {@code Decimal} whose value {@code (this / divisor)} rounded 2601 * down to the next {@code long} value. The specified {@code overflowMode} 2602 * determines whether to truncate the result silently or to throw an 2603 * exception if an overflow occurs. 2604 * 2605 * @param divisor 2606 * value by which this {@code Decimal} is to be divided. 2607 * @param overflowMode 2608 * the overflow mode to apply if the division leads to an 2609 * overflow 2610 * @return The integer part of {@code (this / divisor)} returned as 2611 * {@code long} 2612 * @throws ArithmeticException 2613 * if {@code divisor==0} or if {@code overflowMode==CHECKED} and 2614 * an overflow occurs 2615 * @see #divideToLongValue(Decimal) 2616 * @see #divideToIntegralValue(Decimal) 2617 * @see #remainder(Decimal) 2618 */ 2619 long divideToLongValue(Decimal<S> divisor, OverflowMode overflowMode); 2620 2621 /** 2622 * Returns a two-element {@code Decimal} array containing the result of 2623 * {@code divideToIntegralValue} followed by the result of {@code remainder} 2624 * on the two operands. If the division causes an overflow, the result is 2625 * silently truncated. 2626 * <p> 2627 * Note that if both the integer quotient and remainder are needed, this 2628 * method is faster than using the {@code divideToIntegralValue} and 2629 * {@code remainder} methods separately because the division need only be 2630 * carried out once. 2631 * 2632 * @param divisor 2633 * value by which this {@code Decimal} is to be divided, and the 2634 * remainder computed. 2635 * @return a two element {@code Decimal} array: the quotient (the result of 2636 * {@code divideToIntegralValue}) is the initial element and the 2637 * remainder is the final element. 2638 * @throws ArithmeticException 2639 * if {@code divisor==0} 2640 * @see #divideAndRemainder(Decimal, OverflowMode) 2641 * @see #divideToIntegralValue(Decimal) 2642 * @see #remainder(Decimal) 2643 */ 2644 Decimal<S>[] divideAndRemainder(Decimal<S> divisor); 2645 2646 /** 2647 * Returns a two-element {@code Decimal} array containing the result of 2648 * {@code divideToIntegralValue} followed by the result of {@code remainder} 2649 * on the two operands. The specified {@code overflowMode} determines 2650 * whether to truncate the result silently or to throw an exception if an 2651 * overflow occurs. 2652 * <p> 2653 * Note that if both the integer quotient and remainder are needed, this 2654 * method is faster than using the {@code divideToIntegralValue} and 2655 * {@code remainder} methods separately because the division need only be 2656 * carried out once. 2657 * 2658 * @param divisor 2659 * value by which this {@code Decimal} is to be divided, and the 2660 * remainder computed. 2661 * @param overflowMode 2662 * the overflow mode to apply if the division leads to an 2663 * overflow 2664 * @return a two element {@code Decimal} array: the quotient (the result of 2665 * {@code divideToIntegralValue}) is the initial element and the 2666 * remainder is the final element. 2667 * @throws ArithmeticException 2668 * if {@code divisor==0} or if {@code overflowMode==CHECKED} and 2669 * an overflow occurs 2670 * @see #divideAndRemainder(Decimal) 2671 * @see #divideToIntegralValue(Decimal) 2672 * @see #remainder(Decimal) 2673 */ 2674 Decimal<S>[] divideAndRemainder(Decimal<S> divisor, OverflowMode overflowMode); 2675 2676 /** 2677 * Returns a {@code Decimal} whose value is {@code (this % divisor)}. 2678 * <p> 2679 * The remainder is given by 2680 * {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))} 2681 * . Note that this is not the modulo operation (the result can be 2682 * negative). 2683 * <p> 2684 * The returned value is a new instance if this Decimal is an 2685 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2686 * internal state is altered and {@code this} is returned as result now 2687 * representing the outcome of the operation. 2688 * 2689 * @param divisor 2690 * value by which this {@code Decimal} is to be divided. 2691 * @return {@code this % divisor}. 2692 * @throws ArithmeticException 2693 * if {@code divisor==0} 2694 * @see #divideToIntegralValue(Decimal) 2695 * @see #divideAndRemainder(Decimal) 2696 */ 2697 Decimal<S> remainder(Decimal<S> divisor); 2698 2699 // other arithmetic operations 2700 2701 /** 2702 * Returns a {@code Decimal} whose value is {@code (-this)}. 2703 * <p> 2704 * If an overflow occurs (which is true iff 2705 * {@code this.unscaledValue()==Long.MIN_VALUE}) then the result is still 2706 * negative and numerically equal to {@code this} value. 2707 * <p> 2708 * The returned value is a new instance if this Decimal is an 2709 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2710 * internal state is altered and {@code this} is returned as result now 2711 * representing the outcome of the division. 2712 * 2713 * @return {@code -this} 2714 */ 2715 Decimal<S> negate(); 2716 2717 /** 2718 * Returns a {@code Decimal} whose value is {@code (-this)}. 2719 * <p> 2720 * The specified {@code overflowMode} determines whether to truncate the 2721 * result silently or to throw an exception if an overflow occurs (which is 2722 * true iff {@code this.unscaledValue()==Long.MIN_VALUE}). 2723 * <p> 2724 * The returned value is a new instance if this Decimal is an 2725 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2726 * internal state is altered and {@code this} is returned as result now 2727 * representing the outcome of the division. 2728 * 2729 * @param overflowMode 2730 * the overflow mode to apply 2731 * @return {@code -this} 2732 * @throws ArithmeticException 2733 * if {@code overflowMode==CHECKED} and an overflow occurs 2734 * (which is true iff 2735 * {@code this.unscaledValue()==Long.MIN_VALUE}) 2736 */ 2737 Decimal<S> negate(OverflowMode overflowMode); 2738 2739 /** 2740 * Returns a {@code Decimal} whose value is the absolute value of this 2741 * {@code Decimal}. 2742 * <p> 2743 * If an overflow occurs (which is true iff 2744 * {@code this.unscaledValue()==Long.MIN_VALUE}) then the result is still 2745 * negative and numerically equal to {@code this} value. 2746 * <p> 2747 * The returned value is a new instance if this Decimal is an 2748 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2749 * internal state is altered and {@code this} is returned as result now 2750 * representing the outcome of the division. 2751 * 2752 * @return {@code abs(this)} 2753 */ 2754 Decimal<S> abs(); 2755 2756 /** 2757 * Returns a {@code Decimal} whose value is the absolute value of this 2758 * {@code Decimal}. 2759 * <p> 2760 * The specified {@code overflowMode} determines whether to truncate the 2761 * result silently or to throw an exception if an overflow occurs (which is 2762 * true iff {@code this.unscaledValue()==Long.MIN_VALUE}). 2763 * <p> 2764 * The returned value is a new instance if this Decimal is an 2765 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2766 * internal state is altered and {@code this} is returned as result now 2767 * representing the outcome of the division. 2768 * 2769 * @param overflowMode 2770 * the overflow mode to apply 2771 * @return {@code abs(this)} 2772 * @throws ArithmeticException 2773 * if {@code overflowMode==CHECKED} and an overflow occurs 2774 * (which is true iff 2775 * {@code this.unscaledValue()==Long.MIN_VALUE}) 2776 */ 2777 Decimal<S> abs(OverflowMode overflowMode); 2778 2779 /** 2780 * Returns a {@code Decimal} whose value is {@code (1 / this)}. The result 2781 * is rounded to the {@link #getScale() scale} of this Decimal using default 2782 * {@link RoundingMode#HALF_UP HALF_UP} rounding. If the inversion causes an 2783 * overflow, the result is silently truncated. 2784 * <p> 2785 * The returned value is a new instance if this Decimal is an 2786 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2787 * internal state is altered and {@code this} is returned as result now 2788 * representing the outcome of the inversion. 2789 * 2790 * @return <tt>round<sub>HALF_UP</sub>(1 / this)</tt> 2791 * @throws ArithmeticException 2792 * if {@code this==0} 2793 */ 2794 Decimal<S> invert(); 2795 2796 /** 2797 * Returns a {@code Decimal} whose value is {@code (1 / this)}. The result 2798 * is rounded to the {@link #getScale() scale} of this Decimal using the 2799 * specified {@code roundingMode}. If the inversion causes an overflow, the 2800 * result is silently truncated. 2801 * <p> 2802 * The returned value is a new instance if this Decimal is an 2803 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2804 * internal state is altered and {@code this} is returned as result now 2805 * representing the outcome of the inversion. 2806 * 2807 * @param roundingMode 2808 * the rounding mode to apply if the result needs to be rounded 2809 * @return {@code round(1 / this)} 2810 * @throws ArithmeticException 2811 * if {@code this==0} or if {@code roundingMode==UNNECESSARY} 2812 * and rounding is necessary 2813 */ 2814 Decimal<S> invert(RoundingMode roundingMode); 2815 2816 /** 2817 * Returns a {@code Decimal} whose value is {@code (1 / this)}. The result 2818 * is rounded to the {@link #getScale() scale} of this Decimal using the 2819 * {@link RoundingMode} specified by the {@code truncationPolicy} argument. 2820 * The {@code truncationPolicy} also defines the {@link OverflowMode} to 2821 * apply if an overflow occurs during the invert operation. 2822 * <p> 2823 * The returned value is a new instance if this Decimal is an 2824 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2825 * internal state is altered and {@code this} is returned as result now 2826 * representing the outcome of the inversion. 2827 * 2828 * @param truncationPolicy 2829 * the truncation policy specifying {@link RoundingMode} and 2830 * {@link OverflowMode} to apply if rounding is necessary or if 2831 * an overflow occurs 2832 * @return {@code round(1 / this)} 2833 * @throws ArithmeticException 2834 * if {@code this==0}, if {@code truncationPolicy} defines 2835 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 2836 * if an overflow occurs and the policy declares 2837 * {@link OverflowMode#CHECKED} 2838 */ 2839 Decimal<S> invert(TruncationPolicy truncationPolicy); 2840 2841 /** 2842 * Returns a {@code Decimal} whose value is <tt>(this<sup>2</sup>)</tt>. The 2843 * result is rounded to the {@link #getScale() scale} of this Decimal using 2844 * default {@link RoundingMode#HALF_UP HALF_UP} rounding. If the square 2845 * operation causes an overflow, the result is silently truncated. 2846 * <p> 2847 * The returned value is a new instance if this Decimal is an 2848 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2849 * internal state is altered and {@code this} is returned as result now 2850 * representing the outcome of the square operation. 2851 * 2852 * @return <tt>round<sub>HALF_UP</sub>(this * this)</tt> 2853 */ 2854 Decimal<S> square(); 2855 2856 /** 2857 * Returns a {@code Decimal} whose value is <tt>(this<sup>2</sup>)</tt>. The 2858 * result is rounded to the {@link #getScale() scale} of this Decimal using 2859 * the specified {@code roundingMode}. If the square operation causes an 2860 * overflow, the result is silently truncated. 2861 * <p> 2862 * The returned value is a new instance if this Decimal is an 2863 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2864 * internal state is altered and {@code this} is returned as result now 2865 * representing the outcome of the square operation. 2866 * 2867 * @param roundingMode 2868 * the rounding mode to apply if the result needs to be rounded 2869 * @return {@code round(this * this)} 2870 * @throws ArithmeticException 2871 * if {@code roundingMode==UNNECESSARY} and rounding is 2872 * necessary 2873 */ 2874 Decimal<S> square(RoundingMode roundingMode); 2875 2876 /** 2877 * Returns a {@code Decimal} whose value is <tt>(this<sup>2</sup>)</tt>. The 2878 * result is rounded to the {@link #getScale() scale} of this Decimal using 2879 * the {@link RoundingMode} specified by the {@code truncationPolicy} 2880 * argument. The {@code truncationPolicy} also defines the 2881 * {@link OverflowMode} to apply if an overflow occurs during the square 2882 * operation. 2883 * <p> 2884 * The returned value is a new instance if this Decimal is an 2885 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2886 * internal state is altered and {@code this} is returned as result now 2887 * representing the outcome of the square operation. 2888 * 2889 * @param truncationPolicy 2890 * the truncation policy specifying {@link RoundingMode} and 2891 * {@link OverflowMode} to apply if rounding is necessary or if 2892 * an overflow occurs 2893 * @return {@code round(this * this)} 2894 * @throws ArithmeticException 2895 * if {@code truncationPolicy} defines 2896 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 2897 * if an overflow occurs and the policy declares 2898 * {@link OverflowMode#CHECKED} 2899 */ 2900 Decimal<S> square(TruncationPolicy truncationPolicy); 2901 2902 /** 2903 * Returns a {@code Decimal} whose value is the square root of {@code this} 2904 * Decimal value. The result is rounded to the {@link #getScale() scale} of 2905 * this Decimal using default {@link RoundingMode#HALF_UP HALF_UP} rounding. 2906 * <p> 2907 * The returned value is a new instance if this Decimal is an 2908 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2909 * internal state is altered and {@code this} is returned as result now 2910 * representing the outcome of the operation. 2911 * 2912 * @return {@code sqrt(this)} 2913 * @throws ArithmeticException 2914 * if {@code this < 0} 2915 */ 2916 Decimal<S> sqrt(); 2917 2918 /** 2919 * Returns a {@code Decimal} whose value is the square root of {@code this} 2920 * Decimal value. The result is rounded to the {@link #getScale() scale} of 2921 * this Decimal using the specified {@code roundingMode}. 2922 * <p> 2923 * The returned value is a new instance if this Decimal is an 2924 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2925 * internal state is altered and {@code this} is returned as result now 2926 * representing the outcome of the operation. 2927 * 2928 * @param roundingMode 2929 * the rounding mode to apply if the result needs to be rounded 2930 * @return {@code sqrt(this)} 2931 * @throws ArithmeticException 2932 * if {@code this < 0} or if {@code roundingMode==UNNECESSARY} 2933 * and rounding is necessary 2934 */ 2935 Decimal<S> sqrt(RoundingMode roundingMode); 2936 2937 /** 2938 * Returns the signum function of this {@code Decimal}. 2939 * 2940 * @return -1, 0, or 1 as the value of this {@code Decimal} is negative, 2941 * zero, or positive. 2942 */ 2943 int signum(); 2944 2945 /** 2946 * Returns a {@code Decimal} whose value is {@code (this << n)}. The shift 2947 * distance, {@code n}, may be negative, in which case this method performs 2948 * a right shift. 2949 * <p> 2950 * Computes <tt>floor(this * 2<sup>n</sup>)</tt>. 2951 * <p> 2952 * The returned value is a new instance if this Decimal is an 2953 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2954 * internal state is altered and {@code this} is returned as result now 2955 * representing the outcome of the shift operation. 2956 * 2957 * @param n 2958 * shift distance, in bits. 2959 * @return {@code this << n} 2960 * @see #shiftRight 2961 */ 2962 Decimal<S> shiftLeft(int n); 2963 2964 /** 2965 * Returns a {@code Decimal} whose value is {@code (this << n)}. The shift 2966 * distance, {@code n}, may be negative, in which case this method performs 2967 * a right shift. 2968 * <p> 2969 * Computes <tt>round(this * 2<sup>n</sup>)</tt> using the specified 2970 * {@code roundingMode}. 2971 * <p> 2972 * The returned value is a new instance if this Decimal is an 2973 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 2974 * internal state is altered and {@code this} is returned as result now 2975 * representing the outcome of the shift operation. 2976 * 2977 * @param n 2978 * shift distance, in bits. 2979 * @param roundingMode 2980 * the rounding mode to use if truncation is involved for 2981 * negative {@code n}, that is, for right shifts 2982 * @return {@code this << n} 2983 * @throws ArithmeticException 2984 * if {@code roundingMode==UNNECESSARY} and rounding is 2985 * necessary 2986 * @see #shiftRight 2987 */ 2988 Decimal<S> shiftLeft(int n, RoundingMode roundingMode); 2989 2990 /** 2991 * Returns a {@code Decimal} whose value is {@code (this << n)}. The shift 2992 * distance, {@code n}, may be negative, in which case this method performs 2993 * a right shift. 2994 * <p> 2995 * Computes <tt>round(this * 2<sup>n</sup>)</tt> using the 2996 * {@link RoundingMode} specified by the {@code truncationPolicy} argument. 2997 * <p> 2998 * The returned value is a new instance if this Decimal is an 2999 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3000 * internal state is altered and {@code this} is returned as result now 3001 * representing the outcome of the shift operation. 3002 * 3003 * @param n 3004 * shift distance, in bits. 3005 * @param truncationPolicy 3006 * the truncation policy specifying {@link RoundingMode} and 3007 * {@link OverflowMode} to apply if rounding is necessary or if 3008 * an overflow occurs 3009 * @return {@code this << n} 3010 * @throws ArithmeticException 3011 * if {@code truncationPolicy} defines 3012 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 3013 * if an overflow occurs and the policy declares 3014 * {@link OverflowMode#CHECKED} 3015 * @see #shiftRight 3016 */ 3017 Decimal<S> shiftLeft(int n, TruncationPolicy truncationPolicy); 3018 3019 /** 3020 * Returns a BigInteger whose value is {@code (this >> n)}. Sign extension 3021 * is performed. The shift distance, {@code n}, may be negative, in which 3022 * case this method performs a left shift. 3023 * <p> 3024 * Computes <tt>floor(this / 2<sup>n</sup>)</tt>. 3025 * <p> 3026 * The returned value is a new instance if this Decimal is an 3027 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3028 * internal state is altered and {@code this} is returned as result now 3029 * representing the outcome of the shift operation. 3030 * 3031 * @param n 3032 * shift distance, in bits. 3033 * @return {@code this >> n} 3034 * @see #shiftLeft 3035 */ 3036 Decimal<S> shiftRight(int n); 3037 3038 /** 3039 * Returns a BigInteger whose value is {@code (this >> n)}. Sign extension 3040 * is performed. The shift distance, {@code n}, may be negative, in which 3041 * case this method performs a left shift. 3042 * <p> 3043 * Computes <tt>round(this / 2<sup>n</sup>)</tt> using the specified 3044 * {@code roundingMode}. 3045 * <p> 3046 * The returned value is a new instance if this Decimal is an 3047 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3048 * internal state is altered and {@code this} is returned as result now 3049 * representing the outcome of the shift operation. 3050 * 3051 * @param n 3052 * shift distance, in bits. 3053 * @param roundingMode 3054 * the rounding mode to use if truncation is involved 3055 * @return {@code this >> n} 3056 * @see #shiftLeft 3057 * @throws ArithmeticException 3058 * if {@code roundingMode==UNNECESSARY} and rounding is 3059 * necessary 3060 */ 3061 Decimal<S> shiftRight(int n, RoundingMode roundingMode); 3062 3063 /** 3064 * Returns a BigInteger whose value is {@code (this >> n)}. Sign extension 3065 * is performed. The shift distance, {@code n}, may be negative, in which 3066 * case this method performs a left shift. 3067 * <p> 3068 * Computes <tt>round(this / 2<sup>n</sup>)</tt> using the 3069 * {@link RoundingMode} specified by the {@code truncationPolicy} argument. 3070 * <p> 3071 * The returned value is a new instance if this Decimal is an 3072 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3073 * internal state is altered and {@code this} is returned as result now 3074 * representing the outcome of the shift operation. 3075 * 3076 * @param n 3077 * shift distance, in bits. 3078 * @param truncationPolicy 3079 * the truncation policy specifying {@link RoundingMode} and 3080 * {@link OverflowMode} to apply if rounding is necessary or if 3081 * an overflow occurs 3082 * @return {@code this >> n} 3083 * @throws ArithmeticException 3084 * if {@code truncationPolicy} defines 3085 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 3086 * if an overflow occurs and the policy declares 3087 * {@link OverflowMode#CHECKED} 3088 * @see #shiftLeft 3089 */ 3090 Decimal<S> shiftRight(int n, TruncationPolicy truncationPolicy); 3091 3092 /** 3093 * Returns a {@code Decimal} whose value is <tt>(this<sup>n</sup>)</tt> 3094 * using default {@link RoundingMode#HALF_UP HALF_UP} rounding. 3095 * <p> 3096 * The current implementation uses the core algorithm defined in ANSI 3097 * standard X3.274-1996. For {@code n >= 0}, the returned numerical value is 3098 * within 1 ULP of the exact numerical value. No precision is guaranteed for 3099 * {@code n < 0} but the result is usually exact up to 10-20 ULP. 3100 * <p> 3101 * Properties of the X3.274-1996 algorithm are: 3102 * <ul> 3103 * <li>An {@code IllegalArgumentException} is thrown if 3104 * {@code abs(n) > 999999999}</li> 3105 * <li>if {@code n} is zero, one is returned even if {@code this} is zero, 3106 * otherwise 3107 * <ul> 3108 * <li>if {@code n} is positive, the result is calculated via the repeated 3109 * squaring technique into a single accumulator</li> 3110 * <li>if {@code n} is negative, the result is calculated as if {@code n} 3111 * were positive; this value is then divided into one</li> 3112 * <li>The final value from either the positive or negative case is then 3113 * rounded using {@link RoundingMode#HALF_UP HALF_UP} rounding</li> 3114 * </ul> 3115 * </li> 3116 * </ul> 3117 * <p> 3118 * The returned value is a new instance if this Decimal is an 3119 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3120 * internal state is altered and {@code this} is returned as result now 3121 * representing the outcome of the shift operation. 3122 * 3123 * @param n 3124 * power to raise this {@code Decimal} to 3125 * @return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996 3126 * algorithm 3127 * @throws IllegalArgumentException 3128 * if {@code abs(n) > 999999999} 3129 * @throws ArithmeticException 3130 * if {@code n} is negative and {@code this} equals zero 3131 */ 3132 Decimal<S> pow(int n); 3133 3134 /** 3135 * Returns a {@code Decimal} whose value is <tt>(this<sup>n</sup>)</tt> 3136 * applying the specified {@code roundingMode}. 3137 * <p> 3138 * The current implementation uses the core algorithm defined in ANSI 3139 * standard X3.274-1996. For {@code n >= 0}, the returned numerical value is 3140 * within 1 ULP of the exact numerical value; the result is actually exact 3141 * for all rounding modes other than HALF_UP, HALF_EVEN and HALF_DOWN. No 3142 * precision is guaranteed for {@code n < 0} but the result is usually exact 3143 * up to 10-20 ULP. 3144 * <p> 3145 * Properties of the X3.274-1996 algorithm are: 3146 * <ul> 3147 * <li>An {@code IllegalArgumentException} is thrown if 3148 * {@code abs(n) > 999999999}</li> 3149 * <li>if {@code n} is zero, one is returned even if {@code this} is zero, 3150 * otherwise 3151 * <ul> 3152 * <li>if {@code n} is positive, the result is calculated via the repeated 3153 * squaring technique into a single accumulator</li> 3154 * <li>if {@code n} is negative, the result is calculated as if {@code n} 3155 * were positive; this value is then divided into one</li> 3156 * <li>The final value from either the positive or negative case is then 3157 * rounded using the specified {@code roundingMode}</li> 3158 * </ul> 3159 * </li> 3160 * </ul> 3161 * <p> 3162 * The returned value is a new instance if this Decimal is an 3163 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3164 * internal state is altered and {@code this} is returned as result now 3165 * representing the outcome of the shift operation. 3166 * 3167 * @param n 3168 * power to raise this {@code Decimal} to 3169 * @param roundingMode 3170 * the rounding mode to apply if rounding is necessary 3171 * @return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996 3172 * algorithm 3173 * @throws IllegalArgumentException 3174 * if {@code abs(n) > 999999999} 3175 * @throws ArithmeticException 3176 * if {@code n} is negative and {@code this} equals zero or if 3177 * {@code roundingMode} equals {@link RoundingMode#UNNECESSARY} 3178 * and rounding is necessary 3179 */ 3180 Decimal<S> pow(int n, RoundingMode roundingMode); 3181 3182 /** 3183 * Returns a {@code Decimal} whose value is <tt>(this<sup>n</sup>)</tt> 3184 * applying the {@link RoundingMode} specified by {@code truncationPolicy}. 3185 * The {@code truncationPolicy} argument also defines the 3186 * {@link OverflowMode} to apply if an overflow occurs during the power 3187 * operation. 3188 * <p> 3189 * The current implementation uses the core algorithm defined in ANSI 3190 * standard X3.274-1996. For {@code n >= 0}, the returned numerical value is 3191 * within 1 ULP of the exact numerical value; the result is actually exact 3192 * for all rounding modes other than HALF_UP, HALF_EVEN and HALF_DOWN. No 3193 * precision is guaranteed for {@code n < 0} but the result is usually exact 3194 * up to 10-20 ULP. 3195 * <p> 3196 * Properties of the X3.274-1996 algorithm are: 3197 * <ul> 3198 * <li>An {@code IllegalArgumentException} is thrown if 3199 * {@code abs(n) > 999999999}</li> 3200 * <li>if {@code n} is zero, one is returned even if {@code this} is zero, 3201 * otherwise 3202 * <ul> 3203 * <li>if {@code n} is positive, the result is calculated via the repeated 3204 * squaring technique into a single accumulator</li> 3205 * <li>if {@code n} is negative, the result is calculated as if {@code n} 3206 * were positive; this value is then divided into one</li> 3207 * <li>The final value from either the positive or negative case is then 3208 * rounded using the {@link RoundingMode} specified by 3209 * {@code truncationPolicy}</li> 3210 * </ul> 3211 * </li> 3212 * </ul> 3213 * <p> 3214 * The returned value is a new instance if this Decimal is an 3215 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3216 * internal state is altered and {@code this} is returned as result now 3217 * representing the outcome of the shift operation. 3218 * 3219 * @param n 3220 * power to raise this {@code Decimal} to 3221 * @param truncationPolicy 3222 * the truncation policy specifying {@link RoundingMode} and 3223 * {@link OverflowMode} to apply if rounding is necessary or if 3224 * an overflow occurs 3225 * @return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996 3226 * algorithm 3227 * @throws IllegalArgumentException 3228 * if {@code abs(n) > 999999999} 3229 * @throws ArithmeticException 3230 * if {@code n} is negative and {@code this} equals zero; if 3231 * {@code truncationPolicy} defines 3232 * {@link RoundingMode#UNNECESSARY} and rounding is necessary or 3233 * if an overflow occurs and the policy declares 3234 * {@link OverflowMode#CHECKED} 3235 */ 3236 Decimal<S> pow(int n, TruncationPolicy truncationPolicy); 3237 3238 // compare and related methods 3239 3240 /** 3241 * Compares two {@code Decimal} objects numerically. 3242 * 3243 * @param other 3244 * {@code Decimal} to which this {@code Decimal} is to be 3245 * compared 3246 * @return the value {@code 0} if this {@code Decimal} is equal to the 3247 * argument {@code Decimal}; a value less than {@code 0} if this 3248 * {@code Decimal} is numerically less than the argument 3249 * {@code Decimal}; and a value greater than {@code 0} if this 3250 * {@code Decimal} is numerically greater than the argument 3251 * {@code Decimal} 3252 */ 3253 @Override 3254 int compareTo(Decimal<S> other); 3255 3256 /** 3257 * Compares this {@code Decimal} with the specified {@code Decimal} and 3258 * returns true if the two are numerically equal. 3259 * <p> 3260 * Returns true iff {@link #compareTo(Decimal)} returns 0. 3261 * 3262 * @param other 3263 * {@code Decimal} to which this {@code Decimal} is to be 3264 * compared 3265 * @return true this {@code Decimal} is numerically equal to {@code other} 3266 * and false otherwise 3267 */ 3268 boolean isEqualTo(Decimal<S> other); 3269 3270 /** 3271 * Compares this {@code Decimal} with the specified {@code Decimal} and 3272 * returns true if this Decimal is numerically greater than {@code other}. 3273 * <p> 3274 * Returns true iff {@link #compareTo(Decimal)} returns a value greater than 3275 * 0. 3276 * 3277 * @param other 3278 * {@code Decimal} to which this {@code Decimal} is to be 3279 * compared 3280 * @return true if {@code this > other} 3281 */ 3282 boolean isGreaterThan(Decimal<S> other); 3283 3284 /** 3285 * Compares this {@code Decimal} with the specified {@code Decimal} and 3286 * returns true if this Decimal is numerically greater than or equal to 3287 * {@code other}. 3288 * <p> 3289 * Returns true iff {@link #compareTo(Decimal)} returns a non-negative 3290 * value. 3291 * 3292 * @param other 3293 * {@code Decimal} to which this {@code Decimal} is to be 3294 * compared 3295 * @return true if {@code this >= other} 3296 */ 3297 boolean isGreaterThanOrEqualTo(Decimal<S> other); 3298 3299 /** 3300 * Compares this {@code Decimal} with the specified {@code Decimal} and 3301 * returns true if this Decimal is numerically less than {@code other}. 3302 * <p> 3303 * Returns true iff {@link #compareTo(Decimal)} returns a negative value. 3304 * 3305 * @param other 3306 * {@code Decimal} to which this {@code Decimal} is to be 3307 * compared. 3308 * @return true if {@code this < other} 3309 */ 3310 boolean isLessThan(Decimal<S> other); 3311 3312 /** 3313 * Compares this {@code Decimal} with the specified {@code Decimal} and 3314 * returns true if this Decimal is numerically less than or equal to 3315 * {@code other}. 3316 * <p> 3317 * Returns true iff {@link #compareTo(Decimal)} returns a non-positive 3318 * value. 3319 * 3320 * @param other 3321 * {@code Decimal} to which this {@code Decimal} is to be 3322 * compared 3323 * @return true if {@code this <= other} 3324 */ 3325 boolean isLessThanOrEqualTo(Decimal<S> other); 3326 3327 /** 3328 * Returns the minimum of this {@code Decimal} and {@code val}. 3329 * 3330 * @param val 3331 * value with which the minimum is to be computed. 3332 * @return the {@code Decimal} whose value is the lesser of this 3333 * {@code Decimal} and {@code val}. If they are equal, as defined by 3334 * the {@link #compareTo(Decimal) compareTo} method, {@code this} is 3335 * returned. 3336 * @see #compareTo(Decimal) 3337 */ 3338 Decimal<S> min(Decimal<S> val); 3339 3340 /** 3341 * Returns the maximum of this {@code Decimal} and {@code val}. 3342 * 3343 * @param val 3344 * value with which the maximum is to be computed. 3345 * @return the {@code Decimal} whose value is the greater of this 3346 * {@code Decimal} and {@code val}. If they are equal, as defined by 3347 * the {@link #compareTo(Decimal) compareTo} method, {@code this} is 3348 * returned. 3349 * @see #compareTo(Decimal) 3350 */ 3351 Decimal<S> max(Decimal<S> val); 3352 3353 /** 3354 * Returns the average of this {@code Decimal} and {@code val}. The result 3355 * is rounded to the {@link #getScale() scale} of this Decimal using default 3356 * {@link RoundingMode#HALF_UP HALF_UP} rounding. The method is much more 3357 * efficient than an addition and subsequent long division and is guaranteed 3358 * not to overflow. 3359 * <p> 3360 * The returned value is a new instance if this Decimal is an 3361 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3362 * internal state is altered and {@code this} is returned as result now 3363 * representing the outcome of the average operation. 3364 * 3365 * @param val 3366 * value with which the average is to be computed. 3367 * @return <tt>round<sub>HALF_UP</sub>((this + val) / 2)</tt> 3368 */ 3369 Decimal<S> avg(Decimal<S> val); 3370 3371 /** 3372 * Returns the average of this {@code Decimal} and {@code val}. The result 3373 * is rounded to the {@link #getScale() scale} of this Decimal using the 3374 * specified {@code roundingMode}. The method is much more efficient than an 3375 * addition and subsequent long division and is guaranteed not to overflow. 3376 * <p> 3377 * The returned value is a new instance if this Decimal is an 3378 * {@link ImmutableDecimal}. If it is a {@link MutableDecimal} then its 3379 * internal state is altered and {@code this} is returned as result now 3380 * representing the outcome of the average operation. 3381 * 3382 * @param val 3383 * value with which the average is to be computed. 3384 * @param roundingMode 3385 * the rounding mode to use if rounding is necessary 3386 * @return {@code round((this + val) / 2)} 3387 * @throws ArithmeticException 3388 * if {@code roundingMode==UNNECESSARY} and rounding is 3389 * necessary 3390 */ 3391 Decimal<S> avg(Decimal<S> val, RoundingMode roundingMode); 3392 3393 /** 3394 * Returns true if this {@code Decimal} is zero. 3395 * 3396 * @return true if {@code this == 0} 3397 */ 3398 boolean isZero(); 3399 3400 /** 3401 * Returns true if this {@code Decimal} is one. 3402 * 3403 * @return true if {@code this == 1} 3404 */ 3405 boolean isOne(); 3406 3407 /** 3408 * Returns true if this {@code Decimal} is minus one. 3409 * 3410 * @return true if {@code this == -1} 3411 */ 3412 boolean isMinusOne(); 3413 3414 /** 3415 * Returns true if this {@code Decimal} is equal to the smallest positive 3416 * number representable by a Decimal with the current {@link #getScale() 3417 * scale}. 3418 * 3419 * @return true if {@code unscaledValue() == 1} 3420 */ 3421 boolean isUlp(); 3422 3423 /** 3424 * Returns true if this {@code Decimal} is strictly positive. 3425 * 3426 * @return true if {@code this > 0} 3427 */ 3428 boolean isPositive(); 3429 3430 /** 3431 * Returns true if this {@code Decimal} is not negative. 3432 * 3433 * @return true if {@code this >= 0} 3434 */ 3435 boolean isNonNegative(); 3436 3437 /** 3438 * Returns true if this {@code Decimal} is negative. 3439 * 3440 * @return true if {@code this < 0} 3441 */ 3442 boolean isNegative(); 3443 3444 /** 3445 * Returns true if this {@code Decimal} is not positive. 3446 * 3447 * @return true if {@code this <= 0} 3448 */ 3449 boolean isNonPositive(); 3450 3451 /** 3452 * Returns true if this {@code Decimal} number is integral, or equivalently 3453 * if its {@link #fractionalPart() fractional part} is zero. 3454 * 3455 * @return true if {@code this} is an integer number 3456 */ 3457 boolean isIntegral(); 3458 3459 /** 3460 * Returns true if the {@link #integralPart() integral part} of this 3461 * {@code Decimal} number is zero. 3462 * 3463 * @return true if {@code -1 < this < 1} 3464 */ 3465 boolean isIntegralPartZero(); 3466 3467 /** 3468 * Returns true if this {@code Decimal} is between zero (inclusive) and one 3469 * (exclusive). The result value is true if and only if this {@code Decimal} 3470 * is not negative and its {@link #integralPart() integral part} is zero. 3471 * 3472 * @return true if {@code 0 <= this < 1} 3473 */ 3474 boolean isBetweenZeroAndOne(); 3475 3476 /** 3477 * Returns true if this {@code Decimal} is between zero (inclusive) and 3478 * minus one (exclusive). The result value is true if and only if this 3479 * {@code Decimal} is not positive and its {@link #integralPart() integral 3480 * part} is zero. 3481 * 3482 * @return true if {@code -1 < this <= 0} 3483 */ 3484 boolean isBetweenZeroAndMinusOne(); 3485 3486 /** 3487 * Compares this {@code Decimal} with the specified {@code Decimal}. Two 3488 * {@code Decimal} objects that are equal in value but have a different 3489 * scale (like 2.0 and 2.00) are considered equal by this method. 3490 * 3491 * @param other 3492 * {@code Decimal} to which this {@code Decimal} is to be 3493 * compared. 3494 * @return the value {@code 0} if this {@code Decimal} is equal to the 3495 * argument {@code Decimal}; a value less than {@code 0} if this 3496 * {@code Decimal} is numerically less than the argument 3497 * {@code Decimal}; and a value greater than {@code 0} if this 3498 * {@code Decimal} is numerically greater than the argument 3499 * {@code Decimal} 3500 * @see #isEqualToNumerically(Decimal) 3501 * @see #compareTo(Decimal) 3502 */ 3503 int compareToNumerically(Decimal<?> other); 3504 3505 /** 3506 * Compares this {@code Decimal} with the specified {@code Decimal} and 3507 * returns true if the two are numerically equal. Two {@code Decimal} 3508 * objects that are equal in value but have a different scale (like 2.0 and 3509 * 2.00) are considered equal by this method as opposed to the 3510 * {@link #equals(Object) equals} method which requires identical scales of 3511 * the compared values. 3512 * <p> 3513 * Returns true iff {@link #compareToNumerically(Decimal)} returns 0. 3514 * 3515 * @param other 3516 * {@code Decimal} to which this {@code Decimal} is to be 3517 * compared. 3518 * @return true if this {@code Decimal} is numerically equal to 3519 * {@code other} and false otherwise. 3520 * @see #compareToNumerically(Decimal) 3521 * @see #compareTo(Decimal) 3522 */ 3523 boolean isEqualToNumerically(Decimal<?> other); 3524 3525 // finally some basic object methods plus equals 3526 3527 /** 3528 * Returns a hash code for this {@code Decimal}. The hash code is calculated 3529 * from {@link #getScale() scale} and {@link #unscaledValue() unscaled 3530 * value}. 3531 * 3532 * @return a hash code value for this object 3533 */ 3534 @Override 3535 int hashCode(); 3536 3537 /** 3538 * Compares this Decimal to the specified object. The result is {@code true} 3539 * if and only if the argument is a {@code Decimal} value with the same 3540 * {@link #getScale() scale} and {@link #unscaledValue() unscaled value} as 3541 * this Decimal. 3542 * 3543 * @param obj 3544 * the object to compare with 3545 * @return {@code true} if the argument is a {@code Decimal} object that 3546 * contains the same value and scale as this object; {@code false} 3547 * otherwise 3548 * 3549 * @see #isEqualTo(Decimal) 3550 * @see #isEqualToNumerically(Decimal) 3551 * @see #hashCode() 3552 */ 3553 @Override 3554 boolean equals(Object obj); 3555 3556 /** 3557 * Returns a string representation of this {@code Decimal} object as fixed 3558 * precision Decimal always showing all Decimal places (also trailing zeros) 3559 * and a leading sign character if negative. 3560 * 3561 * @return a {@code String} Decimal representation of this {@code Decimal} 3562 * object with all the fraction digits (including trailing zeros) 3563 */ 3564 @Override 3565 String toString(); 3566}