Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

In this page you can find the example usage for java.lang Math min.

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int[] src = { 1, 2, 3 };
    int[] dst = { 1, 1, 1 };
    System.arraycopy(src, 0, dst, 0, Math.min(src.length, dst.length));

}

From source file:Main.java

public static void main(String[] args) {

    int x = 1234;
    int y = 123;//ww  w  .  ja  v  a 2 s .com

    // print the smaller number between x and y
    System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x, y));

}

From source file:Main.java

public static void main(String[] args) {

    double x = 123456.7;
    double y = -123.45;

    // print the smaller number between x and y
    System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x, y));

}

From source file:Main.java

public static void main(String[] args) {

    long x = 123456789098877l;
    long y = 123456789098771l;

    // print the smaller number between x and y
    System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x, y));

}

From source file:Main.java

public static void main(String[] args) {

    // get two float numbers
    float x = 1234.567f;
    float y = 123.456f;

    // print the smaller number between x and y
    System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x, y));

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    RandomAccessFile fh1 = new RandomAccessFile("a.txt", "r");
    RandomAccessFile fh2 = new RandomAccessFile("b.txt", "r");
    long filesize1 = fh1.length();
    long filesize2 = fh2.length();
    // allocate two buffers large enough to hold entire files
    int bufsize = (int) Math.min(filesize1, filesize2);
    byte[] buffer1 = new byte[bufsize];
    byte[] buffer2 = new byte[bufsize];

    fh1.readFully(buffer1, 0, bufsize);/* w  w w .  j  a v  a  2  s  . co  m*/
    fh2.readFully(buffer2, 0, bufsize);

    for (int i = 0; i < bufsize; i++) {
        if (buffer1[i] != buffer2[i]) {
            System.out.println("Files differ at offset " + i);
            break;
        }
    }
    fh1.close();
    fh2.close();
}

From source file:Main.java

public static void main(String[] args) {
    int a = 10;/*from   www  .ja  va 2 s. co  m*/
    int b = -50;
    int c = 3;
    double x = 25.0;
    double y = 3.0;
    double z = 4.0;

    System.out.println("abs(b)     = " + Math.abs(b));
    System.out.println("cbrt(x)   = " + Math.cbrt(x));
    System.out.println("exp(y)     = " + Math.exp(z));
    System.out.println("hypot(y, z)= " + Math.hypot(y, z));

    System.out.println("log(y)    = " + Math.log(y));
    System.out.println("log10(y)  = " + Math.log10(y));
    System.out.println("max(a, b) = " + Math.max(a, b));
    System.out.println("min(a, b) = " + Math.min(a, b));
    System.out.println("pow(a, c) = " + Math.pow(a, c));
    System.out.println("random()  = " + Math.random());
    System.out.println("signum(b) = " + Math.signum(b));
    System.out.println("sqrt(x)   = " + Math.sqrt(y));
}

From source file:MainClass.java

public static void main(String args[]) {
    System.out.printf("Math.abs( 23.7 ) = %f\n", Math.abs(23.7));
    System.out.printf("Math.abs( 0.0 ) = %f\n", Math.abs(0.0));
    System.out.printf("Math.abs( -23.7 ) = %f\n", Math.abs(-23.7));
    System.out.printf("Math.ceil( 9.2 ) = %f\n", Math.ceil(9.2));
    System.out.printf("Math.ceil( -9.8 ) = %f\n", Math.ceil(-9.8));
    System.out.printf("Math.cos( 0.0 ) = %f\n", Math.cos(0.0));
    System.out.printf("Math.exp( 1.0 ) = %f\n", Math.exp(1.0));
    System.out.printf("Math.exp( 2.0 ) = %f\n", Math.exp(2.0));
    System.out.printf("Math.floor( 9.2 ) = %f\n", Math.floor(9.2));
    System.out.printf("Math.floor( -9.8 ) = %f\n", Math.floor(-9.8));
    System.out.printf("Math.log( Math.E ) = %f\n", Math.log(Math.E));
    System.out.printf("Math.log( Math.E * Math.E ) = %f\n", Math.log(Math.E * Math.E));
    System.out.printf("Math.max( 2.3, 12.7 ) = %f\n", Math.max(2.3, 12.7));
    System.out.printf("Math.max( -2.3, -12.7 ) = %f\n", Math.max(-2.3, -12.7));
    System.out.printf("Math.min( 2.3, 12.7 ) = %f\n", Math.min(2.3, 12.7));
    System.out.printf("Math.min( -2.3, -12.7 ) = %f\n", Math.min(-2.3, -12.7));
    System.out.printf("Math.pow( 2.0, 7.0 ) = %f\n", Math.pow(2.0, 7.0));
    System.out.printf("Math.pow( 9.0, 0.5 ) = %f\n", Math.pow(9.0, 0.5));
    System.out.printf("Math.sin( 0.0 ) = %f\n", Math.sin(0.0));
    System.out.printf("Math.sqrt( 900.0 ) = %f\n", Math.sqrt(900.0));
    System.out.printf("Math.sqrt( 9.0 ) = %f\n", Math.sqrt(9.0));
    System.out.printf("Math.tan( 0.0 ) = %f\n", Math.tan(0.0));
}

From source file:org.eclipse.swt.snippets.Snippet201.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 201");
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER);
    table.addListener(SWT.SetData, event -> {
        TableItem item = (TableItem) event.item;
        int index = table.indexOf(item);
        int start = index / PAGE_SIZE * PAGE_SIZE;
        int end = Math.min(start + PAGE_SIZE, table.getItemCount());
        for (int i = start; i < end; i++) {
            item = table.getItem(i);/*ww w.  j  a  v  a  2 s .  co  m*/
            item.setText("Item " + i);
        }
    });
    table.setLayoutData(new RowData(200, 200));
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add Items");
    final Label label = new Label(shell, SWT.NONE);
    button.addListener(SWT.Selection, event -> {
        long t1 = System.currentTimeMillis();
        table.setItemCount(COUNT);
        long t2 = System.currentTimeMillis();
        label.setText("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms) [page=" + PAGE_SIZE + "]");
        shell.layout();
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Diff.java

public static void main(String args[]) {
    RandomAccessFile fh1 = null;// w w w .  java  2s.  co  m
    RandomAccessFile fh2 = null;
    int bufsize; // size of smallest file
    long filesize1 = -1;
    long filesize2 = -1;
    byte buffer1[]; // the two file caches
    byte buffer2[];
    // check what you get as command-line arguments
    if (args.length == 0 || args[0].equals("?")) {
        System.err.println("USAGE: java Diff <file1> <file2> | ?");
        System.exit(0);
    }
    // open file ONE for reading
    try {
        fh1 = new RandomAccessFile(args[0], "r");
        filesize1 = fh1.length();
    } catch (IOException ioErr) {
        System.err.println("Could not find " + args[0]);
        System.err.println(ioErr);
        System.exit(100);
    }
    // open file TWO for reading
    try {
        fh2 = new RandomAccessFile(args[1], "r");
        filesize2 = fh2.length();
    } catch (IOException ioErr) {
        System.err.println("Could not find " + args[1]);
        System.err.println(ioErr);
        System.exit(100);
    }
    if (filesize1 != filesize2) {
        System.out.println("Files differ in size !");
        System.out.println("'" + args[0] + "' is " + filesize1 + " bytes");
        System.out.println("'" + args[1] + "' is " + filesize2 + " bytes");
    }
    // allocate two buffers large enough to hold entire files
    bufsize = (int) Math.min(filesize1, filesize2);
    buffer1 = new byte[bufsize];
    buffer2 = new byte[bufsize];
    try {
        fh1.readFully(buffer1, 0, bufsize);
        fh2.readFully(buffer2, 0, bufsize);

        for (int i = 0; i < bufsize; i++) {
            if (buffer1[i] != buffer2[i]) {
                System.out.println("Files differ at offset " + i);
                break;
            }
        }
    } catch (IOException ioErr) {
        System.err.println("ERROR: An exception occurred while processing the files");
        System.err.println(ioErr);
    } finally {
        try {
            fh1.close();
            fh2.close();
        } catch (IOException ignored) {
        }
    }
}