# math_func.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD 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.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "math_func.hpp"
    00014
    00024 int LeastCommonMultiple(int a, int b)
    00025 {
    00026   if (a == 0 || b == 0) return 0; // By definition.
    00027   if (a == 1 || a == b) return b;
    00028   if (b == 1) return a;
    00029
    00030   return a * b / GreatestCommonDivisor(a, b);
    00031 }
    00032
    00039 int GreatestCommonDivisor(int a, int b)
    00040 {
    00041   while (b != 0) {
    00042     int t = b;
    00043     b = a % b;
    00044     a = t;
    00045   }
    00046   return a;
    00047
    00048 }
    00049
    00057 int DivideApprox(int a, int b)
    00058 {
    00059   int random_like = ((a + b) * (a - b)) % b;
    00060
    00061   int remainder = a % b;
    00062
    00063   int ret = a / b;
    00064   if (abs(random_like) < abs(remainder)) {
    00065     ret += ((a < 0) ^ (b < 0)) ? -1 : 1;
    00066   }
    00067
    00068   return ret;
    00069 }
    00070
    00077 uint32 IntSqrt(uint32 num)
    00078 {
    00079   uint32 res = 0;
    00080   uint32 bit = 1UL << 30; // Second to top bit number.
    00081
    00082   /* 'bit' starts at the highest power of four <= the argument. */
    00083   while (bit > num) bit >>= 2;
    00084
    00085   while (bit != 0) {
    00086     if (num >= res + bit) {
    00087       num -= res + bit;
    00088       res = (res >> 1) + bit;
    00089     } else {
    00090       res >>= 1;
    00091     }
    00092     bit >>= 2;
    00093   }
    00094
    00095   /* Arithmetic rounding to nearest integer. */
    00096   if (num > res) res++;
    00097
    00098   return res;
    00099 }