Skip to content

Instantly share code, notes, and snippets.

@bartTC
Last active February 28, 2023 05:19
Show Gist options
  • Save bartTC/b6420f598d50098d36f1083b7be19d0d to your computer and use it in GitHub Desktop.
Save bartTC/b6420f598d50098d36f1083b7be19d0d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Decimals show up with a large precision because ... computers. 🙈 If you're interested in the background, watch [this video](https://www.youtube.com/watch?v=8afbTaA-gOQ)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('1.5500000000000000444089209850062616169452667236328125')"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import decimal\n",
"decimal.Decimal(1.55)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Adding two Decimals will give you a result with high precision."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('3.100000000000000088817841970')"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"decimal.Decimal(1.55) + decimal.Decimal(1.55)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"However you can set a global (or local) precision in the decimal context:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"context = decimal.getcontext()\n",
"context.prec = 5\n",
"\n",
"decimal.setcontext(context)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Which does not have effect on the display of the number. That display is just the value in memory."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('1.5500000000000000444089209850062616169452667236328125')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"decimal.Decimal(1.55)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"But it does have impact on caluclations with decimals. They will honor the new precision."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('3.1000')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"decimal.Decimal(1.55) + decimal.Decimal(1.55)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('0.52632')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"decimal.Decimal(1.23456789) / decimal.Decimal(2.34567890)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Quantize Operations work too"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Decimal('1.24')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"decimal.Decimal(1.23456789).quantize(decimal.Decimal(\"0.01\"), rounding=decimal.ROUND_UP)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"But their exponent length must not exceed the context precision."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "InvalidOperation",
"evalue": "[<class 'decimal.InvalidOperation'>]",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mInvalidOperation\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m decimal\u001b[39m.\u001b[39;49mDecimal(\u001b[39m1.23456789\u001b[39;49m)\u001b[39m.\u001b[39;49mquantize(decimal\u001b[39m.\u001b[39;49mDecimal(\u001b[39m\"\u001b[39;49m\u001b[39m0.00001\u001b[39;49m\u001b[39m\"\u001b[39;49m), rounding\u001b[39m=\u001b[39;49mdecimal\u001b[39m.\u001b[39;49mROUND_UP)\n",
"\u001b[0;31mInvalidOperation\u001b[0m: [<class 'decimal.InvalidOperation'>]"
]
}
],
"source": [
"decimal.Decimal(1.23456789).quantize(decimal.Decimal(\"0.00001\"), rounding=decimal.ROUND_UP)"
]
}
],
"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.11.0"
},
"vscode": {
"interpreter": {
"hash": "bbd4ab6cee4fbadc42120d79571a0419777e4c09f1fba222b628301c5d986467"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment