OpenTTD
math_func.cpp File Reference

Math functions. More...

`#include "../stdafx.h"`
`#include "math_func.hpp"`
`#include "../safeguards.h"`

Go to the source code of this file.

## Functions

int LeastCommonMultiple (int a, int b)
Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multiple of both a and b. More...

int GreatestCommonDivisor (int a, int b)
Compute greatest common divisor (gcd) of a and b. More...

int DivideApprox (int a, int b)
Deterministic approximate division. More...

uint32 IntSqrt (uint32 num)
Compute the integer square root. More...

## Detailed Description

Math functions.

Definition in file math_func.cpp.

## ◆ DivideApprox()

 int DivideApprox ( int a, int b )

Deterministic approximate division.

Cancels out division errors stemming from the integer nature of the division over multiple runs.

Parameters
 a Dividend. b Divisor.
Returns
a/b or (a/b)+1.

Definition at line 59 of file math_func.cpp.

References abs().

## ◆ GreatestCommonDivisor()

 int GreatestCommonDivisor ( int a, int b )

Compute greatest common divisor (gcd) of a and b.

Parameters
 a First number. b second number.
Returns
Greatest common divisor of a and b.

Definition at line 41 of file math_func.cpp.

Referenced by LeastCommonMultiple().

## ◆ IntSqrt()

 uint32 IntSqrt ( uint32 num )

Compute the integer square root.

Parameters
Returns
Rounded integer square root.
Note
Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

Definition at line 79 of file math_func.cpp.

Referenced by RoundDivSU().

## ◆ LeastCommonMultiple()

 int LeastCommonMultiple ( int a, int b )

Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multiple of both a and b.

Parameters
 a First number. b second number.
Returns
Least common multiple of values a and b.
Note
This function only works for non-negative values of a and b.

Definition at line 26 of file math_func.cpp.

References GreatestCommonDivisor().