From: Martin Gregorie on 12 Jul 2010 14:13 On Tue, 13 Jul 2010 00:07:36 +0900, J.H.Kim wrote: > Hi, everyone > > My java compiler and interpreter cannot display floating point variable. > > public class PrintTest { > public static void main(String[] args) { > double a = 3.141592; > System.out.println(a); > } > } > > The routine above does not display 3.141592. > So, what does it display? It works fine here: [kiwi(a)zappa java]$ javac PrintTest.java [kiwi(a)zappa java]$ java PrintTest 3.141592 [kiwi(a)zappa java]$ javac -version javac 1.6.0_17 [kiwi(a)zappa java]$ java -version java version "1.6.0_17" Java(TM) SE Runtime Environment (build 1.6.0_17-b04) Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing) -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
From: J.H.Kim on 12 Jul 2010 18:23 Martin Gregorie wrote: > > So, what does it display? > > It works fine here: > > [kiwi(a)zappa java]$ javac PrintTest.java > [kiwi(a)zappa java]$ java PrintTest > 3.141592 > [kiwi(a)zappa java]$ javac -version > javac 1.6.0_17 > [kiwi(a)zappa java]$ java -version > java version "1.6.0_17" > Java(TM) SE Runtime Environment (build 1.6.0_17-b04) > Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing) > It displayed blank.
From: Martin Gregorie on 12 Jul 2010 18:33 On Tue, 13 Jul 2010 07:23:28 +0900, J.H.Kim wrote: > Martin Gregorie wrote: > > >> So, what does it display? >> >> It works fine here: >> >> [kiwi(a)zappa java]$ javac PrintTest.java >> [kiwi(a)zappa java]$ java PrintTest >> 3.141592 >> [kiwi(a)zappa java]$ javac -version >> javac 1.6.0_17 >> [kiwi(a)zappa java]$ java -version >> java version "1.6.0_17" >> Java(TM) SE Runtime Environment (build 1.6.0_17-b04) Java HotSpot(TM) >> Client VM (build 14.3-b01, mixed mode, sharing) >> >> > It displayed blank. And what does it display when run from the command line? -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
From: Arne Vajhøj on 12 Jul 2010 21:11 On 12-07-2010 12:36, Screamin Lord Byron wrote: > On 07/12/2010 05:15 PM, markspace wrote: >> J.H.Kim wrote: >>> Hi, everyone >>> >>> My java compiler and interpreter cannot display floating point variable. >>> >>> public class PrintTest { >>> public static void main(String[] args) { >>> double a = 3.141592; >>> System.out.println(a); >>> } >>> } >>> >>> The routine above does not display 3.141592. >> >> <http://docs.sun.com/source/806-3568/ncg_goldberg.html> >> >> Floating point numbers aren't exact. You can't get exactly 3.141592 out >> of any floating point system. Consider using BigDecimal if you need >> "exact" numbers. > > > Well, in this example there is no calculation, and significand fits > within 52 bits of IEEE 754 double type so it should be printed out as > expected: 3.141592. Please someone correct me if I'm wrong. Even though 7 decimals digits is less than 52 bits, then it is far from guaranteed that a 7 decimals number can be represented exact within 52 bit. It would be nice if println guaranteed that any literal with less digits than what is max. due to size would be printed exactly like the literal. But I don't think that will always be the case. Arne > However, OP forgot to mention what actually is displayed after running this.
From: Screamin Lord Byron on 13 Jul 2010 07:02 On 07/13/2010 03:11 AM, Arne Vajhøj wrote: > On 12-07-2010 12:36, Screamin Lord Byron wrote: >> On 07/12/2010 05:15 PM, markspace wrote: >>> J.H.Kim wrote: >>>> Hi, everyone >>>> >>>> My java compiler and interpreter cannot display floating point >>>> variable. >>>> >>>> public class PrintTest { >>>> public static void main(String[] args) { >>>> double a = 3.141592; >>>> System.out.println(a); >>>> } >>>> } >>>> >>>> The routine above does not display 3.141592. >>> >>> <http://docs.sun.com/source/806-3568/ncg_goldberg.html> >>> >>> Floating point numbers aren't exact. You can't get exactly 3.141592 out >>> of any floating point system. Consider using BigDecimal if you need >>> "exact" numbers. >> >> >> Well, in this example there is no calculation, and significand fits >> within 52 bits of IEEE 754 double type so it should be printed out as >> expected: 3.141592. Please someone correct me if I'm wrong. > > Even though 7 decimals digits is less than 52 bits, then it is > far from guaranteed that a 7 decimals number can be represented > exact within 52 bit. I am aware there is a problem of representing fractions in any numeral system (for example, decimal 0.1 can't be exactly represented in binary, just as 1/3 can't be exactly represented in decimal), and here we're not saying nothing like double d = 2. - 0.1; 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.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: POI Excel Sheet String Formats Next: How to append to textarea immediately? |