Arithmetic: Difference between revisions

From David's Wiki
No edit summary
No edit summary
 
Line 1: Line 1:
Notes on basic math operations.
Notes on basic math operations and computer primitives.


==Integers==
==Integers==
Line 18: Line 18:
(n + (m/2)) + m
(n + (m/2)) + m
</pre>
</pre>
==Floats==


==Rounding==
==Rounding==

Latest revision as of 16:02, 7 January 2022

Notes on basic math operations and computer primitives.

Integers

Division

Given 2 integers n, m, integer division returns the integer portion of the divided value.
This is floored result of true-division for positive numbers. In C, this rounds to zero but Python rounds down.
E.g. 5/2 returns 2

# Round down
n/m

# To round up always
(n + m - 1) / m

# To round to nearest
(n + (m/2)) + m

Floats

Rounding

Types of rounding

  • Round towards zero
  • Round down, i.e. towards negative infinity