From: Arne Vajhøj on 13 Jul 2010 20:33 On 13-07-2010 07:11, Screamin Lord Byron wrote: > On 07/13/2010 01:02 PM, Screamin Lord Byron wrote: >> just like it prints 0.8999999999999999 as a >> result of 2. - 0.1. > > Typo > > 2. - 1.1 > > Anyway, the result isn't exact, of course, but I don't think it would be > such a good idea if println did rounding to show us "expected" 0.9. In most cases that is probably what people want. Of course they can use printf with a fixed number of decimals in most cases. Arne
From: Screamin Lord Byron on 14 Jul 2010 09:20 On 07/14/2010 01:35 AM, Arne Vajhøj wrote: > On 13-07-2010 07:02, Screamin Lord Byron wrote: >> Am I wrong if I say that the literal 3.141592 is represented in memory >> like this: the sign is 0, the significand is 3141592 and the exponent is >> -6. There are no mathematical operations with this number, so it's sole >> representation is in fact exact, is it not? The println should print it >> like it's represented, just like it prints 0.8999999999999999 as a >> result of 2. - 0.1. > > Yes. > > Double in Java are IEEE 64 bit floating point and they are binary > not decimal, so you have 1/2, 1/4 etc. not 1/10, 1/100 etc.. > > Arne Of course they are and of course I'm wrong. Thanks to all who replied to my post. It's much clearer now. I must admit that what I said earlier was pretty naive thing to say. The more posts I read here, the more I realize how bad my Java training course was. There's nothing related to this even on the SCJP exam, so you can officially be "Java Certified Programmer" and not know anything about floating point. Now how about that.
From: Patricia Shanahan on 14 Jul 2010 09:52 Arne Vajhøj wrote: > On 13-07-2010 17:33, Patricia Shanahan wrote: .... >> There is also a simple pure Java way of getting the actual decimal >> expansion. The BigDecimal(double) constructor and BigDecimal's toString >> are both exact: >> >> import java.math.BigDecimal; >> public class DemoExactDoubleOutput { >> public static void main(String[] args) { >> System.out.println(new BigDecimal(3.141592)); >> } >> } >> >> Output: >> >> 3.14159200000000016217427400988526642322540283203125 > > Cute. > > You are using the well-known flaw in that constructor > as a useful feature! It can even be combined with the String constructor to get an estimate of the relative rounding error: import java.math.BigDecimal; public class TestFraction { public static void main(String[] args) { System.out.println(new BigDecimal(3.141592)); System.out.println(relativeError("3.141592")); } static double relativeError(String s){ double d = Double.valueOf(s); BigDecimal error = new BigDecimal(d).subtract(new BigDecimal(s)).abs(); return error.doubleValue()/d; } } output: 3.14159200000000016217427400988526642322540283203125 5.162168544161217E-17
From: Arne Vajhøj on 14 Jul 2010 21:40 On 14-07-2010 09:20, Screamin Lord Byron wrote: > On 07/14/2010 01:35 AM, Arne Vajhøj wrote: >> On 13-07-2010 07:02, Screamin Lord Byron wrote: >>> Am I wrong if I say that the literal 3.141592 is represented in memory >>> like this: the sign is 0, the significand is 3141592 and the exponent is >>> -6. There are no mathematical operations with this number, so it's sole >>> representation is in fact exact, is it not? The println should print it >>> like it's represented, just like it prints 0.8999999999999999 as a >>> result of 2. - 0.1. >> >> Yes. >> >> Double in Java are IEEE 64 bit floating point and they are binary >> not decimal, so you have 1/2, 1/4 etc. not 1/10, 1/100 etc.. > > Of course they are and of course I'm wrong. Thanks to all who replied to > my post. It's much clearer now. > > I must admit that what I said earlier was pretty naive thing to say. > The more posts I read here, the more I realize how bad my Java training > course was. There's nothing related to this even on the SCJP exam, so > you can officially be "Java Certified Programmer" and not know anything > about floating point. Now how about that. For many Java programmers doing various business apps, the only think they need to know about floating point is that they should not use them. Arne
First
|
Prev
|
Pages: 1 2 3 4 5 Prev: POI Excel Sheet String Formats Next: How to append to textarea immediately? |