Skocz do zawartości

Recommended Posts

Napisano

Cześć,

Jestem nowy na forum i nie mniej nowy w programowaniu. Mam za sobą zaledwie ok. 10 godzin nauki Javy, więc wiem, że nic nie wiem ;)

Wykonałem proste ćwiczenie z użyciem pętli:

public class Main {

    public static void main(String[] args) {

        for (int i=1; i<31; i++) {
            System.out.println(calculateInterest(10000, i));
        }

    }

    public static double calculateInterest (double amount, double interestRate) {
        return (amount * (interestRate/100));
    }
}

Spodziewałem się otrzymać taki wynik:

100.0
200.0
300.0
400.0
500.0
600.0
700.0
800.0
900.0
1000.0
1100.0
1200.0
1300.0
1400.0
1500.0
1600.0
1700.0
1800.0
1900.0
2000.0
2100.0
2200.0
2300.0
2400.0
2500.0
2600.0
2700.0
2800.0
2900.0
3000.0

Tymczasem, ku mojemu zaskoczeniu, komputer zwrócił takie coś:


100.0
200.0
300.0
400.0
500.0
600.0
700.0000000000001
800.0
900.0
1000.0
1100.0
1200.0
1300.0
1400.0000000000002
1500.0
1600.0
1700.0000000000002
1800.0
1900.0
2000.0
2100.0
2200.0
2300.0
2400.0
2500.0
2600.0
2700.0
2800.0000000000005
2900.0
3000.0

Skąd biorą się te ułamki?

  • 6 months later...
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...