Last active
August 17, 2018 18:52
-
-
Save travishen/6a233f8bd3626644d89c2c369a2f8721 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## floor operator & modulo operator(mod)\n", | |
"\n", | |
"155 / 4 = 38 with remainder 3\n", | |
"\n", | |
"155 = 4 * (155 // 4) + (155 % 4)\n", | |
"\n", | |
"### floor: the floor of a real number a is the largest integer <= a\n", | |
"\n", | |
"a // b is not the integer portion of a / b, it is the floor of a / b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import math" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"floor(3.14)= 3\n", | |
"floor(-3.14)= -4\n", | |
"floor(-3.00000000000001)= -4\n", | |
"floor(-3.0000000000000001)= -3\n" | |
] | |
} | |
], | |
"source": [ | |
"print('floor(3.14)=', floor(3.14))\n", | |
"print('floor(-3.14)=', floor(-3.14))\n", | |
"print('floor(-3.00000000000001)=', floor(-3.00000000000001))\n", | |
"# float has limited precision in python\n", | |
"print('floor(-3.0000000000000001)=', floor(-3.0000000000000001)) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def divider(a, b):\n", | |
" division = a / b\n", | |
" flr = a // b\n", | |
" mod = a % b\n", | |
" print('a/b=', division)\n", | |
" print('a//b=', flr)\n", | |
" print('a%b=', mod)\n", | |
" print('floor(a/b)=', math.floor(division))\n", | |
" print('trunc(a/b)=', math.trunc(division)) \n", | |
" print('equation:{0} = {1} * {2} + {3}'.format(a, b, flr, mod))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a/b= 33.75\n", | |
"a//b= 33\n", | |
"a%b= 3\n", | |
"floor(a/b)= 33\n", | |
"trunc(a/b)= 33\n", | |
"equation:135 = 4 * 33 + 3\n" | |
] | |
} | |
], | |
"source": [ | |
"# positive number\n", | |
"divider(135, 4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a/b= -33.75\n", | |
"a//b= -34\n", | |
"a%b= 1\n", | |
"floor(a/b)= -34\n", | |
"trunc(a/b)= -33\n", | |
"equation:-135 = 4 * -34 + 1\n" | |
] | |
} | |
], | |
"source": [ | |
"# nagtive number\n", | |
"divider(-135, 4)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Reminder: Always satisfied the equation: a = b * (a // b) + (a % b)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment