/* * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Codename One designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Codename One through http://www.codenameone.com/ if you * need additional information or have any questions. */ package net.sourceforge.retroweaver.runtime.java.lang; /** * * @author Shai Almog */ public class Math_ { /** * Returns the result of rounding the argument to an integer. The result is * equivalent to {@code (long) Math.floor(d+0.5)}. * <p> * Special cases: * <ul> * <li>{@code round(+0.0) = +0.0}</li> * <li>{@code round(-0.0) = +0.0}</li> * <li>{@code round((anything > Long.MAX_VALUE) = Long.MAX_VALUE}</li> * <li>{@code round((anything < Long.MIN_VALUE) = Long.MIN_VALUE}</li> * <li>{@code round(+infintiy) = Long.MAX_VALUE}</li> * <li>{@code round(-infintiy) = Long.MIN_VALUE}</li> * <li>{@code round(NaN) = +0.0}</li> * </ul> * * @param d * the value to be rounded. * @return the closest integer to the argument. */ public static long round(double d) { // check for NaN if (d != d) { return 0L; } return (long) Math.floor(d + 0.5d); } /** * Returns the result of rounding the argument to an integer. The result is * equivalent to {@code (int) Math.floor(f+0.5)}. * <p> * Special cases: * <ul> * <li>{@code round(+0.0) = +0.0}</li> * <li>{@code round(-0.0) = +0.0}</li> * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li> * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li> * <li>{@code round(+infintiy) = Integer.MAX_VALUE}</li> * <li>{@code round(-infintiy) = Integer.MIN_VALUE}</li> * <li>{@code round(NaN) = +0.0}</li> * </ul> * * @param f * the value to be rounded. * @return the closest integer to the argument. */ public static int round(float f) { // check for NaN if (f != f) { return 0; } return (int) Math.floor(f + 0.5f); } }