Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: [OT] Re: missing import java.lang.Comparable Date: Mon, 11 Jul 2016 22:30:14 -0400 Organization: Aioe.org NNTP Server Lines: 79 Message-ID: References: <128e7e20-d311-413d-abbc-e885b34ae402@googlegroups.com> <87dc5bee-f689-4f2e-90a8-a43a2de9fe49@googlegroups.com> <638a1d64-f125-4456-a20b-4dbf45f49da2@googlegroups.com> <60a5ccde-2657-48d6-b3dd-04ec1a0973ab@googlegroups.com> <6f69c451-849d-4672-9025-7880a62bd72d@googlegroups.com> NNTP-Posting-Host: 7SK6yTFfqwlIPNBLl3l7Qg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:34866 On 7/11/2016 1:56 PM, Eric Douglas wrote: > On Monday, July 11, 2016 at 12:27:04 PM UTC-4, Martin Gregorie > wrote: >> As others have said, use floats or doubles for science and >> engineering and int, long, BigInteger or BigDecimal for >> financials. > > That seems misleading and tedious. When I first started learning > Java I learned about primitives, which I would think you'd want to > use if you can because they're less overhead, Financial calculation that need book keeping accuracy are typical not very computing intensive after todays standard. Just use BigDecimal and forget about the overhead compared to simple types. > but only the float and > double support decimals which seems odd since they both carry the > warning "This data type should never be used for precise values, such > as currency." Some computer hardware and some programming languages allow a data type that we can call fixed point in a hardware close format. Not Java. > or apparently any values which use normal math. In > grade school math we're taught .0002 + .0001 = .0003, so I'm not sure > how it adds up to .00030000000000000003. Floating point and math does not follow the same rules. Because something works in math then it does not necessarily work in floating point. There are plenty of examples. An attempt (hopefully a good one): public class FPFun { public static void main(String[] args) { float a = 12345; float b = 123; float c = (a - b) * (a - b); System.out.printf("%10.1f\n", c); float d = a * a - 2 * a * b + b * b; System.out.printf("%10.1f\n",d); } } 149377280.0 149377296.0 > Oracle even has their own Java based financials software, so I assume > all numeric references in their programs use BigDecimal? Likely. > I'm not > sure how you add decimal values using primitives which only support > whole numbers. You can do some tricks by having an implied number of decimals. > When I'm writing a program that calls a method of my API and needs to > give it a numeric value with a decimal it seems like a lot of extra > overhead to always convert to a String first and pass into a new > BigDecimal to get the parameter into the method. If you do double->String->BigDecimal there is no point - you are already in trouble. Your program should already have it as either String or BigDecimal. No double. Arne