What's the prospect of getting native/literal decimal type support in the future?
Regardless of the JDK runtime optimizations underneath, BigDecimal is clunky and error-prone* to work with, making Java look really bad. Seems like low-hanging frontend syntax-sugar fruit that would actually cater to a great number of active Java practitioners.* It's a leaky abstraction that compareTo(..) ignores scale so in spite of being a numeric type, 0 != 0.0.
BD's have go be constructed using Strings: BD's
"add" does not actually add/mutate anything so it really should've been named "plus" etc.
/Casper
On Wednesday, March 14, 2012 3:05:56 AM UTC+1, jddarcy wrote:While listening to episode Java Posse #379, Roundup '11 - ReSTful AMF, some of the technical details of BigDecimal came up and I wanted to address some erroneous statements.
First, logically a BigDecimal is a tuple of an arbitrary precision unscaled integer value and a 32-bit integer exponent where the numerical value of the BigDecimal is equal to
unscaledValue * 10^exponent
The decimal-ness of BigDecimal comes from using 10 as an exponent and not from how unscaledValue is represented. In particular, BigDecimal does not use a decimal digit-oriented representation internally; instead it uses a BigInteger. For that reason, multiplies of BigDecimal values are comparatively faster than addition/subtraction because the decimal digit alignment shifts to line up corresponding digits positions in add/subtract are relatively expensive with a binary-based representation.
In addition, since JDK5 update 5, small unscaled values have been represented internally using a 64-bit long rather than a BigInteger, giving a considerable performance boost for such values, up to about 18 decimal digits of precision.
--
You received this message because you are subscribed to the Google Groups "The Java Posse" group.
To view this discussion on the web visit https://groups.google.com/d/msg/javaposse/-/Q4wo1DlNsJIJ.
To post to this group, send email to java...@googlegroups.com.
To unsubscribe from this group, send email to javaposse+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
Certainly technically feasible, but not under consideration for the set of features being contemplated for JDK 8:
http://openjdk.java.net/jeps/0
Yes, that is the documented behavior of compareTo; however, note that such numbers are *not* .equals with each other.
No; there are BigDecimal constructors or factories that accept BigInteger, int, or long arguments for the unscaled value including:
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#BigDecimal(java.math.BigInteger,%20int)
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#valueOf(long,%20int)
I see no connection between mutation and the name "add", likewise for "subtract" and "multiply". The name "setScale" however is certainly misleading.
"add" does not actually add/mutate anything so it really should've been named "plus" etc.
I see no connection between mutation and the name "add", likewise for "subtract" and "multiply". The name "setScale" however is certainly misleading.
myFriends.add("Fred") vs. myFriends.plus("Fred")
I think that might be a difference between UK and US English. Specifically, I think Americans are more likely to pronounce + as add than as plus in 3 + 2.
--
You received this message because you are subscribed to the Google Groups "The Java Posse" group.
Unless you program in COBOL.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel...@ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
I think that most native English speakers *will* make a distinction between "x add y" and "x plus y"Consider how it sounds when not referring to numbers:myFriends.add("Fred") vs. myFriends.plus("Fred")The first form would normally be read as updating my friends to include Fred. The second form would normally be read as the set comprising both my friends and Fred (who isn't necessarily my friend).
Certainly technically feasible, but not under consideration for the set of features being contemplated for JDK 8:
http://openjdk.java.net/jeps/0Which is odd, considering C# (Java's main competitor) had it since its inception and given it's also on its way into C++ via ISO/IEC TR 24733.Yes, that is the documented behavior of compareTo; however, note that such numbers are *not* .equals with each other.
True, but that does little in a common arithmetic algorithmic branching scenario.No; there are BigDecimal constructors or factories that accept BigInteger, int, or long arguments for the unscaled value including:
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#BigDecimal(java.math.BigInteger,%20int)
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#valueOf(long,%20int)Yes but that won't help constructing decimals, where the text for the BigDecimal(double) constructor goes "The results of this constructor can be somewhat unpredictable and its use is generally not recommended" which kind of leaves a cliffhanger with the developer.
double into a BigDecimal which
is the exact decimal representation of the double's
binary floating-point value. The scale of the returned
BigDecimal is the smallest value such that
(10scale × val) is an integer.
Notes:
new BigDecimal(0.1) in
Java creates a BigDecimal which is exactly equal to
0.1 (an unscaled value of 1, with a scale of 1), but it is
actually equal to
0.1000000000000000055511151231257827021181583404541015625.
This is because 0.1 cannot be represented exactly as a
double (or, for that matter, as a binary fraction of
any finite length). Thus, the value that is being passed
in to the constructor is not exactly equal to 0.1,
appearances notwithstanding.
String constructor, on the other hand, is
perfectly predictable: writing new BigDecimal("0.1")
creates a BigDecimal which is exactly equal to
0.1, as one would expect. Therefore, it is generally
recommended that the String constructor be used in preference to this one.
double must be used as a source for a
BigDecimal, note that this constructor provides an
exact conversion; it does not give the same result as
converting the double to a String using the
Double.toString(double) method and then using the
BigDecimal(String) constructor. To get that result,
use the static valueOf(double) method.
--
You received this message because you are subscribed to the Google Groups "The Java Posse" group.
So, should Java add PICTURE subclasses of Number? Should it have
(BIN|DEC) (FIXED|FLOAT) types? It's been a long time since I took
my freshman PL/I class.