Package eu.simuline.util
Class RealRepresentation
- java.lang.Object
-
- eu.simuline.util.RealRepresentation
-
- Direct Known Subclasses:
RealRepresentation.BigDecimalRep,RealRepresentation.DoubleRep
public class RealRepresentation extends Object
Describes the representation of a real value, which may be either a double, resp. a Double-value or aBigDecimalfor use in tables. At a rudimentary stage,RealRepresentationwraps the number whereasRealRepresentation.Descdescribes how to format it, e.g. where to add blanks.Also
asInteger()provides some support for formatting integers. These are the two supported levels of precision. It is characterized by themantissa()and theexponent. The mantissa in turn splits into theintegerand thefractional part. The essential point is that these three parts are properly aligned and filled up with0's and blanks by need.The splitting between integer, fraction and exponent is as specified for
Double.toString(double). The difference between the two levels of precision is made as transparent as possible. Transparence is given up in (at least) the following cases:-
unlike
BigDecimalsdoubles (influenced by the standard IEEE 754), distinguish between+0and-0. -
for doubles by specification of the string representation,
"There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0. "
This does not make sense forBigDecimalsbecause there is no nearestBigDecimal: TheBigDecimals are "almost" dense in the real numbers. Instead we neglect any trailing zeros. In later versions, this should be different.
double's andNaNare not supported. This is because they have no splitting into integer, fractional part and exponent. They do not make sense forBigDecimals and so this would make transparency more difficult. Note that denormalization ofdouble's does not affect their string representation such that this is completely as forforBigDecimals for which automatic denormalization does not make sense (although can be forced by the user).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classRealRepresentation.BigDecimalRepDescribes the representation of aBigDecimal-value.static interfaceRealRepresentation.CutterACutteris used to shorten numbers.static classRealRepresentation.DescDescribes the way the real number represented byRealRepresentationis displayed as a string.private static classRealRepresentation.DoubleRepDescribes the representation of a double, resp. a Double-value.private static classRealRepresentation.Number2SignUnsignedContainer for number splitted into sign and unsigned.
-
Field Summary
Fields Modifier and Type Field Description static RealRepresentation.CutterALIGN_CUT_OFF_LEFTDescribe the action to be taken if a sequence of symbols, e.g. digits are longer as needed.static RealRepresentation.CutterALIGN_CUT_OFF_RIGHTDescribe the action to be taken if a sequence of symbols, e.g. digits are longer as needed.static RealRepresentation.CutterALIGN_EXCEPTIONDescribe the action to be taken if a sequence of symbols, e.g. digits are longer as needed.static RealRepresentation.CutterALIGN_LEAVE_UNCHANGEDDescribe the action to be taken if a sequence of symbols, e.g. digits are longer as desired.static StringEXP_CHARA string containing the character signifying an exponent.protected StringexponentThe exponent of the double value represented.private static Map<Character,String>FILL_STRINGSThe keys are Characters (intended for either " " or "0") and the associated values are strings consisting of a number of characters given by the key.protected StringfractionThe fractional part of the mantissa of the double value represented.protected StringintegerThe integer part of the mantissa of the double value represented.private static StringnoPointSSubstitutes the place for the point in case the real number represented has no fractional part.private static StringPOINT_SA string constant containing the decimal point.protected StringsignThe sign of the double value represented.private static StringSIGN_MINUSA character constant containing+.private static StringSIGN_PLUSA character constant containing+.private StringsignOfExpprivate StringunsignedExp
-
Constructor Summary
Constructors Constructor Description RealRepresentation()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StringalignLeft(String str, int len, char filler, RealRepresentation.Cutter cutter)Returns aStringwhich is by attaching the minimal number offillers to the right hand side ofstrsuch that the length of the result is at leastint.static StringalignRight(String str, int len, char filler, RealRepresentation.Cutter cutter)Returns aStringwhich is by attaching the minimal number offillers to the left hand side ofstrsuch that the length of the result is at leastint.voidasInteger()static voidcompensatePoint(boolean compensate)static RealRepresentationcreate(double val, boolean precision)Converts the givendoublenumber to aRealRepresentationwith the given precision.static RealRepresentationcreate(Number val)Converts the given number to aRealRepresentationwith the natural precision.static RealRepresentationcreate(Number val, boolean precision)Converts the given high precision representation of a real number to aRealRepresentationpreserving full precision if so specified.static RealRepresentationcreate(String val, boolean precision)Converts the given string representation of a real number to aRealRepresentationwith the given precision.Stringexponent()static Stringfill(char chr0, int len)Returns aStringconsisting oflencopies of the characterc.Stringfraction()private StringgetExpWithE()booleanhasBlankFraction()booleanhasExponent()booleanhasFraction()booleanhasInteger()protected voidinitSignExp()Stringinteger()Stringmantissa()Stringsign()BigDecimalthis2BigDecimal()Returns aBigDecimalrepresented.doublethis2double()Returns adoublerepresented.Doublethis2Double()Returns aDoublerepresented.StringtoString()Returns a String representation of this real number.StringtoString(RealRepresentation.Desc desc)StringtoStringDecomp()Returns a string showing the decomposition of the underlying real number into sign, mantissa and exponent.StringtrimExponent(int numDigits, char blankOrNull, boolean strict)Trims the exponent part to lengthnumDigitsif possible.StringtrimFraction(int numDigits, char blankOrNull, RealRepresentation.Cutter cutter)Trims the fractional part to lengthnumDigitsif possible.StringtrimInteger(int numDigits, char blankOrNull, boolean strict)Trims the integer part to lengthnumDigitsif possible.
-
-
-
Field Detail
-
ALIGN_CUT_OFF_LEFT
public static final RealRepresentation.Cutter ALIGN_CUT_OFF_LEFT
Describe the action to be taken if a sequence of symbols, e.g. digits are longer as needed.
-
ALIGN_CUT_OFF_RIGHT
public static final RealRepresentation.Cutter ALIGN_CUT_OFF_RIGHT
Describe the action to be taken if a sequence of symbols, e.g. digits are longer as needed.
-
ALIGN_EXCEPTION
public static final RealRepresentation.Cutter ALIGN_EXCEPTION
Describe the action to be taken if a sequence of symbols, e.g. digits are longer as needed.
-
ALIGN_LEAVE_UNCHANGED
public static final RealRepresentation.Cutter ALIGN_LEAVE_UNCHANGED
Describe the action to be taken if a sequence of symbols, e.g. digits are longer as desired.
-
FILL_STRINGS
private static final Map<Character,String> FILL_STRINGS
The keys are Characters (intended for either " " or "0") and the associated values are strings consisting of a number of characters given by the key.- See Also:
fill(char, int)
-
EXP_CHAR
public static final String EXP_CHAR
A string containing the character signifying an exponent. This isebut could also beE.- See Also:
- Constant Field Values
-
noPointS
private static String noPointS
Substitutes the place for the point in case the real number represented has no fractional part. This is either " " or "". The default value is " ".- See Also:
compensatePoint(boolean)
-
POINT_S
private static final String POINT_S
A string constant containing the decimal point.- See Also:
- Constant Field Values
-
SIGN_PLUS
private static final String SIGN_PLUS
A character constant containing+.- See Also:
- Constant Field Values
-
SIGN_MINUS
private static final String SIGN_MINUS
A character constant containing+.- See Also:
- Constant Field Values
-
sign
protected String sign
The sign of the double value represented. This may be either+or-. Note that the standard IEEE 754 distinguishes-0from+0and so do we in the subclassRealRepresentation.DoubleRep.
-
integer
protected String integer
The integer part of the mantissa of the double value represented.
-
fraction
protected String fraction
The fractional part of the mantissa of the double value represented. This may never be empty but it may be0.
-
exponent
protected String exponent
The exponent of the double value represented.
-
signOfExp
private String signOfExp
-
unsignedExp
private String unsignedExp
-
-
Method Detail
-
compensatePoint
public static void compensatePoint(boolean compensate)
SetsnoPointSaccording to the parameter:truemeans thatnoPointSis attached with " "; otherwise it is "".- Parameters:
compensate- abooleandeciding whether to compensate a decimal point in case the real number represented has no fractional part.
-
initSignExp
protected final void initSignExp()
-
create
public static RealRepresentation create(String val, boolean precision)
Converts the given string representation of a real number to aRealRepresentationwith the given precision.- Parameters:
val- aStringrepresentation of a real number.precision- abooleanwhich offers choice between full precision (true) and double precision (false).- Returns:
- a
RealRepresentationof the real value given by the string representationvalwith the precision determined byprecision. - Throws:
NumberFormatException- ifvalis not the string representation of a double or if it isNaNor represents an infinite value.
-
create
public static RealRepresentation create(Number val, boolean precision)
Converts the given high precision representation of a real number to aRealRepresentationpreserving full precision if so specified.- Parameters:
val- aNumbervalue which is either aBigDecimalor aDouble.precision- specifies whether to preserve full precision. If set tofalseonlydoubleprecision is used.- Returns:
- a
RealRepresentationof the real value given byvalpreserving full precision is specified so. - Throws:
ClassCastException- ifvalis neither aBigDecimalnor aDouble.
-
create
public static RealRepresentation create(double val, boolean precision)
Converts the givendoublenumber to aRealRepresentationwith the given precision.- Parameters:
val- adoublevalue.precision- abooleanwhich offers choice between full precision (truewhich is obsolete****) and double precision (false).- Returns:
- a
RealRepresentationof the givendoublewith the precision determined byprecision. - Throws:
NumberFormatException- ifvalis eitherNaNor infinite.
-
create
public static RealRepresentation create(Number val)
Converts the given number to aRealRepresentationwith the natural precision.- Parameters:
val- aNumbervalue which is either aBigDecimalor aDouble.- Returns:
- a
RealRepresentationof the given number with the natural precision:create((BigDecimal)val, true)orcreate((Double )val, true). - Throws:
ClassCastException- ifvalis neither aBigDecimalnor aDouble.
-
fill
public static String fill(char chr0, int len)
Returns aStringconsisting oflencopies of the characterc.- Parameters:
chr0- achar.len- the number of repetitions ofcneeded. This should be a non-negative integer.- Returns:
- a
Stringconsisting oflencharactersc.
-
alignLeft
public static String alignLeft(String str, int len, char filler, RealRepresentation.Cutter cutter)
Returns aStringwhich is by attaching the minimal number offillers to the right hand side ofstrsuch that the length of the result is at leastint. Forstrict == trueeven equality is assured.- Parameters:
str- theStringto be aligned.len- anintvalue which determines the length of the result string:filler- acharvalue.cutter- takes effect ifstr.length > len:-
for
ALIGN_CUT_OFF_RIGHTsuperfluous digits are cut off, - {
for
ALIGN_EXCEPTIONsuperfluous digits cause an exception, -
for
ALIGN_LEAVE_UNCHANGEDsuperfluous digits are left unchanged,
-
for
- Returns:
- for
str.length()≤lencopies offillers are attached to the right hand side ofstrsuch that the length of the result is leastint. - for
str.length()>len-
stris returned as is providedcutter ==ALIGN_LEAVE_UNCHANGED -
For
cutter ==there is no return value: an exception is thrown and finallyALIGN_EXCEPTION -
for
cutter ==the overhead is silently cut off.ALIGN_CUT_OFF_RIGHT
-
- for
- Throws:
IllegalArgumentException- forcutter ==ifALIGN_EXCEPTIONstr.length()exceedslen.
-
alignRight
public static String alignRight(String str, int len, char filler, RealRepresentation.Cutter cutter)
Returns aStringwhich is by attaching the minimal number offillers to the left hand side ofstrsuch that the length of the result is at leastint. Forstrict == trueeven equality is assured.- Parameters:
str- theStringto be aligned.len- anintvalue which determines the length of the result string:filler- acharvalue.cutter- takes effect ifstr.length > len: { {-
for
ALIGN_CUT_OFF_LEFTsuperfluous digits are cut off, -
for
ALIGN_EXCEPTIONsuperfluous digits cause an exception, -
for
ALIGN_LEAVE_UNCHANGEDsuperfluous digits are left unchanged,
-
for
- Returns:
- for
str.length()≤lencopies offillers are attached to the right hand side ofstrsuch that the length of the result is leastint. - for
str.length()>len-
stris returned as is providedcutter ==ALIGN_LEAVE_UNCHANGED -
For
cutter ==there is no return value: an exception is thrown and finallyALIGN_EXCEPTION -
for
cutter ==the overhead is silently cut off.ALIGN_CUT_OFF_LEFT
-
- for
- Throws:
IllegalArgumentException- forcutter ==ifALIGN_EXCEPTIONstr.length()exceedslen.
-
trimInteger
public final String trimInteger(int numDigits, char blankOrNull, boolean strict)
Trims the integer part to lengthnumDigitsif possible. If this leads to a prolongation of the integer part, it is filled up with charactersblankOrNullfrom the left hand side. If the integer part is already longer than specified, the action taken relies on the parameterstrict. Caution: ***** this does not work very well fornumDigits = 0. Caution: **** not ok with signs. Caution: **** what to do if ".4" is needed?- Parameters:
numDigits- a non-negativeintvalue signifying the length of the integer part.blankOrNull- acharwith which the integer part is filled up by need from the left hand side to reach lengthnumDigits. Useful settings are probably blank and'0'only.strict- abooleansignifying whether an attempt to cut off parts of the integer will result in an exception; otherwise it is just ignored.- Returns:
- the new integer part as a
String. - Throws:
IllegalArgumentException- forstrict( == true)if the length of the integer part exceedsnumDigits.
-
trimExponent
public final String trimExponent(int numDigits, char blankOrNull, boolean strict)
Trims the exponent part to lengthnumDigitsif possible. If this leads to a prolongation of the exponent part, it is filled up with charactersblankOrNullfrom the left hand side. If the exponent part is already longer than specified, the action taken relies on the parameterstrict. Caution: ***** this does not work very well fornumDigits = 0. Caution: **** not ok with signs. Caution: **** not ok if there are no exponents at all.- Parameters:
numDigits- a non-negativeintvalue signifying the length of the exponent part.blankOrNull- acharwith which the exponent part is filled up by need from the left hand side to reach lengthnumDigits. Useful settings are probably blank and'0'only.strict- abooleansignifying whether an attempt to cut off parts of the exponent will result in an exception; otherwise it is just ignored.- Returns:
- the new exponent as a
String. - Throws:
IllegalArgumentException- forstrict( == true)if the length of the exponent part exceedsnumDigits.
-
trimFraction
public final String trimFraction(int numDigits, char blankOrNull, RealRepresentation.Cutter cutter)
Trims the fractional part to lengthnumDigitsif possible. If this leads to a prolongation of the fractional part, it is filled up with charactersblankOrNull. If the fractional part is already longer than specified, the action taken relies on the codecutter. Caution: ***** this does not work very well fornumDigits = 0. Caution: **** this method simply cuts off digits without rounding.- Parameters:
numDigits- a non-negativeintvalue signifying the length of the fractional part.blankOrNull- acharwith which the fractional part is filled up by need to reach lengthnumDigits. Useful settings are probably'0'and blank only.cutter- anintcode which is relevant only, if the current fractional part is longer than specified bynumDigits.- Returns:
- the new trimmed fractional part as a
String. - Throws:
IllegalArgumentException- forcutter ==if the length of the fractional part exceedsALIGN_EXCEPTIONnumDigits.
-
asInteger
public final void asInteger()
Converts this representation such that there is neither afractionnor anexponent. Of course in general thereby the current format is lost.- Throws:
NumberFormatException- if this representation cannot be written as an integer.
-
hasBlankFraction
public final boolean hasBlankFraction()
-
hasFraction
public final boolean hasFraction()
-
hasInteger
public final boolean hasInteger()
-
hasExponent
public final boolean hasExponent()
-
sign
public final String sign()
-
exponent
public final String exponent()
-
mantissa
public final String mantissa()
-
integer
public final String integer()
-
fraction
public final String fraction()
-
this2Double
public final Double this2Double()
Returns aDoublerepresented.- Returns:
- a
Doubledsatisfyingcreate(d, false).this2Double().compareTo(d) == 0.
-
this2double
public final double this2double()
Returns adoublerepresented.- Returns:
- a
doubledsatisfyingcreate(d, false).this2double() == d.
-
this2BigDecimal
public final BigDecimal this2BigDecimal()
Returns aBigDecimalrepresented.- Returns:
- a
BigDecimaldsatisfyingcreate(d, true).this2BigDecimal().compareTo(d) == 0.
-
getExpWithE
private String getExpWithE()
-
toString
public final String toString()
Returns a String representation of this real number. Formatting is determined by previous invocations of the methodstrimInteger(int, char, boolean),trimFraction(int, char, eu.simuline.util.RealRepresentation.Cutter)andtrimExponent(int, char, boolean).- Overrides:
toStringin classObject- Returns:
- a
Stringreflecting this representation of a real number. In front of the sign (which may be empty), between the sign and the integer part, between the fractional part and the exponent or the end of the string and also between the ***** - See Also:
toStringDecomp()
-
toString
public final String toString(RealRepresentation.Desc desc)
-
toStringDecomp
public final String toStringDecomp()
Returns a string showing the decomposition of the underlying real number into sign, mantissa and exponent.- Returns:
- a
Stringvalue - See Also:
toString()
-
-