number lenght

Hi. I have code like so:double a= 1.11111111111111111111111111111111111111111111111111;System.out.println(a);And it writes 1.11111111111111112. Why? What should I do to write pi const. (but not from string)?
[268 byte] By [laqa] at [2007-11-27 2:24:12]
# 1

This is related to the way doubles are stored internally.

It's technically impossible to represent the number

1.11111111111111111111111111111111111111111111111111

exactly using the IEEE floating point number binary format. So you don't actually have

1.111111111111111111111111111111111111111111111111111

stored. The other problem is that the jvm will round your output to a certain number of decimal places when you want to display it, so you will need to use a formatter to specify how many decimal places you really want. That still won't guarantee that it wont round the last digit up to a 2, though, because of what I said above.

- Adam

guitar_man_Fa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 2
so what should I do if I want to compute pi const and display e.g. 70 digit after comma?
laqa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 3
If you want Pi then take a look at the Math class. It is in java.lang package so no need to import anything.
floundera at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 4
Use the java.math.BigDecimal class.- Adam
guitar_man_Fa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 5
> If you want Pi then take a look at the Math class. It> is in java.lang package so no need to import anything.? to 70 significant figures? I doubt it, but maybe.
guitar_man_Fa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 6
> so what should I do if I want to compute pi const and> display e.g. 70 digit after comma?Trick question?
DrLaszloJamfa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 7
> > so what should I do if I want to compute pi const> and> > display e.g. 70 digit after comma?> > Trick question?maybe he's French?
guitar_man_Fa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...
# 8
> so what should I do if I want to compute pi const and> display e.g. 70 digit after comma?Use the BigDecimal class to do that computation.
DrClapa at 2007-7-12 2:30:54 > top of Java-index,Java Essentials,New To Java...