Arithmetic: Difference between revisions

From David's Wiki
Created page with "Notes on basic math operations. =Integers= ==Division== Given 2 integers <code>n</code>, <code>m</code>, integer division returns the integer portion of the divided value.<br> This is floored result of true-division for positive numbers. In C, this rounds to zero but Python rounds down.<br> E.g. 5/2 returns 2 <pre> # Round down n/m # To round up always (n + m - 1) / m # To round to nearest (n + (m/2)) + m </pre> =Rounding="
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Notes on basic math operations.
Notes on basic math operations and computer primitives.


=Integers=
==Integers==


==Division==
===Division===
Given 2 integers <code>n</code>, <code>m</code>, integer division returns the integer portion of the divided value.<br>
Given 2 integers <code>n</code>, <code>m</code>, integer division returns the integer portion of the divided value.<br>
This is floored result of true-division for positive numbers. In C, this rounds to zero but Python rounds down.<br>
This is floored result of true-division for positive numbers. In C, this rounds to zero but Python rounds down.<br>
Line 19: Line 19:
</pre>
</pre>


=Rounding=
==Floats==
 
 
==Rounding==
Types of rounding
* Round towards zero
* Round down, i.e. towards negative infinity

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