Matematyka w programowaniu Java. Przegląd metod klasy Math.

Przegląd podstawowych metod z klasy Math z wyjaśnieniami. Zakres omawianych metod obejmuje:

  • Potęgowanie
  • Pierwiastkowanie
  • Logarytmy
  • Konwersja stopnie – radiany i odwrotnie
  • Funkcje trygonometryczne, cyklometryczne i hiperboliczne
  • Przeciwprostokątna
  • min, max
  • Zaokrąglenia
  • Reszta z dzielenia
  • znak liczby

Dodatkowo omawiam funkcje pomocnicze które nie znajdują się w klasie Math:

  • pierwiastek dowolnego stopnia
  • logarytm od dowolnej podstawie
  • zaokrąglenia o dowolnej precyzji
package pl.am.math.base;

public class MathDemo {
    public static void main(String[] args) {
        System.out.println("\nstałe");
        System.out.println("Math.PI = " + Math.PI);
        System.out.println("Math.E = " + Math.E);

        System.out.println("\npotęgowanie");
        System.out.println("Math.pow(2.5, 4) = " + Math.pow(2.5, 4));
        System.out.println("Math.exp(1) = " + Math.exp(1)); //E^x

        System.out.println("\npierwiastkowanie");
        System.out.println("Math.sqrt(64) =  " + Math.sqrt(64));
        System.out.println("Math.cbrt(125) = " + Math.cbrt(125));
        System.out.println("Math.pow(1024, 1/10) = " + Math.pow(1024, 1/10));
        System.out.println("Math.pow(1024, 1/10.0) = " + Math.pow(1024, 1/10.0));
        System.out.println("MathUtils.root(1024, 10) = " + MathUtils.root(1024, 10));

        System.out.println("\nlogarytmy");
        System.out.println("Math.log(Math.E) =  " + Math.log(Math.E));
        System.out.println("Math.log10(100) =  " + Math.log10(100));
        System.out.println("MathUtils.log(27, 3) =  " + MathUtils.log(27, 3));

        System.out.println("\nstopnie i radiany");
        System.out.println("Math.toRadians(180) =  " + Math.toRadians(180));
        System.out.println("Math.toDegrees(2*Math.PI) =  " + Math.toDegrees(2*Math.PI));

        System.out.println("\nFunkcje trygonometryczne");
        System.out.println("Math.sin(Math.PI/2) = " + Math.sin(Math.PI/2));
        System.out.println("Math.cos(Math.PI/2) = " + Math.cos(Math.PI/2));
        System.out.println("Math.tan(Math.PI/2) = " + Math.tan(Math.PI/2));

        System.out.println("\nFunkcje cyklometryczne");
        System.out.println("Math.asin(2) = " + Math.asin(2));
        System.out.println("Math.asin(0) = " + Math.asin(0));
        System.out.println("Math.acos(0) = " + Math.acos(0));
        System.out.println("Math.atan(0) = " + Math.atan(0));

        System.out.println("\nFunkcje hiperboliczne");
        System.out.println("Math.sinh(1) = " + Math.sinh(1));
        System.out.println("Math.cosh(1) = " + Math.cosh(1));
        System.out.println("Math.tanh(1) = " + Math.tanh(1));

        System.out.println("\nPrzeciwprostokątna");
        System.out.println("Math.hypot(3, 4) = " + Math.hypot(3, 4));

        System.out.println("\nMin Max");
        System.out.println("Math.min(-1, 5) = " + Math.min(-1, 5));
        System.out.println("Math.max(-1, 5) = " + Math.max(-1, 5));

        System.out.println("\nWartość bezwzględna");
        System.out.println("Math.abs(-1) = " + Math.abs(-1));
        System.out.println("Math.abs(1) = " + Math.abs(1));

        System.out.println("\nZaokrąglania");
        System.out.println("Math.ceil(2.3) = " + Math.ceil(2.3));
        System.out.println("Math.ceil(2.7) = " + Math.ceil(2.7));
        System.out.println("Math.floor(2.3) = " + Math.floor(2.3));
        System.out.println("Math.floor(2.7) = " + Math.floor(2.7));
        System.out.println("Math.round(2.3) = " + Math.round(2.3));
        System.out.println("Math.round(2.5) = " + Math.round(2.5));
        System.out.println("Math.round(2.7) = " + Math.round(2.7));
        System.out.println("Math.rint(2.3) = " + Math.rint(2.3));
        System.out.println("Math.rint(2.5) = " + Math.rint(2.5));
        System.out.println("Math.rint(2.7) = " + Math.rint(2.7));
        System.out.println("MathUtils.round(1.2345, 2) = " + MathUtils.round(1.2345, 2));
        System.out.println("MathUtils.round(1.235, 2) = " + MathUtils.round(1.235, 2));
        System.out.println("MathUtils.round(1.235, 0)) = " + MathUtils.round(1.235, 0));
        System.out.println("MathUtils.round(555, -1) = " + MathUtils.round(555, -1));
        System.out.println("MathUtils.round(555, -2) = " + MathUtils.round(555, -2));

        System.out.println("\nReszta z dzielenia");
        System.out.println("21/5 = " + 21/5);
        System.out.println("Math.floorDiv(21, 5) = " + Math.floorDiv(21, 5));
        System.out.println("-21/5 = " + -21/5);
        System.out.println("Math.floorDiv(-21, 5) = " + Math.floorDiv(-21, 5));
        System.out.println("21%5 = " + 21%5);
        System.out.println("Math.floorMod(21, 5) = " + Math.floorMod(21, 5));
        System.out.println("-21%5 = " + -21%5);
        System.out.println("Math.floorMod(-21, 5) = " + Math.floorMod(-21, 5));
        System.out.println("-21/5 = " + -21/5 + " reszty " + -21%5);
        System.out.println("-21/5 = " + Math.floorDiv(-21, 5)+ " reszty " + Math.floorMod(-21, 5));

        System.out.println("\nznak");
        System.out.println("Math.signum(-34) = " + Math.signum(-34));
        System.out.println("Math.signum(0) = " + Math.signum(0));
        System.out.println("Math.signum(21) = " + Math.signum(21));
        System.out.println("Math.copySign(-14, 4) = " + Math.copySign(-14, 4));
        System.out.println("Math.copySign(14, -4) = " + Math.copySign(14, -4));


    }
}
package pl.am.math.base;

public class MathUtils {
    public static double root(double a, double degree) {
        return Math.pow(a, 1/degree);
    }

    public static double log(double a, double base) {
        return Math.log(a) / Math.log(base);
    }

    public static double round(double a, int precision) {
        double multiplier = Math.pow(10, precision);
        double temp = Math.round(a * multiplier);

        return temp / multiplier;
    }
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *