Skip to content

Commit 3b246b3

Browse files
committed
update Java version
1 parent f593879 commit 3b246b3

File tree

8 files changed

+360
-241
lines changed

8 files changed

+360
-241
lines changed

src/main/java/com/upokecenter/numbers/EDecimal.java

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* Represents an arbitrary-precision decimal floating-point number. <p><b>About
1212
* decimal arithmetic</b></p> <p> Decimal (base-10) arithmetic, such as
1313
* that provided by this class, is appropriate for calculations
14-
* involving such real-world data as prices, tax rates, and
15-
* measurements. These calculations often involve multiplying or
16-
* dividing one decimal with another decimal, or performing other
14+
* involving such real-world data as prices and other sums of money, tax
15+
* rates, and measurements. These calculations often involve multiplying
16+
* or dividing one decimal with another decimal, or performing other
1717
* operations on decimal numbers. Many of these calculations also rely
1818
* on rounding behavior in which the result after rounding is a decimal
1919
* number (for example, multiplying a price by a premium rate, then
@@ -88,7 +88,11 @@
8888
* class's natural ordering (under the compareTo method) is not
8989
* consistent with the Equals method. This means that two values that
9090
* compare as equal under the compareTo method might not be equal under
91-
* the Equals method.</p>
91+
* the Equals method. The compareTo method compares the mathematical
92+
* values of the two instances passed to it (and considers two different
93+
* NaN values as equal), while two instances with the same mathematical
94+
* value, but different exponents, will be considered unequal under the
95+
* Equals method.</p>
9296
*/
9397
public final class EDecimal implements Comparable<EDecimal> {
9498
private static final int MaxSafeInt = 214748363;
@@ -2075,7 +2079,7 @@ public EDecimal DivideToIntegerNaturalScale(EDecimal
20752079
}
20762080

20772081
/**
2078-
* Removes trailing zeros from this object&#x27;s mantissa. For example, 1.000
2082+
* Removes trailing zeros from this object&#x27;s mantissa. For example, 1.00
20792083
* becomes 1. <p>If this object's value is 0, changes the exponent to
20802084
* 0.</p>
20812085
* @param ctx A precision context to control precision, rounding, and exponent
@@ -2735,18 +2739,34 @@ public EDecimal Add(
27352739
* value's exponent and the desired exponent is too big, depending on
27362740
* the maximum precision. If rounding to a number of decimal places is
27372741
* desired, it's better to use the RoundToExponent and RoundToIntegral
2738-
* methods instead.</p>
2739-
* @param desiredExponent An arbitrary-precision integer.
2742+
* methods instead.</p> <p><b>Remark:</b> This method can be used to
2743+
* implement fixed-point decimal arithmetic, in which each decimal
2744+
* number has a fixed number of digits after the decimal point. The
2745+
* following code example returns a fixed-point number with up to 20
2746+
* digits before and exactly 5 digits after the decimal point:</p>
2747+
* <code> // After performing arithmetic operations, adjust // the
2748+
* number to 5 digits after the decimal point number = number.Quantize(
2749+
* EInteger.FromInt32(-5), // five digits after the decimal point
2750+
* PrecisionContext.ForPrecision(25) // 25-digit precision); </code><p>
2751+
* A fixed-point decimal arithmetic in which no digits come after the
2752+
* decimal point (a desired exponent of 0) is considered an "integer
2753+
* arithmetic". </p>
2754+
* @param desiredExponent The desired exponent for the result. The exponent is
2755+
* the number of fractional digits in the result, expressed as a
2756+
* negative number. Can also be positive, which eliminates lower-order
2757+
* places from the number. For example, -3 means round to the thousandth
2758+
* (10^-3, 0.0001), and 3 means round to the thousand (10^3, 1000). A
2759+
* value of 0 rounds the number to an integer.
27402760
* @param ctx A precision context to control precision and rounding of the
27412761
* result. If HasFlags of the context is true, will also store the flags
27422762
* resulting from the operation (the flags are in addition to the
27432763
* pre-existing flags). Can be null, in which case the default rounding
27442764
* mode is HalfEven.
27452765
* @return A decimal number with the same value as this object but with the
27462766
* exponent changed. Signals FlagInvalid and returns not-a-number (NaN)
2747-
* if the rounded result can't fit the given precision, or if the
2748-
* context defines an exponent range and the given exponent is outside
2749-
* that range.
2767+
* if this object is infinity, if the rounded result can't fit the given
2768+
* precision, or if the context defines an exponent range and the given
2769+
* exponent is outside that range.
27502770
*/
27512771
public EDecimal Quantize(
27522772
EInteger desiredExponent,
@@ -2757,12 +2777,24 @@ public EDecimal Quantize(
27572777
}
27582778

27592779
/**
2760-
* Returns a decimal number with the same value as this one but a new exponent.
2761-
* @param desiredExponentSmall A 32-bit signed integer.
2762-
* @param rounding The parameter {@code rounding} is not documented yet.
2780+
* Returns a decimal number with the same value as this one but a new
2781+
* exponent.<p><b>Remark:</b> This method can be used to implement
2782+
* fixed-point decimal arithmetic, in which a fixed number of digits
2783+
* come after the decimal point. A fixed-point decimal arithmetic in
2784+
* which no digits come after the decimal point (a desired exponent of
2785+
* 0) is considered an "integer arithmetic".</p>
2786+
* @param desiredExponentSmall The desired exponent for the result. The
2787+
* exponent is the number of fractional digits in the result, expressed
2788+
* as a negative number. Can also be positive, which eliminates
2789+
* lower-order places from the number. For example, -3 means round to
2790+
* the thousandth (10^-3, 0.0001), and 3 means round to the thousand
2791+
* (10^3, 1000). A value of 0 rounds the number to an integer.
2792+
* @param rounding A rounding mode to use in case the result needs to be
2793+
* rounded to fit the given exponent.
27632794
* @return A decimal number with the same value as this object but with the
2764-
* exponent changed. Returns not-a-number (NaN) if the rounding mode is
2765-
* Rounding.Unnecessary and the result is not exact.
2795+
* exponent changed. Returns not-a-number (NaN) if this object is
2796+
* infinity, or if the rounding mode is ERounding.None and the result is
2797+
* not exact.
27662798
*/
27672799
public EDecimal Quantize(
27682800
int desiredExponentSmall,
@@ -2779,7 +2811,18 @@ public EDecimal Quantize(
27792811
* value's exponent and the desired exponent is too big, depending on
27802812
* the maximum precision. If rounding to a number of decimal places is
27812813
* desired, it's better to use the RoundToExponent and RoundToIntegral
2782-
* methods instead.</p>
2814+
* methods instead.</p> <p><b>Remark:</b> This method can be used to
2815+
* implement fixed-point decimal arithmetic, in which each decimal
2816+
* number has a fixed number of digits after the decimal point. The
2817+
* following code example returns a fixed-point number with up to 20
2818+
* digits before and exactly 5 digits after the decimal point:</p>
2819+
* <code> // After performing arithmetic operations, adjust // the
2820+
* number to 5 digits after the decimal point number = number.Quantize(
2821+
* -5, // five digits after the decimal point
2822+
* PrecisionContext.ForPrecision(25) // 25-digit precision); </code><p>
2823+
* A fixed-point decimal arithmetic in which no digits come after the
2824+
* decimal point (a desired exponent of 0) is considered an "integer
2825+
* arithmetic". </p>
27832826
* @param desiredExponentSmall The desired exponent for the result. The
27842827
* exponent is the number of fractional digits in the result, expressed
27852828
* as a negative number. Can also be positive, which eliminates
@@ -2793,9 +2836,9 @@ public EDecimal Quantize(
27932836
* mode is HalfEven.
27942837
* @return A decimal number with the same value as this object but with the
27952838
* exponent changed. Signals FlagInvalid and returns not-a-number (NaN)
2796-
* if the rounded result can't fit the given precision, or if the
2797-
* context defines an exponent range and the given exponent is outside
2798-
* that range.
2839+
* if this object is infinity, if the rounded result can't fit the given
2840+
* precision, or if the context defines an exponent range and the given
2841+
* exponent is outside that range.
27992842
*/
28002843
public EDecimal Quantize(
28012844
int desiredExponentSmall,
@@ -2813,6 +2856,11 @@ public EDecimal Quantize(
28132856
* the desired exponent is too big, depending on the maximum precision.
28142857
* If rounding to a number of decimal places is desired, it's better to
28152858
* use the RoundToExponent and RoundToIntegral methods instead.</p>
2859+
* <p><b>Remark:</b> This method can be used to implement fixed-point
2860+
* decimal arithmetic, in which a fixed number of digits come after the
2861+
* decimal point. A fixed-point decimal arithmetic in which no digits
2862+
* come after the decimal point (a desired exponent of 0) is considered
2863+
* an "integer arithmetic".</p>
28162864
* @param otherValue A decimal number containing the desired exponent of the
28172865
* result. The mantissa is ignored. The exponent is the number of
28182866
* fractional digits in the result, expressed as a negative number. Can
@@ -3147,39 +3195,14 @@ public EDecimal Plus(EContext ctx) {
31473195
return GetMathValue(ctx).Plus(this, ctx);
31483196
}
31493197

3150-
/**
3151-
* Rounds this object&#x27;s value to a given maximum bit length, using the
3152-
* given rounding mode and range of exponent.
3153-
* @param ctx A context for controlling the precision, rounding mode, and
3154-
* exponent range. The precision is interpreted as the maximum bit
3155-
* length of the mantissa. Can be null.
3156-
* @return The closest value to this object's value, rounded to the specified
3157-
* precision. Returns the same value as this object if {@code ctx} is
3158-
* null or the precision and exponent range are unlimited.
3159-
* @deprecated Instead of this method use RoundToPrecision and pass a precision context
3160-
* with the IsPrecisionInBits property set.
3161-
*/
3162-
@Deprecated
3163-
public EDecimal RoundToBinaryPrecision(EContext ctx) {
3164-
if (ctx == null) {
3165-
return this;
3166-
}
3167-
EContext ctx2 = ctx.Copy().WithPrecisionInBits(true);
3168-
EDecimal ret = GetMathValue(ctx).RoundToPrecision(this, ctx2);
3169-
if (ctx2.getHasFlags()) {
3170-
ctx.setFlags(ctx2.getFlags());
3171-
}
3172-
return ret;
3173-
}
3174-
31753198
/**
31763199
* Finds the square root of this object&#x27;s value.
31773200
* @param ctx A precision context to control precision, rounding, and exponent
31783201
* range of the result. If HasFlags of the context is true, will also
31793202
* store the flags resulting from the operation (the flags are in
3180-
* addition to the pre-existing flags). --This parameter cannot be null,
3181-
* as the square root function's results are generally not exact for
3182-
* many inputs.--.
3203+
* addition to the pre-existing flags). <i>This parameter cannot be
3204+
* null, as the square root function's results are generally not exact
3205+
* for many inputs.</i>
31833206
* @return The square root. Signals the flag FlagInvalid and returns NaN if
31843207
* this object is less than 0 (the square root would be a complex
31853208
* number, but the return value is still NaN). Signals FlagInvalid and
@@ -3196,8 +3219,9 @@ public EDecimal SquareRoot(EContext ctx) {
31963219
* @param ctx A precision context to control precision, rounding, and exponent
31973220
* range of the result. If HasFlags of the context is true, will also
31983221
* store the flags resulting from the operation (the flags are in
3199-
* addition to the pre-existing flags). --This parameter cannot be null,
3200-
* as the exponential function's results are generally not exact.--.
3222+
* addition to the pre-existing flags). <i>This parameter cannot be
3223+
* null, as the exponential function's results are generally not
3224+
* exact.</i>
32013225
* @return Exponential of this object. If this object's value is 1, returns an
32023226
* approximation to " e" within the given precision. Signals FlagInvalid
32033227
* and returns not-a-number (NaN) if the parameter {@code ctx} is null
@@ -3215,8 +3239,8 @@ public EDecimal Exp(EContext ctx) {
32153239
* @param ctx A precision context to control precision, rounding, and exponent
32163240
* range of the result. If HasFlags of the context is true, will also
32173241
* store the flags resulting from the operation (the flags are in
3218-
* addition to the pre-existing flags). --This parameter cannot be null,
3219-
* as the ln function's results are generally not exact.--.
3242+
* addition to the pre-existing flags). <i>This parameter cannot be
3243+
* null, as the ln function's results are generally not exact.</i>
32203244
* @return Ln(this object). Signals the flag FlagInvalid and returns NaN if
32213245
* this object is less than 0 (the result would be a complex number with
32223246
* a real part equal to Ln of this object's absolute value and an
@@ -3237,8 +3261,8 @@ public EDecimal Log(EContext ctx) {
32373261
* @param ctx A precision context to control precision, rounding, and exponent
32383262
* range of the result. If HasFlags of the context is true, will also
32393263
* store the flags resulting from the operation (the flags are in
3240-
* addition to the pre-existing flags). --This parameter cannot be null,
3241-
* as the ln function's results are generally not exact.--.
3264+
* addition to the pre-existing flags). <i>This parameter cannot be
3265+
* null, as the ln function's results are generally not exact.</i>
32423266
* @return Ln(this object)/Ln(10). Signals the flag FlagInvalid and returns
32433267
* not-a-number (NaN) if this object is less than 0. Signals FlagInvalid
32443268
* and returns not-a-number (NaN) if the parameter {@code ctx} is null
@@ -3292,13 +3316,13 @@ public EDecimal Pow(int exponentSmall) {
32923316
}
32933317

32943318
/**
3295-
* Finds the constant pi.
3319+
* Finds the constant &#x3c0;.
32963320
* @param ctx A precision context to control precision, rounding, and exponent
32973321
* range of the result. If HasFlags of the context is true, will also
32983322
* store the flags resulting from the operation (the flags are in
3299-
* addition to the pre-existing flags). --This parameter cannot be null,
3300-
* as pi can never be represented exactly.--.
3301-
* @return Pi rounded to the given precision. Signals FlagInvalid and returns
3323+
* addition to the pre-existing flags). <i>This parameter cannot be
3324+
* null, as &#x3c0; can never be represented exactly.</i>
3325+
* @return π rounded to the given precision. Signals FlagInvalid and returns
33023326
* not-a-number (NaN) if the parameter {@code ctx} is null or the
33033327
* precision is unlimited (the context's Precision property is 0).
33043328
*/

0 commit comments

Comments
 (0)