OpenTTD Source: src/core/math_func.cpp File Reference

OpenTTD Source 15.3

Functions
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 | DivideApprox (int a, int b)
| Deterministic approximate division.
uint32_t | IntSqrt (uint32_t num)
| Compute the integer square root.

## Detailed Description

Math functions.

Definition in file math_func.cpp.

## Function Documentation

## ◆ 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 22 of file math_func.cpp.

References abs().

Referenced by StationViewWindow::BuildCargoList(), and StationViewWindow::EstimateDestinations().

## ◆ IntSqrt()

uint32_t IntSqrt | ( | uint32_t | num | ) |

Compute the integer square root.

Parameters

  num | Radicand.

Returns
  Rounded integer square root.

Note
  Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

Definition at line 42 of file math_func.cpp.

Referenced by CanalMaintenanceCost(), RailMaintenanceCost(), RoadMaintenanceCost(), SignalMaintenanceCost(), and StationMaintenanceCost().