From: Chad Perrin Date: 2012-01-30T14:56:52+09:00 Subject: Re: Microrant on Ruy's Math Skills On Mon, Jan 30, 2012 at 10:03:04AM +0900, Gary Wright wrote: > > On Jan 29, 2012, at 2:26 AM, Jon Lambert wrote: > > > On Jan 27, 2012, at 3:26 PM, Gary Wright wrote: > >> What 'value' do you expect for this expression: > >> > >> BigDecimal("1.0") / BigDecimal("3.0") > > > > Decimal math operations need use a default rounding context or require one to set a > > rounding context before performing an operation. > > > > Do you round towards +infinity, -infinity, > > towards 0, away from 0, > > to nearest (and if equidistant, round down), > > to nearest (and if equidistant, round up), or > > to nearest (and if equidistant, round so that the last digit is even) aka. bankers rounding? > > > > Only then can you know what to expect. > > Agree 100%. I was just trying to illustrate that even if you have some > wonderfully crafted big decimal literal syntax, you still need to address > the context issues you just listed. > > 1.0D / 3.0D > > Might be easier to type but it is only part (perhaps a small part) of the puzzle. The point of offering something other than "all you get is floats and libraries", of course, is that a simple set of such rules is a lot easier to deal with than the floating point mess that is common now. It's a lot easier to deal with "it truncates decimals past the Nth place" or any other such simple rule than "Uh . . . it's probably going to do some weird shit when you start doing math -- weird shit that requires you to do binary math to predict the outcome. You really just shouldn't use it. Use some verbose library syntax instead. You like typing -- right?" -- Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]