Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Ai giải thích giùm mình với??

Discussion in 'Xây dựng ứng dụng server-client desktop' started by thangthang23, 31/10/15.

  1. thangthang23

    thangthang23 New Member

    Tại sao cùng 1 kiểu mà kết quả nó lại khác nhau ạ ??

    public class NewClass {

    public static void main(String[] args) {
    int dem = 0;
    int dem1 = 0;
    int dem2 = 0;
    int dem3 = 0;
    for (double a = 0; a < 10; a += 0.2) {
    for (int i = 0; i < 100; i += 2) {
    for (double a = 0; a < 100; a += 0.2) {
    for (int i = 0; i < 1000; i += 2) {
  2. TruongVanHoc

    TruongVanHoc New Member

    Mình thử thêm dòng này System.out.println("a="+a+" dem"+dem); vào dưới dem++; thì kết quả nó ra thế này @@

    a=0.0 dem1
    a=0.2 dem2
    a=0.4 dem3
    a=0.6000000000000001 dem4
    a=0.8 dem5
    a=1.0 dem6
    a=1.2 dem7
    a=1.4 dem8
    a=1.5999999999999999 dem9
    a=1.7999999999999998 dem10
    a=1.9999999999999998 dem11
    a=2.1999999999999997 dem12
    a=2.4 dem13
    a=2.6 dem14
    a=2.8000000000000003 dem15
    a=3.0000000000000004 dem16
    a=3.2000000000000006 dem17
    a=3.400000000000001 dem18
    a=3.600000000000001 dem19
    a=3.800000000000001 dem20
    a=4.000000000000001 dem21
    a=4.200000000000001 dem22
    a=4.400000000000001 dem23
    a=4.600000000000001 dem24
    a=4.800000000000002 dem25
    a=5.000000000000002 dem26
    a=5.200000000000002 dem27
    a=5.400000000000002 dem28
    a=5.600000000000002 dem29
    a=5.8000000000000025 dem30
    a=6.000000000000003 dem31
    a=6.200000000000003 dem32
    a=6.400000000000003 dem33
    a=6.600000000000003 dem34
    a=6.800000000000003 dem35
    a=7.0000000000000036 dem36
    a=7.200000000000004 dem37
    a=7.400000000000004 dem38
    a=7.600000000000004 dem39
    a=7.800000000000004 dem40
    a=8.000000000000004 dem41
    a=8.200000000000003 dem42
    a=8.400000000000002 dem43
    a=8.600000000000001 dem44
    a=8.8 dem45
    a=9.0 dem46
    a=9.2 dem47
    a=9.399999999999999 dem48
    a=9.599999999999998 dem49
    a=9.799999999999997 dem50
    a=9.999999999999996 dem51
    đúng ra dem=50 là a=9.8, a+0.2 nữa là =10 thì sẽ dừng lại, nhưng ở đây a=9.7999<9,8 nên a+0.2<10 và dem+1=51 :P
    còn vì sao a nó nhảy như vậy thì mình cũng không biết

    a ở dem2 cũng bị, nhưng dem2 = 500 thì a=99.8000...88>99.8, dem2+0.2 >100 nên nó mới dừng
    Last edited: 2/11/15
  3. snoob2014

    snoob2014 New Member

            for (BigDecimal a = new BigDecimal(0); a.compareTo(new BigDecimal(100)) == -1; a = a.add(new BigDecimal(0.2))) {
    Xài BigDecimal đi bạn, đừng bao giờ dùng float hay double để làm counter
  4. Joe

    Joe Thành viên VIP

    Double or float is never exactly rendered by computer. If you study Computer Science you'll understand what I say. However, here is a brief explanation:
    Floating point is a continuous value and modern Computer bases on discrete value. What's DISCRETE value ? It's a value that always has a final value like an integer. Before the revolution of digital technology "old" computer was primitive and based on "continuous" value like this sliderule here
    Modern computer is digital and digital is either ONE (or true) or NULL (or false). Or in short: Binary. Because digital computer was invented in Europe and in the US the base of modern computer is European character set which fits neatly in 8 digital bits = a byte. A range between 0..255. Now, just let a computer try to represent a continuous value which is 0.00000000, 0.00000001, ..., 1.00000000 in digital or in binary. Every Computer will give up if human does not teach it a TRICK how-to-do-that. This trick is called Computing Algorithm. Click HERE and read the last reply I made for Dangxunb.

    You guys, snoob2014, TruongVanHoc and ThangThang23, should learn the basics of computer before you all start to work with computer. Otherwise it's like a blind who try to make his way without a stick.
    Last edited: 2/11/15
    TruongVanHoc and mrjulee like this.

Chia sẻ trang này