/****************************************************************************** * This program demonstrates the math integer functions. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import org.pureprogrammer.Utils; public class Math2 { public static void main(String[] args) { final int a = 5; final int b = 10; final int c = -2; // abs, floor, ceil, round, trunc, min, max System.out.println(Utils.format("abs({0:d}) = {1:d}", a, Math.abs(a))); System.out.println(Utils.format("abs({0:d}) = {1:d}", c, Math.abs(c))); System.out.println(Utils.format("min({0:d}, {1:d}) = {2:d}", a, b, Math.min(a, b))); System.out.println(Utils.format("max({0:d}, {1:d}) = {2:d}", a, b, Math.max(a, b))); System.out.println(Utils.format("min({0:d}, {1:d}) = {2:d}", b, c, Math.min(b, c))); System.out.println(Utils.format("max({0:d}, {1:d}) = {2:d}", b, c, Math.max(b, c))); // random numbers System.out.println(Utils.format("random({0:d}) = {1:d}", a, (int)(a * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", a, (int)(a * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", a, (int)(a * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", a, (int)(a * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", a, (int)(a * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", b, (int)(b * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", b, (int)(b * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", b, (int)(b * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", b, (int)(b * Math.random()))); System.out.println(Utils.format("random({0:d}) = {1:d}", b, (int)(b * Math.random()))); System.out.println(Utils.format("random(2) = {0:d}", (int)(2 * Math.random()))); System.out.println(Utils.format("random(2) = {0:d}", (int)(2 * Math.random()))); System.out.println(Utils.format("random(2) = {0:d}", (int)(2 * Math.random()))); System.out.println(Utils.format("random(2) = {0:d}", (int)(2 * Math.random()))); System.out.println(Utils.format("random(2) = {0:d}", (int)(2 * Math.random()))); System.out.println(Utils.format("random() = {0:f}", Math.random())); System.out.println(Utils.format("random() = {0:f}", Math.random())); System.out.println(Utils.format("random() = {0:f}", Math.random())); System.out.println(Utils.format("random() = {0:f}", Math.random())); System.out.println(Utils.format("random() = {0:f}", Math.random())); } }