This file is indexed.

/usr/share/doc/python-mpmath-doc/html/general.html is in python-mpmath-doc 1.0.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Utility functions &#8212; mpmath 1.0.0 documentation</title>
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Plotting" href="plotting.html" />
    <link rel="prev" title="Contexts" href="contexts.html" /> 
  </head>
  <body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="plotting.html" title="Plotting"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="contexts.html" title="Contexts"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">mpmath 1.0.0 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="utility-functions">
<h1>Utility functions<a class="headerlink" href="#utility-functions" title="Permalink to this headline"></a></h1>
<p>This page lists functions that perform basic operations
on numbers or aid general programming.</p>
<div class="section" id="conversion-and-printing">
<h2>Conversion and printing<a class="headerlink" href="#conversion-and-printing" title="Permalink to this headline"></a></h2>
<div class="section" id="mpmathify-convert">
<h3><code class="xref py py-func docutils literal"><span class="pre">mpmathify()</span></code> / <code class="xref py py-func docutils literal"><span class="pre">convert()</span></code><a class="headerlink" href="#mpmathify-convert" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.mpmathify">
<code class="descclassname">mpmath.</code><code class="descname">mpmathify</code><span class="sig-paren">(</span><em>x</em>, <em>strings=True</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.mpmathify" title="Permalink to this definition"></a></dt>
<dd><p>Converts <em>x</em> to an <code class="docutils literal"><span class="pre">mpf</span></code> or <code class="docutils literal"><span class="pre">mpc</span></code>. If <em>x</em> is of type <code class="docutils literal"><span class="pre">mpf</span></code>,
<code class="docutils literal"><span class="pre">mpc</span></code>, <code class="docutils literal"><span class="pre">int</span></code>, <code class="docutils literal"><span class="pre">float</span></code>, <code class="docutils literal"><span class="pre">complex</span></code>, the conversion
will be performed losslessly.</p>
<p>If <em>x</em> is a string, the result will be rounded to the present
working precision. Strings representing fractions or complex
numbers are permitted.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mpmathify</span><span class="p">(</span><span class="mf">3.5</span><span class="p">)</span>
<span class="go">mpf(&#39;3.5&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mpmathify</span><span class="p">(</span><span class="s1">&#39;2.1&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;2.1000000000000001&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mpmathify</span><span class="p">(</span><span class="s1">&#39;3/4&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;0.75&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mpmathify</span><span class="p">(</span><span class="s1">&#39;2+3j&#39;</span><span class="p">)</span>
<span class="go">mpc(real=&#39;2.0&#39;, imag=&#39;3.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="nstr">
<h3><code class="xref py py-func docutils literal"><span class="pre">nstr()</span></code><a class="headerlink" href="#nstr" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.nstr">
<code class="descclassname">mpmath.</code><code class="descname">nstr</code><span class="sig-paren">(</span><em>x</em>, <em>n=6</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.nstr" title="Permalink to this definition"></a></dt>
<dd><p>Convert an <code class="docutils literal"><span class="pre">mpf</span></code> or <code class="docutils literal"><span class="pre">mpc</span></code> to a decimal string literal with <em>n</em>
significant digits. The small default value for <em>n</em> is chosen to
make this function useful for printing collections of numbers
(lists, matrices, etc).</p>
<p>If <em>x</em> is a list or tuple, <a class="reference internal" href="#mpmath.nstr" title="mpmath.nstr"><code class="xref py py-func docutils literal"><span class="pre">nstr()</span></code></a> is applied recursively
to each element. For unrecognized classes, <a class="reference internal" href="#mpmath.nstr" title="mpmath.nstr"><code class="xref py py-func docutils literal"><span class="pre">nstr()</span></code></a>
simply returns <code class="docutils literal"><span class="pre">str(x)</span></code>.</p>
<p>The companion function <a class="reference internal" href="#mpmath.nprint" title="mpmath.nprint"><code class="xref py py-func docutils literal"><span class="pre">nprint()</span></code></a> prints the result
instead of returning it.</p>
<p>The keyword arguments <em>strip_zeros</em>, <em>min_fixed</em>, <em>max_fixed</em>
and <em>show_zero_exponent</em> are forwarded to <code class="xref py py-func docutils literal"><span class="pre">to_str()</span></code>.</p>
<p>The number will be printed in fixed-point format if the position
of the leading digit is strictly between min_fixed
(default = min(-dps/3,-5)) and max_fixed (default = dps).</p>
<p>To force fixed-point format always, set min_fixed = -inf,
max_fixed = +inf. To force floating-point format, set
min_fixed &gt;= max_fixed.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nstr</span><span class="p">([</span><span class="o">+</span><span class="n">pi</span><span class="p">,</span> <span class="n">ldexp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">500</span><span class="p">)])</span>
<span class="go">&#39;[3.14159, 3.05494e-151]&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">([</span><span class="o">+</span><span class="n">pi</span><span class="p">,</span> <span class="n">ldexp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">500</span><span class="p">)])</span>
<span class="go">[3.14159, 3.05494e-151]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nstr</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="s2">&quot;5e-10&quot;</span><span class="p">),</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">&#39;5.0e-10&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nstr</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="s2">&quot;5e-10&quot;</span><span class="p">),</span> <span class="mi">5</span><span class="p">,</span> <span class="n">strip_zeros</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">&#39;5.0000e-10&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nstr</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="s2">&quot;5e-10&quot;</span><span class="p">),</span> <span class="mi">5</span><span class="p">,</span> <span class="n">strip_zeros</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">min_fixed</span><span class="o">=-</span><span class="mi">11</span><span class="p">)</span>
<span class="go">&#39;0.00000000050000&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nstr</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="mi">5</span><span class="p">,</span> <span class="n">show_zero_exponent</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">&#39;0.0e+0&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="nprint">
<h3><code class="xref py py-func docutils literal"><span class="pre">nprint()</span></code><a class="headerlink" href="#nprint" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.nprint">
<code class="descclassname">mpmath.</code><code class="descname">nprint</code><span class="sig-paren">(</span><em>x</em>, <em>n=6</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.nprint" title="Permalink to this definition"></a></dt>
<dd><p>Equivalent to <code class="docutils literal"><span class="pre">print(nstr(x,</span> <span class="pre">n))</span></code>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="arithmetic-operations">
<h2>Arithmetic operations<a class="headerlink" href="#arithmetic-operations" title="Permalink to this headline"></a></h2>
<p>See also <a class="reference internal" href="functions/powers.html#mpmath.sqrt" title="mpmath.sqrt"><code class="xref py py-func docutils literal"><span class="pre">mpmath.sqrt()</span></code></a>, <a class="reference internal" href="functions/powers.html#mpmath.exp" title="mpmath.exp"><code class="xref py py-func docutils literal"><span class="pre">mpmath.exp()</span></code></a> etc., listed
in <a class="reference internal" href="functions/powers.html"><span class="doc">Powers and logarithms</span></a></p>
<div class="section" id="fadd">
<h3><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code><a class="headerlink" href="#fadd" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fadd">
<code class="descclassname">mpmath.</code><code class="descname">fadd</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fadd" title="Permalink to this definition"></a></dt>
<dd><p>Adds the numbers <em>x</em> and <em>y</em>, giving a floating-point result,
optionally using a custom precision and rounding mode.</p>
<p>The default precision is the working precision of the context.
You can specify a custom precision in bits by passing the <em>prec</em> keyword
argument, or by providing an equivalent decimal precision with the <em>dps</em>
keyword argument. If the precision is set to <code class="docutils literal"><span class="pre">+inf</span></code>, or if the flag
<em>exact=True</em> is passed, an exact addition with no rounding is performed.</p>
<p>When the precision is finite, the optional <em>rounding</em> keyword argument
specifies the direction of rounding. Valid options are <code class="docutils literal"><span class="pre">'n'</span></code> for
nearest (default), <code class="docutils literal"><span class="pre">'f'</span></code> for floor, <code class="docutils literal"><span class="pre">'c'</span></code> for ceiling, <code class="docutils literal"><span class="pre">'d'</span></code>
for down, <code class="docutils literal"><span class="pre">'u'</span></code> for up.</p>
<p><strong>Examples</strong></p>
<p>Using <a class="reference internal" href="#mpmath.fadd" title="mpmath.fadd"><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code></a> with precision and rounding control:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">)</span>
<span class="go">mpf(&#39;2.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="s1">&#39;u&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;2.0000000000000004&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="mi">100</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">2.00000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="mi">15</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">2.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="mi">25</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">2.00000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">2.00000000000000000001</span>
</pre></div>
</div>
<p>Exact addition avoids cancellation errors, enforcing familiar laws
of numbers such as <span class="math">\(x+y-x = y\)</span>, which don’t hold in floating-point
arithmetic with finite precision:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">mpf</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">mpf</span><span class="p">(</span><span class="s1">&#39;1e-1000&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fadd</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="n">inf</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span>
<span class="go">1.0e-1000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fadd</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span>
<span class="go">1.0e-1000</span>
</pre></div>
</div>
<p>Exact addition can be inefficient and may be impossible to perform
with large magnitude differences:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fadd</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;1e-100000000000000000000&#39;</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="n">inf</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">OverflowError</span>: <span class="n">the exact result does not fit in memory</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fsub">
<h3><code class="xref py py-func docutils literal"><span class="pre">fsub()</span></code><a class="headerlink" href="#fsub" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fsub">
<code class="descclassname">mpmath.</code><code class="descname">fsub</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fsub" title="Permalink to this definition"></a></dt>
<dd><p>Subtracts the numbers <em>x</em> and <em>y</em>, giving a floating-point result,
optionally using a custom precision and rounding mode.</p>
<p>See the documentation of <a class="reference internal" href="#mpmath.fadd" title="mpmath.fadd"><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code></a> for a detailed description
of how to specify precision and rounding.</p>
<p><strong>Examples</strong></p>
<p>Using <a class="reference internal" href="#mpmath.fsub" title="mpmath.fsub"><code class="xref py py-func docutils literal"><span class="pre">fsub()</span></code></a> with precision and rounding control:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fsub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">)</span>
<span class="go">mpf(&#39;2.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fsub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="s1">&#39;d&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;1.9999999999999998&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fsub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="mi">100</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">1.99999999999999999999</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fsub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="mi">15</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">2.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fsub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="mi">25</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">1.99999999999999999999</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fsub</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> <span class="mi">25</span><span class="p">)</span>
<span class="go">1.99999999999999999999</span>
</pre></div>
</div>
<p>Exact subtraction avoids cancellation errors, enforcing familiar laws
of numbers such as <span class="math">\(x-y+y = x\)</span>, which don’t hold in floating-point
arithmetic with finite precision:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">mpf</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">mpf</span><span class="p">(</span><span class="s1">&#39;1e1000&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fsub</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="n">inf</span><span class="p">)</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
<span class="go">2.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fsub</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span>
<span class="go">2.0</span>
</pre></div>
</div>
<p>Exact addition can be inefficient and may be impossible to perform
with large magnitude differences:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fsub</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;1e-100000000000000000000&#39;</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="n">inf</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">OverflowError</span>: <span class="n">the exact result does not fit in memory</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fneg">
<h3><code class="xref py py-func docutils literal"><span class="pre">fneg()</span></code><a class="headerlink" href="#fneg" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fneg">
<code class="descclassname">mpmath.</code><code class="descname">fneg</code><span class="sig-paren">(</span><em>x</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fneg" title="Permalink to this definition"></a></dt>
<dd><p>Negates the number <em>x</em>, giving a floating-point result, optionally
using a custom precision and rounding mode.</p>
<p>See the documentation of <a class="reference internal" href="#mpmath.fadd" title="mpmath.fadd"><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code></a> for a detailed description
of how to specify precision and rounding.</p>
<p><strong>Examples</strong></p>
<p>An mpmath number is returned:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fneg</span><span class="p">(</span><span class="mf">2.5</span><span class="p">)</span>
<span class="go">mpf(&#39;-2.5&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fneg</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpc(real=&#39;5.0&#39;, imag=&#39;-2.0&#39;)</span>
</pre></div>
</div>
<p>Precise control over rounding is possible:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">fadd</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1e-100</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fneg</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">mpf(&#39;-2.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fneg</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="s1">&#39;f&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;-2.0000000000000004&#39;)</span>
</pre></div>
</div>
<p>Negating with and without roundoff:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">200000000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="o">-</span><span class="n">mpf</span><span class="p">(</span><span class="n">n</span><span class="p">)))</span>
<span class="go">-200000000000000016777216</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fneg</span><span class="p">(</span><span class="n">n</span><span class="p">)))</span>
<span class="go">-200000000000000016777216</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fneg</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="n">log</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)))</span>
<span class="go">-200000000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fneg</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="n">log</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="mi">10</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)))</span>
<span class="go">-200000000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fneg</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="n">inf</span><span class="p">)))</span>
<span class="go">-200000000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fneg</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="n">inf</span><span class="p">)))</span>
<span class="go">-200000000000000000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fneg</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)))</span>
<span class="go">-200000000000000000000001</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fmul">
<h3><code class="xref py py-func docutils literal"><span class="pre">fmul()</span></code><a class="headerlink" href="#fmul" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fmul">
<code class="descclassname">mpmath.</code><code class="descname">fmul</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fmul" title="Permalink to this definition"></a></dt>
<dd><p>Multiplies the numbers <em>x</em> and <em>y</em>, giving a floating-point result,
optionally using a custom precision and rounding mode.</p>
<p>See the documentation of <a class="reference internal" href="#mpmath.fadd" title="mpmath.fadd"><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code></a> for a detailed description
of how to specify precision and rounding.</p>
<p><strong>Examples</strong></p>
<p>The result is an mpmath number:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmul</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">5.0</span><span class="p">)</span>
<span class="go">mpf(&#39;10.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmul</span><span class="p">(</span><span class="mf">0.5</span><span class="n">j</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
<span class="go">mpc(real=&#39;0.0&#39;, imag=&#39;0.25&#39;)</span>
</pre></div>
</div>
<p>Avoiding roundoff:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">10</span><span class="o">**</span><span class="mi">10</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="o">**</span><span class="mi">15</span><span class="o">+</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span>
<span class="go">10000000001000010000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="n">mpf</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="go">1.0000000001e+25</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">*</span> <span class="n">mpf</span><span class="p">(</span><span class="n">y</span><span class="p">)))</span>
<span class="go">10000000001000011026399232</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)))</span>
<span class="go">10000000001000011026399232</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="mi">25</span><span class="p">)))</span>
<span class="go">10000000001000010000000001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">fmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)))</span>
<span class="go">10000000001000010000000001</span>
</pre></div>
</div>
<p>Exact multiplication with complex numbers can be inefficient and may
be impossible to perform with large magnitude differences between
real and imaginary parts:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">mpc</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;1e-100000000000000000000&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">mpc(real=&#39;2.0&#39;, imag=&#39;4.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="s1">&#39;u&#39;</span><span class="p">)</span>
<span class="go">mpc(real=&#39;2.0&#39;, imag=&#39;4.0000000000000009&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmul</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">OverflowError</span>: <span class="n">the exact result does not fit in memory</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fdiv">
<h3><code class="xref py py-func docutils literal"><span class="pre">fdiv()</span></code><a class="headerlink" href="#fdiv" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fdiv">
<code class="descclassname">mpmath.</code><code class="descname">fdiv</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fdiv" title="Permalink to this definition"></a></dt>
<dd><p>Divides the numbers <em>x</em> and <em>y</em>, giving a floating-point result,
optionally using a custom precision and rounding mode.</p>
<p>See the documentation of <a class="reference internal" href="#mpmath.fadd" title="mpmath.fadd"><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code></a> for a detailed description
of how to specify precision and rounding.</p>
<p><strong>Examples</strong></p>
<p>The result is an mpmath number:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">mpf(&#39;1.5&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;0.66666666666666663&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">)</span>
<span class="go">mpc(real=&#39;4.0&#39;, imag=&#39;8.0&#39;)</span>
</pre></div>
</div>
<p>The rounding direction and precision can be controlled:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">dps</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>    <span class="c1"># Should be accurate to at least 3 digits</span>
<span class="go">mpf(&#39;0.6666259765625&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="s1">&#39;d&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;0.66666666666666663&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
<span class="go">mpf(&#39;0.66666666666666667&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="s1">&#39;u&#39;</span><span class="p">)</span>
<span class="go">mpf(&#39;0.66666666666666674&#39;)</span>
</pre></div>
</div>
<p>Checking the error of a division by performing it at higher precision:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">-</span> <span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="go">mpf(&#39;-3.7007434154172148e-17&#39;)</span>
</pre></div>
</div>
<p>Unlike <a class="reference internal" href="#mpmath.fadd" title="mpmath.fadd"><code class="xref py py-func docutils literal"><span class="pre">fadd()</span></code></a>, <a class="reference internal" href="#mpmath.fmul" title="mpmath.fmul"><code class="xref py py-func docutils literal"><span class="pre">fmul()</span></code></a>, etc., exact division is not
allowed since the quotient of two floating-point numbers generally
does not have an exact floating-point representation. (In the
future this might be changed to allow the case where the division
is actually exact.)</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fdiv</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">exact</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">division is not an exact operation</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fmod">
<h3><code class="xref py py-func docutils literal"><span class="pre">fmod()</span></code><a class="headerlink" href="#fmod" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fmod">
<code class="descclassname">mpmath.</code><code class="descname">fmod</code><span class="sig-paren">(</span><em>x</em>, <em>y</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fmod" title="Permalink to this definition"></a></dt>
<dd><p>Converts <span class="math">\(x\)</span> and <span class="math">\(y\)</span> to mpmath numbers and returns <span class="math">\(x \mod y\)</span>.
For mpmath numbers, this is equivalent to <code class="docutils literal"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmod</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="n">pi</span><span class="p">)</span>
<span class="go">2.61062773871641</span>
</pre></div>
</div>
<p>You can use <a class="reference internal" href="#mpmath.fmod" title="mpmath.fmod"><code class="xref py py-func docutils literal"><span class="pre">fmod()</span></code></a> to compute fractional parts of numbers:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fmod</span><span class="p">(</span><span class="mf">10.25</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">0.25</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fsum">
<h3><code class="xref py py-func docutils literal"><span class="pre">fsum()</span></code><a class="headerlink" href="#fsum" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fsum">
<code class="descclassname">mpmath.</code><code class="descname">fsum</code><span class="sig-paren">(</span><em>terms</em>, <em>absolute=False</em>, <em>squared=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fsum" title="Permalink to this definition"></a></dt>
<dd><p>Calculates a sum containing a finite number of terms (for infinite
series, see <a class="reference internal" href="calculus/sums_limits.html#mpmath.nsum" title="mpmath.nsum"><code class="xref py py-func docutils literal"><span class="pre">nsum()</span></code></a>). The terms will be converted to
mpmath numbers. For len(terms) &gt; 2, this function is generally
faster and produces more accurate results than the builtin
Python function <code class="xref py py-func docutils literal"><span class="pre">sum()</span></code>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fsum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>
<span class="go">mpf(&#39;10.5&#39;)</span>
</pre></div>
</div>
<p>With squared=True each term is squared, and with absolute=True
the absolute value of each term is used.</p>
</dd></dl>

</div>
<div class="section" id="fprod">
<h3><code class="xref py py-func docutils literal"><span class="pre">fprod()</span></code><a class="headerlink" href="#fprod" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fprod">
<code class="descclassname">mpmath.</code><code class="descname">fprod</code><span class="sig-paren">(</span><em>factors</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fprod" title="Permalink to this definition"></a></dt>
<dd><p>Calculates a product containing a finite number of factors (for
infinite products, see <a class="reference internal" href="calculus/sums_limits.html#mpmath.nprod" title="mpmath.nprod"><code class="xref py py-func docutils literal"><span class="pre">nprod()</span></code></a>). The factors will be
converted to mpmath numbers.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fprod</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>
<span class="go">mpf(&#39;7.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="fdot">
<h3><code class="xref py py-func docutils literal"><span class="pre">fdot()</span></code><a class="headerlink" href="#fdot" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fdot">
<code class="descclassname">mpmath.</code><code class="descname">fdot</code><span class="sig-paren">(</span><em>A</em>, <em>B=None</em>, <em>conjugate=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fdot" title="Permalink to this definition"></a></dt>
<dd><p>Computes the dot product of the iterables <span class="math">\(A\)</span> and <span class="math">\(B\)</span>,</p>
<div class="math">
\[\sum_{k=0} A_k B_k.\]</div>
<p>Alternatively, <a class="reference internal" href="#mpmath.fdot" title="mpmath.fdot"><code class="xref py py-func docutils literal"><span class="pre">fdot()</span></code></a> accepts a single iterable of pairs.
In other words, <code class="docutils literal"><span class="pre">fdot(A,B)</span></code> and <code class="docutils literal"><span class="pre">fdot(zip(A,B))</span></code> are equivalent.
The elements are automatically converted to mpmath numbers.</p>
<p>With <code class="docutils literal"><span class="pre">conjugate=True</span></code>, the elements in the second vector
will be conjugated:</p>
<div class="math">
\[\sum_{k=0} A_k \overline{B_k}\]</div>
<p><strong>Examples</strong></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdot</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
<span class="go">mpf(&#39;6.5&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">))</span>
<span class="go">[(2, 1), (1.5, -1), (3, 2)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdot</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">mpf(&#39;6.5&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">,</span> <span class="mi">3</span><span class="n">j</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="o">+</span><span class="n">j</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="n">j</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdot</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
<span class="go">mpc(real=&#39;9.5&#39;, imag=&#39;-1.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fdot</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">,</span> <span class="n">conjugate</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">mpc(real=&#39;3.5&#39;, imag=&#39;-5.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="complex-components">
<h2>Complex components<a class="headerlink" href="#complex-components" title="Permalink to this headline"></a></h2>
<div class="section" id="fabs">
<h3><code class="xref py py-func docutils literal"><span class="pre">fabs()</span></code><a class="headerlink" href="#fabs" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fabs">
<code class="descclassname">mpmath.</code><code class="descname">fabs</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fabs" title="Permalink to this definition"></a></dt>
<dd><p>Returns the absolute value of <span class="math">\(x\)</span>, <span class="math">\(|x|\)</span>. Unlike <code class="xref py py-func docutils literal"><span class="pre">abs()</span></code>,
<a class="reference internal" href="#mpmath.fabs" title="mpmath.fabs"><code class="xref py py-func docutils literal"><span class="pre">fabs()</span></code></a> converts non-mpmath numbers (such as <code class="docutils literal"><span class="pre">int</span></code>)
into mpmath numbers:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fabs</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;3.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fabs</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;3.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fabs</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpf(&#39;5.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="sign">
<h3><code class="xref py py-func docutils literal"><span class="pre">sign()</span></code><a class="headerlink" href="#sign" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.sign">
<code class="descclassname">mpmath.</code><code class="descname">sign</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.sign" title="Permalink to this definition"></a></dt>
<dd><p>Returns the sign of <span class="math">\(x\)</span>, defined as <span class="math">\(\mathrm{sign}(x) = x / |x|\)</span>
(with the special case <span class="math">\(\mathrm{sign}(0) = 0\)</span>):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sign</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">mpf(&#39;1.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sign</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span>
<span class="go">mpf(&#39;-1.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sign</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">mpf(&#39;0.0&#39;)</span>
</pre></div>
</div>
<p>Note that the sign function is also defined for complex numbers,
for which it gives the projection onto the unit circle:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sign</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="n">j</span><span class="p">)</span>
<span class="go">(0.707106781186547 + 0.707106781186547j)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="re">
<h3><code class="xref py py-func docutils literal"><span class="pre">re()</span></code><a class="headerlink" href="#re" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.re">
<code class="descclassname">mpmath.</code><code class="descname">re</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.re" title="Permalink to this definition"></a></dt>
<dd><p>Returns the real part of <span class="math">\(x\)</span>, <span class="math">\(\Re(x)\)</span>. <a class="reference internal" href="#mpmath.re" title="mpmath.re"><code class="xref py py-func docutils literal"><span class="pre">re()</span></code></a>
converts a non-mpmath number to an mpmath number:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;3.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpf(&#39;-1.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="im">
<h3><code class="xref py py-func docutils literal"><span class="pre">im()</span></code><a class="headerlink" href="#im" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.im">
<code class="descclassname">mpmath.</code><code class="descname">im</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.im" title="Permalink to this definition"></a></dt>
<dd><p>Returns the imaginary part of <span class="math">\(x\)</span>, <span class="math">\(\Im(x)\)</span>. <a class="reference internal" href="#mpmath.im" title="mpmath.im"><code class="xref py py-func docutils literal"><span class="pre">im()</span></code></a>
converts a non-mpmath number to an mpmath number:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">im</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;0.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">im</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpf(&#39;4.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="arg">
<h3><code class="xref py py-func docutils literal"><span class="pre">arg()</span></code><a class="headerlink" href="#arg" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.arg">
<code class="descclassname">mpmath.</code><code class="descname">arg</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.arg" title="Permalink to this definition"></a></dt>
<dd><p>Computes the complex argument (phase) of <span class="math">\(x\)</span>, defined as the
signed angle between the positive real axis and <span class="math">\(x\)</span> in the
complex plane:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arg</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arg</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="go">0.785398163397448</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arg</span><span class="p">(</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="go">1.5707963267949</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arg</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">3.14159265358979</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arg</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="go">-1.5707963267949</span>
</pre></div>
</div>
<p>The angle is defined to satisfy <span class="math">\(-\pi &lt; \arg(x) \le \pi\)</span> and
with the sign convention that a nonnegative imaginary part
results in a nonnegative argument.</p>
<p>The value returned by <a class="reference internal" href="#mpmath.arg" title="mpmath.arg"><code class="xref py py-func docutils literal"><span class="pre">arg()</span></code></a> is an <code class="docutils literal"><span class="pre">mpf</span></code> instance.</p>
</dd></dl>

</div>
<div class="section" id="conj">
<h3><code class="xref py py-func docutils literal"><span class="pre">conj()</span></code><a class="headerlink" href="#conj" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.conj">
<code class="descclassname">mpmath.</code><code class="descname">conj</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.conj" title="Permalink to this definition"></a></dt>
<dd><p>Returns the complex conjugate of <span class="math">\(x\)</span>, <span class="math">\(\overline{x}\)</span>. Unlike
<code class="docutils literal"><span class="pre">x.conjugate()</span></code>, <a class="reference internal" href="#mpmath.im" title="mpmath.im"><code class="xref py py-func docutils literal"><span class="pre">im()</span></code></a> converts <span class="math">\(x\)</span> to a mpmath number:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conj</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;3.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conj</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpc(real=&#39;-1.0&#39;, imag=&#39;-4.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="polar">
<h3><code class="xref py py-func docutils literal"><span class="pre">polar()</span></code><a class="headerlink" href="#polar" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.polar">
<code class="descclassname">mpmath.</code><code class="descname">polar</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.polar" title="Permalink to this definition"></a></dt>
<dd><p>Returns the polar representation of the complex number <span class="math">\(z\)</span>
as a pair <span class="math">\((r, \phi)\)</span> such that <span class="math">\(z = r e^{i \phi}\)</span>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">polar</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(2.0, 3.14159265358979)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">polar</span><span class="p">(</span><span class="mi">3</span><span class="o">-</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">(5.0, -0.927295218001612)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="rect">
<h3><code class="xref py py-func docutils literal"><span class="pre">rect()</span></code><a class="headerlink" href="#rect" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.rect">
<code class="descclassname">mpmath.</code><code class="descname">rect</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.rect" title="Permalink to this definition"></a></dt>
<dd><p>Returns the complex number represented by polar
coordinates <span class="math">\((r, \phi)\)</span>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chop</span><span class="p">(</span><span class="n">rect</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">pi</span><span class="p">))</span>
<span class="go">-2.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rect</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="o">-</span><span class="n">pi</span><span class="o">/</span><span class="mi">4</span><span class="p">)</span>
<span class="go">(1.0 - 1.0j)</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="integer-and-fractional-parts">
<h2>Integer and fractional parts<a class="headerlink" href="#integer-and-fractional-parts" title="Permalink to this headline"></a></h2>
<div class="section" id="floor">
<h3><code class="xref py py-func docutils literal"><span class="pre">floor()</span></code><a class="headerlink" href="#floor" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.floor">
<code class="descclassname">mpmath.</code><code class="descname">floor</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.floor" title="Permalink to this definition"></a></dt>
<dd><p>Computes the floor of <span class="math">\(x\)</span>, <span class="math">\(\lfloor x \rfloor\)</span>, defined as
the largest integer less than or equal to <span class="math">\(x\)</span>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">floor</span><span class="p">(</span><span class="mf">3.5</span><span class="p">)</span>
<span class="go">mpf(&#39;3.0&#39;)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference internal" href="#mpmath.floor" title="mpmath.floor"><code class="xref py py-func docutils literal"><span class="pre">floor()</span></code></a>, <a class="reference internal" href="#mpmath.ceil" title="mpmath.ceil"><code class="xref py py-func docutils literal"><span class="pre">ceil()</span></code></a> and <a class="reference internal" href="#mpmath.nint" title="mpmath.nint"><code class="xref py py-func docutils literal"><span class="pre">nint()</span></code></a> return a
floating-point number, not a Python <code class="docutils literal"><span class="pre">int</span></code>. If <span class="math">\(\lfloor x \rfloor\)</span> is
too large to be represented exactly at the present working precision,
the result will be rounded, not necessarily in the direction
implied by the mathematical definition of the function.</p>
</div>
<p>To avoid rounding, use <em>prec=0</em>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="mi">10</span><span class="o">**</span><span class="mi">30</span><span class="o">+</span><span class="mi">1</span><span class="p">)))</span>
<span class="go">1000000000000000019884624838656</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">floor</span><span class="p">(</span><span class="mi">10</span><span class="o">**</span><span class="mi">30</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">prec</span><span class="o">=</span><span class="mi">0</span><span class="p">)))</span>
<span class="go">1000000000000000000000000000001</span>
</pre></div>
</div>
<p>The floor function is defined for complex numbers and
acts on the real and imaginary parts separately:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">floor</span><span class="p">(</span><span class="mf">3.25</span><span class="o">+</span><span class="mf">4.75</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpc(real=&#39;3.0&#39;, imag=&#39;4.0&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="ceil">
<h3><code class="xref py py-func docutils literal"><span class="pre">ceil()</span></code><a class="headerlink" href="#ceil" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.ceil">
<code class="descclassname">mpmath.</code><code class="descname">ceil</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.ceil" title="Permalink to this definition"></a></dt>
<dd><p>Computes the ceiling of <span class="math">\(x\)</span>, <span class="math">\(\lceil x \rceil\)</span>, defined as
the smallest integer greater than or equal to <span class="math">\(x\)</span>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ceil</span><span class="p">(</span><span class="mf">3.5</span><span class="p">)</span>
<span class="go">mpf(&#39;4.0&#39;)</span>
</pre></div>
</div>
<p>The ceiling function is defined for complex numbers and
acts on the real and imaginary parts separately:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ceil</span><span class="p">(</span><span class="mf">3.25</span><span class="o">+</span><span class="mf">4.75</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpc(real=&#39;4.0&#39;, imag=&#39;5.0&#39;)</span>
</pre></div>
</div>
<p>See notes about rounding for <a class="reference internal" href="#mpmath.floor" title="mpmath.floor"><code class="xref py py-func docutils literal"><span class="pre">floor()</span></code></a>.</p>
</dd></dl>

</div>
<div class="section" id="nint">
<h3><code class="xref py py-func docutils literal"><span class="pre">nint()</span></code><a class="headerlink" href="#nint" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.nint">
<code class="descclassname">mpmath.</code><code class="descname">nint</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.nint" title="Permalink to this definition"></a></dt>
<dd><p>Evaluates the nearest integer function, <span class="math">\(\mathrm{nint}(x)\)</span>.
This gives the nearest integer to <span class="math">\(x\)</span>; on a tie, it
gives the nearest even integer:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nint</span><span class="p">(</span><span class="mf">3.2</span><span class="p">)</span>
<span class="go">mpf(&#39;3.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nint</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span>
<span class="go">mpf(&#39;4.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nint</span><span class="p">(</span><span class="mf">3.5</span><span class="p">)</span>
<span class="go">mpf(&#39;4.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nint</span><span class="p">(</span><span class="mf">4.5</span><span class="p">)</span>
<span class="go">mpf(&#39;4.0&#39;)</span>
</pre></div>
</div>
<p>The nearest integer function is defined for complex numbers and
acts on the real and imaginary parts separately:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">nint</span><span class="p">(</span><span class="mf">3.25</span><span class="o">+</span><span class="mf">4.75</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpc(real=&#39;3.0&#39;, imag=&#39;5.0&#39;)</span>
</pre></div>
</div>
<p>See notes about rounding for <a class="reference internal" href="#mpmath.floor" title="mpmath.floor"><code class="xref py py-func docutils literal"><span class="pre">floor()</span></code></a>.</p>
</dd></dl>

</div>
<div class="section" id="frac">
<h3><code class="xref py py-func docutils literal"><span class="pre">frac()</span></code><a class="headerlink" href="#frac" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.frac">
<code class="descclassname">mpmath.</code><code class="descname">frac</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.frac" title="Permalink to this definition"></a></dt>
<dd><p>Gives the fractional part of <span class="math">\(x\)</span>, defined as
<span class="math">\(\mathrm{frac}(x) = x - \lfloor x \rfloor\)</span> (see <a class="reference internal" href="#mpmath.floor" title="mpmath.floor"><code class="xref py py-func docutils literal"><span class="pre">floor()</span></code></a>).
In effect, this computes <span class="math">\(x\)</span> modulo 1, or <span class="math">\(x+n\)</span> where
<span class="math">\(n \in \mathbb{Z}\)</span> is such that <span class="math">\(x+n \in [0,1)\)</span>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span><span class="p">(</span><span class="mf">1.25</span><span class="p">)</span>
<span class="go">mpf(&#39;0.25&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;0.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span><span class="p">(</span><span class="o">-</span><span class="mf">1.25</span><span class="p">)</span>
<span class="go">mpf(&#39;0.75&#39;)</span>
</pre></div>
</div>
<p>For a complex number, the fractional part function applies to
the real and imaginary parts separately:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span><span class="p">(</span><span class="mf">2.25</span><span class="o">+</span><span class="mf">3.75</span><span class="n">j</span><span class="p">)</span>
<span class="go">mpc(real=&#39;0.25&#39;, imag=&#39;0.75&#39;)</span>
</pre></div>
</div>
<p>Plotted, the fractional part function gives a sawtooth
wave. The Fourier series coefficients have a simple
form:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">fourier</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">frac</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="mi">4</span><span class="p">))</span>
<span class="go">([0.0, 0.0, 0.0, 0.0, 0.0], [0.0, -0.31831, -0.159155, -0.106103, -0.0795775])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">pi</span><span class="o">*</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">)])</span>
<span class="go">[-0.31831, -0.159155, -0.106103, -0.0795775]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The fractional part is sometimes defined as a symmetric
function, i.e. returning <span class="math">\(-\mathrm{frac}(-x)\)</span> if <span class="math">\(x &lt; 0\)</span>.
This convention is used, for instance, by Mathematica’s
<code class="docutils literal"><span class="pre">FractionalPart</span></code>.</p>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="tolerances-and-approximate-comparisons">
<h2>Tolerances and approximate comparisons<a class="headerlink" href="#tolerances-and-approximate-comparisons" title="Permalink to this headline"></a></h2>
<div class="section" id="chop">
<h3><code class="xref py py-func docutils literal"><span class="pre">chop()</span></code><a class="headerlink" href="#chop" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.chop">
<code class="descclassname">mpmath.</code><code class="descname">chop</code><span class="sig-paren">(</span><em>x</em>, <em>tol=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.chop" title="Permalink to this definition"></a></dt>
<dd><p>Chops off small real or imaginary parts, or converts
numbers close to zero to exact zeros. The input can be a
single number or an iterable:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">chop</span><span class="p">(</span><span class="mi">5</span><span class="o">+</span><span class="mf">1e-10j</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-9</span><span class="p">)</span>
<span class="go">mpf(&#39;5.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nprint</span><span class="p">(</span><span class="n">chop</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">1e-20</span><span class="p">,</span> <span class="mi">3</span><span class="o">+</span><span class="mf">1e-18j</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">]))</span>
<span class="go">[1.0, 0.0, 3.0, -4.0, 2.0]</span>
</pre></div>
</div>
<p>The tolerance defaults to <code class="docutils literal"><span class="pre">100*eps</span></code>.</p>
</dd></dl>

</div>
<div class="section" id="almosteq">
<h3><code class="xref py py-func docutils literal"><span class="pre">almosteq()</span></code><a class="headerlink" href="#almosteq" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.almosteq">
<code class="descclassname">mpmath.</code><code class="descname">almosteq</code><span class="sig-paren">(</span><em>s</em>, <em>t</em>, <em>rel_eps=None</em>, <em>abs_eps=None</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.almosteq" title="Permalink to this definition"></a></dt>
<dd><p>Determine whether the difference between <span class="math">\(s\)</span> and <span class="math">\(t\)</span> is smaller
than a given epsilon, either relatively or absolutely.</p>
<p>Both a maximum relative difference and a maximum difference
(‘epsilons’) may be specified. The absolute difference is
defined as <span class="math">\(|s-t|\)</span> and the relative difference is defined
as <span class="math">\(|s-t|/\max(|s|, |t|)\)</span>.</p>
<p>If only one epsilon is given, both are set to the same value.
If none is given, both epsilons are set to <span class="math">\(2^{-p+m}\)</span> where
<span class="math">\(p\)</span> is the current working precision and <span class="math">\(m\)</span> is a small
integer. The default setting typically allows <a class="reference internal" href="#mpmath.almosteq" title="mpmath.almosteq"><code class="xref py py-func docutils literal"><span class="pre">almosteq()</span></code></a>
to be used to check for mathematical equality
in the presence of small rounding errors.</p>
<p><strong>Examples</strong></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">almosteq</span><span class="p">(</span><span class="mf">3.141592653589793</span><span class="p">,</span> <span class="mf">3.141592653589790</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">almosteq</span><span class="p">(</span><span class="mf">3.141592653589793</span><span class="p">,</span> <span class="mf">3.141592653589700</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">almosteq</span><span class="p">(</span><span class="mf">3.141592653589793</span><span class="p">,</span> <span class="mf">3.141592653589700</span><span class="p">,</span> <span class="mf">1e-10</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">almosteq</span><span class="p">(</span><span class="mf">1e-20</span><span class="p">,</span> <span class="mf">2e-20</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">almosteq</span><span class="p">(</span><span class="mf">1e-20</span><span class="p">,</span> <span class="mf">2e-20</span><span class="p">,</span> <span class="n">rel_eps</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">abs_eps</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="properties-of-numbers">
<h2>Properties of numbers<a class="headerlink" href="#properties-of-numbers" title="Permalink to this headline"></a></h2>
<div class="section" id="isinf">
<h3><code class="xref py py-func docutils literal"><span class="pre">isinf()</span></code><a class="headerlink" href="#isinf" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.isinf">
<code class="descclassname">mpmath.</code><code class="descname">isinf</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.isinf" title="Permalink to this definition"></a></dt>
<dd><p>Return <em>True</em> if the absolute value of <em>x</em> is infinite;
otherwise return <em>False</em>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isinf</span><span class="p">(</span><span class="n">inf</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isinf</span><span class="p">(</span><span class="o">-</span><span class="n">inf</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isinf</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isinf</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isinf</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="n">inf</span><span class="p">))</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isinf</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="n">inf</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="isnan">
<h3><code class="xref py py-func docutils literal"><span class="pre">isnan()</span></code><a class="headerlink" href="#isnan" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.isnan">
<code class="descclassname">mpmath.</code><code class="descname">isnan</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.isnan" title="Permalink to this definition"></a></dt>
<dd><p>Return <em>True</em> if <em>x</em> is a NaN (not-a-number), or for a complex
number, whether either the real or complex part is NaN;
otherwise return <em>False</em>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnan</span><span class="p">(</span><span class="mf">3.14</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnan</span><span class="p">(</span><span class="n">nan</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnan</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span><span class="mf">2.72</span><span class="p">))</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnan</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span><span class="n">nan</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="isnormal">
<h3><code class="xref py py-func docutils literal"><span class="pre">isnormal()</span></code><a class="headerlink" href="#isnormal" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.isnormal">
<code class="descclassname">mpmath.</code><code class="descname">isnormal</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.isnormal" title="Permalink to this definition"></a></dt>
<dd><p>Determine whether <em>x</em> is “normal” in the sense of floating-point
representation; that is, return <em>False</em> if <em>x</em> is zero, an
infinity or NaN; otherwise return <em>True</em>. By extension, a
complex number <em>x</em> is considered “normal” if its magnitude is
normal:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnormal</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnormal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnormal</span><span class="p">(</span><span class="n">inf</span><span class="p">);</span> <span class="n">isnormal</span><span class="p">(</span><span class="o">-</span><span class="n">inf</span><span class="p">);</span> <span class="n">isnormal</span><span class="p">(</span><span class="n">nan</span><span class="p">)</span>
<span class="go">False</span>
<span class="go">False</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnormal</span><span class="p">(</span><span class="mi">0</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnormal</span><span class="p">(</span><span class="mi">0</span><span class="o">+</span><span class="mi">3</span><span class="n">j</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isnormal</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">nan</span><span class="p">))</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="isfinite">
<h3><code class="xref py py-func docutils literal"><span class="pre">isfinite()</span></code><a class="headerlink" href="#isfinite" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.isfinite">
<code class="descclassname">mpmath.</code><code class="descname">isfinite</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.isfinite" title="Permalink to this definition"></a></dt>
<dd><p>Return <em>True</em> if <em>x</em> is a finite number, i.e. neither
an infinity or a NaN.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="n">inf</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="o">-</span><span class="n">inf</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="n">nan</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">4</span><span class="n">j</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="n">inf</span><span class="p">))</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isfinite</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="n">nan</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="isint">
<h3><code class="xref py py-func docutils literal"><span class="pre">isint()</span></code><a class="headerlink" href="#isint" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.isint">
<code class="descclassname">mpmath.</code><code class="descname">isint</code><span class="sig-paren">(</span><em>x</em>, <em>gaussian=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.isint" title="Permalink to this definition"></a></dt>
<dd><p>Return <em>True</em> if <em>x</em> is integer-valued; otherwise return
<em>False</em>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="mf">3.2</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="n">inf</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Optionally, Gaussian integers can be checked for:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isint</span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span><span class="p">,</span> <span class="n">gaussian</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="ldexp">
<h3><code class="xref py py-func docutils literal"><span class="pre">ldexp()</span></code><a class="headerlink" href="#ldexp" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.ldexp">
<code class="descclassname">mpmath.</code><code class="descname">ldexp</code><span class="sig-paren">(</span><em>x</em>, <em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.ldexp" title="Permalink to this definition"></a></dt>
<dd><p>Computes <span class="math">\(x 2^n\)</span> efficiently. No rounding is performed.
The argument <span class="math">\(x\)</span> must be a real floating-point number (or
possible to convert into one) and <span class="math">\(n\)</span> must be a Python <code class="docutils literal"><span class="pre">int</span></code>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ldexp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">mpf(&#39;1024.0&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ldexp</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="go">mpf(&#39;0.125&#39;)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="frexp">
<h3><code class="xref py py-func docutils literal"><span class="pre">frexp()</span></code><a class="headerlink" href="#frexp" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.frexp">
<code class="descclassname">mpmath.</code><code class="descname">frexp</code><span class="sig-paren">(</span><em>x</em>, <em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.frexp" title="Permalink to this definition"></a></dt>
<dd><p>Given a real number <span class="math">\(x\)</span>, returns <span class="math">\((y, n)\)</span> with <span class="math">\(y \in [0.5, 1)\)</span>,
<span class="math">\(n\)</span> a Python integer, and such that <span class="math">\(x = y 2^n\)</span>. No rounding is
performed.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frexp</span><span class="p">(</span><span class="mf">7.5</span><span class="p">)</span>
<span class="go">(mpf(&#39;0.9375&#39;), 3)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="mag">
<h3><code class="xref py py-func docutils literal"><span class="pre">mag()</span></code><a class="headerlink" href="#mag" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.mag">
<code class="descclassname">mpmath.</code><code class="descname">mag</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.mag" title="Permalink to this definition"></a></dt>
<dd><p>Quick logarithmic magnitude estimate of a number. Returns an
integer or infinity <span class="math">\(m\)</span> such that <span class="math">\(|x| &lt;= 2^m\)</span>. It is not
guaranteed that <span class="math">\(m\)</span> is an optimal bound, but it will never
be too large by more than 2 (and probably not more than 1).</p>
<p><strong>Examples</strong></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mag</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">mag</span><span class="p">(</span><span class="mf">10.0</span><span class="p">),</span> <span class="n">mag</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="mi">10</span><span class="p">)),</span> <span class="nb">int</span><span class="p">(</span><span class="n">ceil</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="go">(4, 4, 4, 4)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mag</span><span class="p">(</span><span class="mi">10</span><span class="n">j</span><span class="p">),</span> <span class="n">mag</span><span class="p">(</span><span class="mi">10</span><span class="o">+</span><span class="mi">10</span><span class="n">j</span><span class="p">)</span>
<span class="go">(4, 5)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mag</span><span class="p">(</span><span class="mf">0.01</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">ceil</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="mf">0.01</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="go">(-6, -6)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mag</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">mag</span><span class="p">(</span><span class="n">inf</span><span class="p">),</span> <span class="n">mag</span><span class="p">(</span><span class="o">-</span><span class="n">inf</span><span class="p">),</span> <span class="n">mag</span><span class="p">(</span><span class="n">nan</span><span class="p">)</span>
<span class="go">(-inf, +inf, +inf, nan)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="nint-distance">
<h3><code class="xref py py-func docutils literal"><span class="pre">nint_distance()</span></code><a class="headerlink" href="#nint-distance" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.nint_distance">
<code class="descclassname">mpmath.</code><code class="descname">nint_distance</code><span class="sig-paren">(</span><em>x</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.nint_distance" title="Permalink to this definition"></a></dt>
<dd><p>Return <span class="math">\((n,d)\)</span> where <span class="math">\(n\)</span> is the nearest integer to <span class="math">\(x\)</span> and <span class="math">\(d\)</span> is
an estimate of <span class="math">\(\log_2(|x-n|)\)</span>. If <span class="math">\(d &lt; 0\)</span>, <span class="math">\(-d\)</span> gives the precision
(measured in bits) lost to cancellation when computing <span class="math">\(x-n\)</span>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">nint_distance</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">5</span>
<span class="go">-inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">nint_distance</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">5</span>
<span class="go">-inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">nint_distance</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="mf">5.00000001</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">5</span>
<span class="go">-26</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">nint_distance</span><span class="p">(</span><span class="n">mpf</span><span class="p">(</span><span class="mf">4.99999999</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">5</span>
<span class="go">-26</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">nint_distance</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">5</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">nint_distance</span><span class="p">(</span><span class="n">mpc</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mf">0.000001</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">5</span>
<span class="go">-19</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="number-generation">
<h2>Number generation<a class="headerlink" href="#number-generation" title="Permalink to this headline"></a></h2>
<div class="section" id="fraction">
<h3><code class="xref py py-func docutils literal"><span class="pre">fraction()</span></code><a class="headerlink" href="#fraction" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.fraction">
<code class="descclassname">mpmath.</code><code class="descname">fraction</code><span class="sig-paren">(</span><em>p</em>, <em>q</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.fraction" title="Permalink to this definition"></a></dt>
<dd><p>Given Python integers <span class="math">\((p, q)\)</span>, returns a lazy <code class="docutils literal"><span class="pre">mpf</span></code> representing
the fraction <span class="math">\(p/q\)</span>. The value is updated with the precision.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">fraction</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">100</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">mpf</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">0.01</span>
<span class="go">0.01</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">30</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">);</span> <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>      <span class="c1"># a will be accurate</span>
<span class="go">0.01</span>
<span class="go">0.0100000000000000002081668171172</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="rand">
<h3><code class="xref py py-func docutils literal"><span class="pre">rand()</span></code><a class="headerlink" href="#rand" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.rand">
<code class="descclassname">mpmath.</code><code class="descname">rand</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.rand" title="Permalink to this definition"></a></dt>
<dd><p>Returns an <code class="docutils literal"><span class="pre">mpf</span></code> with value chosen randomly from <span class="math">\([0, 1)\)</span>.
The number of randomly generated bits in the mantissa is equal
to the working precision.</p>
</dd></dl>

</div>
<div class="section" id="arange">
<h3><code class="xref py py-func docutils literal"><span class="pre">arange()</span></code><a class="headerlink" href="#arange" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.arange">
<code class="descclassname">mpmath.</code><code class="descname">arange</code><span class="sig-paren">(</span><em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.arange" title="Permalink to this definition"></a></dt>
<dd><p>This is a generalized version of Python’s <code class="xref py py-func docutils literal"><span class="pre">range()</span></code> function
that accepts fractional endpoints and step sizes and
returns a list of <code class="docutils literal"><span class="pre">mpf</span></code> instances. Like <code class="xref py py-func docutils literal"><span class="pre">range()</span></code>,
<a class="reference internal" href="#mpmath.arange" title="mpmath.arange"><code class="xref py py-func docutils literal"><span class="pre">arange()</span></code></a> can be called with 1, 2 or 3 arguments:</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">arange(b)</span></code></dt>
<dd><span class="math">\([0, 1, 2, \ldots, x]\)</span></dd>
<dt><code class="docutils literal"><span class="pre">arange(a,</span> <span class="pre">b)</span></code></dt>
<dd><span class="math">\([a, a+1, a+2, \ldots, x]\)</span></dd>
<dt><code class="docutils literal"><span class="pre">arange(a,</span> <span class="pre">b,</span> <span class="pre">h)</span></code></dt>
<dd><span class="math">\([a, a+h, a+h, \ldots, x]\)</span></dd>
</dl>
<p>where <span class="math">\(b-1 \le x &lt; b\)</span> (in the third case, <span class="math">\(b-h \le x &lt; b\)</span>).</p>
<p>Like Python’s <code class="xref py py-func docutils literal"><span class="pre">range()</span></code>, the endpoint is not included. To
produce ranges where the endpoint is included, <a class="reference internal" href="#mpmath.linspace" title="mpmath.linspace"><code class="xref py py-func docutils literal"><span class="pre">linspace()</span></code></a>
is more convenient.</p>
<p><strong>Examples</strong></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="go">[mpf(&#39;0.0&#39;), mpf(&#39;1.0&#39;), mpf(&#39;2.0&#39;), mpf(&#39;3.0&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">0.25</span><span class="p">)</span>
<span class="go">[mpf(&#39;1.0&#39;), mpf(&#39;1.25&#39;), mpf(&#39;1.5&#39;), mpf(&#39;1.75&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.75</span><span class="p">)</span>
<span class="go">[mpf(&#39;1.0&#39;), mpf(&#39;0.25&#39;), mpf(&#39;-0.5&#39;)]</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="linspace">
<h3><code class="xref py py-func docutils literal"><span class="pre">linspace()</span></code><a class="headerlink" href="#linspace" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.linspace">
<code class="descclassname">mpmath.</code><code class="descname">linspace</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.linspace" title="Permalink to this definition"></a></dt>
<dd><p><code class="docutils literal"><span class="pre">linspace(a,</span> <span class="pre">b,</span> <span class="pre">n)</span></code> returns a list of <span class="math">\(n\)</span> evenly spaced
samples from <span class="math">\(a\)</span> to <span class="math">\(b\)</span>. The syntax <code class="docutils literal"><span class="pre">linspace(mpi(a,b),</span> <span class="pre">n)</span></code>
is also valid.</p>
<p>This function is often more convenient than <a class="reference internal" href="#mpmath.arange" title="mpmath.arange"><code class="xref py py-func docutils literal"><span class="pre">arange()</span></code></a>
for partitioning an interval into subintervals, since
the endpoint is included:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">linspace</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go">[mpf(&#39;1.0&#39;), mpf(&#39;2.0&#39;), mpf(&#39;3.0&#39;), mpf(&#39;4.0&#39;)]</span>
</pre></div>
</div>
<p>You may also provide the keyword argument <code class="docutils literal"><span class="pre">endpoint=False</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">linspace</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">[mpf(&#39;1.0&#39;), mpf(&#39;1.75&#39;), mpf(&#39;2.5&#39;), mpf(&#39;3.25&#39;)]</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="precision-management">
<h2>Precision management<a class="headerlink" href="#precision-management" title="Permalink to this headline"></a></h2>
<div class="section" id="autoprec">
<h3><code class="xref py py-func docutils literal"><span class="pre">autoprec()</span></code><a class="headerlink" href="#autoprec" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.autoprec">
<code class="descclassname">mpmath.</code><code class="descname">autoprec</code><span class="sig-paren">(</span><em>f</em>, <em>maxprec=None</em>, <em>catch=()</em>, <em>verbose=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.autoprec" title="Permalink to this definition"></a></dt>
<dd><p>Return a wrapped copy of <em>f</em> that repeatedly evaluates <em>f</em>
with increasing precision until the result converges to the
full precision used at the point of the call.</p>
<p>This heuristically protects against rounding errors, at the cost of
roughly a 2x slowdown compared to manually setting the optimal
precision. This method can, however, easily be fooled if the results
from <em>f</em> depend “discontinuously” on the precision, for instance
if catastrophic cancellation can occur. Therefore, <a class="reference internal" href="#mpmath.autoprec" title="mpmath.autoprec"><code class="xref py py-func docutils literal"><span class="pre">autoprec()</span></code></a>
should be used judiciously.</p>
<p><strong>Examples</strong></p>
<p>Many functions are sensitive to perturbations of the input arguments.
If the arguments are decimal numbers, they may have to be converted
to binary at a much higher precision. If the amount of required
extra precision is unknown, <a class="reference internal" href="#mpmath.autoprec" title="mpmath.autoprec"><code class="xref py py-func docutils literal"><span class="pre">autoprec()</span></code></a> is convenient:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">besselj</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">125</span> <span class="o">*</span> <span class="mi">10</span><span class="o">**</span><span class="mi">28</span><span class="p">)</span>    <span class="c1"># Exact input</span>
<span class="go">-8.03284785591801e-17</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">besselj</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;1.25e30&#39;</span><span class="p">)</span>   <span class="c1"># Bad</span>
<span class="go">7.12954868316652e-16</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">autoprec</span><span class="p">(</span><span class="n">besselj</span><span class="p">)(</span><span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;1.25e30&#39;</span><span class="p">)</span>   <span class="c1"># Good</span>
<span class="go">-8.03284785591801e-17</span>
</pre></div>
</div>
<p>The following fails to converge because <span class="math">\(\sin(\pi) = 0\)</span> whereas all
finite-precision approximations of <span class="math">\(\pi\)</span> give nonzero values:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">autoprec</span><span class="p">(</span><span class="n">sin</span><span class="p">)(</span><span class="n">pi</span><span class="p">)</span> 
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">NoConvergence</span>: <span class="n">autoprec: prec increased to 2910 without convergence</span>
</pre></div>
</div>
<p>As the following example shows, <a class="reference internal" href="#mpmath.autoprec" title="mpmath.autoprec"><code class="xref py py-func docutils literal"><span class="pre">autoprec()</span></code></a> can protect against
cancellation, but is fooled by too severe cancellation:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mf">1e-10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="n">expm1</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="n">autoprec</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">exp</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="go">1.00000008274037e-10</span>
<span class="go">1.00000000005e-10</span>
<span class="go">1.00000000005e-10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mf">1e-50</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="n">expm1</span><span class="p">(</span><span class="n">x</span><span class="p">);</span> <span class="n">autoprec</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">exp</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="go">1.0e-50</span>
<span class="go">0.0</span>
</pre></div>
</div>
<p>With <em>catch</em>, an exception or list of exceptions to intercept
may be specified. The raised exception is interpreted
as signaling insufficient precision. This permits, for example,
evaluating a function where a too low precision results in a
division by zero:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mf">1e-30</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ZeroDivisionError</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">autoprec</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">catch</span><span class="o">=</span><span class="ne">ZeroDivisionError</span><span class="p">)(</span><span class="mf">1e-30</span><span class="p">)</span>
<span class="go">1.0e+30</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="workprec">
<h3><code class="xref py py-func docutils literal"><span class="pre">workprec()</span></code><a class="headerlink" href="#workprec" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.workprec">
<code class="descclassname">mpmath.</code><code class="descname">workprec</code><span class="sig-paren">(</span><em>n</em>, <em>normalize_output=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.workprec" title="Permalink to this definition"></a></dt>
<dd><p>The block</p>
<blockquote>
<div><dl class="docutils">
<dt>with workprec(n):</dt>
<dd>&lt;code&gt;</dd>
</dl>
</div></blockquote>
<p>sets the precision to n bits, executes &lt;code&gt;, and then restores
the precision.</p>
<p>workprec(n)(f) returns a decorated version of the function f
that sets the precision to n bits before execution,
and restores the precision afterwards. With normalize_output=True,
it rounds the return value to the parent precision.</p>
</dd></dl>

</div>
<div class="section" id="workdps">
<h3><code class="xref py py-func docutils literal"><span class="pre">workdps()</span></code><a class="headerlink" href="#workdps" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.workdps">
<code class="descclassname">mpmath.</code><code class="descname">workdps</code><span class="sig-paren">(</span><em>n</em>, <em>normalize_output=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.workdps" title="Permalink to this definition"></a></dt>
<dd><p>This function is analogous to workprec (see documentation)
but changes the decimal precision instead of the number of bits.</p>
</dd></dl>

</div>
<div class="section" id="extraprec">
<h3><code class="xref py py-func docutils literal"><span class="pre">extraprec()</span></code><a class="headerlink" href="#extraprec" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.extraprec">
<code class="descclassname">mpmath.</code><code class="descname">extraprec</code><span class="sig-paren">(</span><em>n</em>, <em>normalize_output=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.extraprec" title="Permalink to this definition"></a></dt>
<dd><p>The block</p>
<blockquote>
<div><dl class="docutils">
<dt>with extraprec(n):</dt>
<dd>&lt;code&gt;</dd>
</dl>
</div></blockquote>
<p>increases the precision n bits, executes &lt;code&gt;, and then
restores the precision.</p>
<p>extraprec(n)(f) returns a decorated version of the function f
that increases the working precision by n bits before execution,
and restores the parent precision afterwards. With
normalize_output=True, it rounds the return value to the parent
precision.</p>
</dd></dl>

</div>
<div class="section" id="extradps">
<h3><code class="xref py py-func docutils literal"><span class="pre">extradps()</span></code><a class="headerlink" href="#extradps" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.extradps">
<code class="descclassname">mpmath.</code><code class="descname">extradps</code><span class="sig-paren">(</span><em>n</em>, <em>normalize_output=False</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.extradps" title="Permalink to this definition"></a></dt>
<dd><p>This function is analogous to extraprec (see documentation)
but changes the decimal precision instead of the number of bits.</p>
</dd></dl>

</div>
</div>
<div class="section" id="performance-and-debugging">
<h2>Performance and debugging<a class="headerlink" href="#performance-and-debugging" title="Permalink to this headline"></a></h2>
<div class="section" id="memoize">
<h3><code class="xref py py-func docutils literal"><span class="pre">memoize()</span></code><a class="headerlink" href="#memoize" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.memoize">
<code class="descclassname">mpmath.</code><code class="descname">memoize</code><span class="sig-paren">(</span><em>f</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.memoize" title="Permalink to this definition"></a></dt>
<dd><p>Return a wrapped copy of <em>f</em> that caches computed values, i.e.
a memoized copy of <em>f</em>. Values are only reused if the cached precision
is equal to or higher than the working precision:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">memoize</span><span class="p">(</span><span class="n">maxcalls</span><span class="p">(</span><span class="n">sin</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0.909297426825682</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0.909297426825682</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">25</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> 
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">NoConvergence</span>: <span class="n">maxcalls: function evaluated 1 times</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="maxcalls">
<h3><code class="xref py py-func docutils literal"><span class="pre">maxcalls()</span></code><a class="headerlink" href="#maxcalls" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.maxcalls">
<code class="descclassname">mpmath.</code><code class="descname">maxcalls</code><span class="sig-paren">(</span><em>f</em>, <em>N</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.maxcalls" title="Permalink to this definition"></a></dt>
<dd><p>Return a wrapped copy of <em>f</em> that raises <code class="docutils literal"><span class="pre">NoConvergence</span></code> when <em>f</em>
has been called more than <em>N</em> times:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">maxcalls</span><span class="p">(</span><span class="n">sin</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)))</span>
<span class="go">1.95520948210738</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> 
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">NoConvergence</span>: <span class="n">maxcalls: function evaluated 10 times</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="monitor">
<h3><code class="xref py py-func docutils literal"><span class="pre">monitor()</span></code><a class="headerlink" href="#monitor" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.monitor">
<code class="descclassname">mpmath.</code><code class="descname">monitor</code><span class="sig-paren">(</span><em>f</em>, <em>input='print'</em>, <em>output='print'</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.monitor" title="Permalink to this definition"></a></dt>
<dd><p>Returns a wrapped copy of <em>f</em> that monitors evaluation by calling
<em>input</em> with every input (<em>args</em>, <em>kwargs</em>) passed to <em>f</em> and
<em>output</em> with every value returned from <em>f</em>. The default action
(specify using the special string value <code class="docutils literal"><span class="pre">'print'</span></code>) is to print
inputs and outputs to stdout, along with the total evaluation
count:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mpmath</span> <span class="k">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span> <span class="n">mp</span><span class="o">.</span><span class="n">pretty</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">monitor</span><span class="p">(</span><span class="n">exp</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>   <span class="c1"># diff will eval f(x-h) and f(x+h)</span>
<span class="go">in  0 (mpf(&#39;0.99999999906867742538452148&#39;),) {}</span>
<span class="go">out 0 mpf(&#39;2.7182818259274480055282064&#39;)</span>
<span class="go">in  1 (mpf(&#39;1.0000000009313225746154785&#39;),) {}</span>
<span class="go">out 1 mpf(&#39;2.7182818309906424675501024&#39;)</span>
<span class="go">mpf(&#39;2.7182808&#39;)</span>
</pre></div>
</div>
<p>To disable either the input or the output handler, you may
pass <em>None</em> as argument.</p>
<p>Custom input and output handlers may be used e.g. to store
results for later analysis:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">mp</span><span class="o">.</span><span class="n">dps</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">input</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">findroot</span><span class="p">(</span><span class="n">monitor</span><span class="p">(</span><span class="n">sin</span><span class="p">,</span> <span class="nb">input</span><span class="o">.</span><span class="n">append</span><span class="p">,</span> <span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">),</span> <span class="mf">3.0</span><span class="p">)</span>
<span class="go">mpf(&#39;3.1415926535897932&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>  <span class="c1"># Count number of evaluations</span>
<span class="go">9</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">input</span><span class="p">[</span><span class="mi">3</span><span class="p">]);</span> <span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
<span class="go">((mpf(&#39;3.1415076583334066&#39;),), {})</span>
<span class="go">8.49952562843408e-5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">input</span><span class="p">[</span><span class="mi">4</span><span class="p">]);</span> <span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">[</span><span class="mi">4</span><span class="p">])</span>
<span class="go">((mpf(&#39;3.1415928201669122&#39;),), {})</span>
<span class="go">-1.66577118985331e-7</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="timing">
<h3><code class="xref py py-func docutils literal"><span class="pre">timing()</span></code><a class="headerlink" href="#timing" title="Permalink to this headline"></a></h3>
<dl class="function">
<dt id="mpmath.timing">
<code class="descclassname">mpmath.</code><code class="descname">timing</code><span class="sig-paren">(</span><em>f</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#mpmath.timing" title="Permalink to this definition"></a></dt>
<dd><p>Returns time elapsed for evaluating <code class="docutils literal"><span class="pre">f()</span></code>. Optionally arguments
may be passed to time the execution of <code class="docutils literal"><span class="pre">f(*args,</span> <span class="pre">**kwargs)</span></code>.</p>
<p>If the first call is very quick, <code class="docutils literal"><span class="pre">f</span></code> is called
repeatedly and the best time is returned.</p>
</dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Utility functions</a><ul>
<li><a class="reference internal" href="#conversion-and-printing">Conversion and printing</a><ul>
<li><a class="reference internal" href="#mpmathify-convert"><code class="docutils literal"><span class="pre">mpmathify()</span></code> / <code class="docutils literal"><span class="pre">convert()</span></code></a></li>
<li><a class="reference internal" href="#nstr"><code class="docutils literal"><span class="pre">nstr()</span></code></a></li>
<li><a class="reference internal" href="#nprint"><code class="docutils literal"><span class="pre">nprint()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#arithmetic-operations">Arithmetic operations</a><ul>
<li><a class="reference internal" href="#fadd"><code class="docutils literal"><span class="pre">fadd()</span></code></a></li>
<li><a class="reference internal" href="#fsub"><code class="docutils literal"><span class="pre">fsub()</span></code></a></li>
<li><a class="reference internal" href="#fneg"><code class="docutils literal"><span class="pre">fneg()</span></code></a></li>
<li><a class="reference internal" href="#fmul"><code class="docutils literal"><span class="pre">fmul()</span></code></a></li>
<li><a class="reference internal" href="#fdiv"><code class="docutils literal"><span class="pre">fdiv()</span></code></a></li>
<li><a class="reference internal" href="#fmod"><code class="docutils literal"><span class="pre">fmod()</span></code></a></li>
<li><a class="reference internal" href="#fsum"><code class="docutils literal"><span class="pre">fsum()</span></code></a></li>
<li><a class="reference internal" href="#fprod"><code class="docutils literal"><span class="pre">fprod()</span></code></a></li>
<li><a class="reference internal" href="#fdot"><code class="docutils literal"><span class="pre">fdot()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#complex-components">Complex components</a><ul>
<li><a class="reference internal" href="#fabs"><code class="docutils literal"><span class="pre">fabs()</span></code></a></li>
<li><a class="reference internal" href="#sign"><code class="docutils literal"><span class="pre">sign()</span></code></a></li>
<li><a class="reference internal" href="#re"><code class="docutils literal"><span class="pre">re()</span></code></a></li>
<li><a class="reference internal" href="#im"><code class="docutils literal"><span class="pre">im()</span></code></a></li>
<li><a class="reference internal" href="#arg"><code class="docutils literal"><span class="pre">arg()</span></code></a></li>
<li><a class="reference internal" href="#conj"><code class="docutils literal"><span class="pre">conj()</span></code></a></li>
<li><a class="reference internal" href="#polar"><code class="docutils literal"><span class="pre">polar()</span></code></a></li>
<li><a class="reference internal" href="#rect"><code class="docutils literal"><span class="pre">rect()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#integer-and-fractional-parts">Integer and fractional parts</a><ul>
<li><a class="reference internal" href="#floor"><code class="docutils literal"><span class="pre">floor()</span></code></a></li>
<li><a class="reference internal" href="#ceil"><code class="docutils literal"><span class="pre">ceil()</span></code></a></li>
<li><a class="reference internal" href="#nint"><code class="docutils literal"><span class="pre">nint()</span></code></a></li>
<li><a class="reference internal" href="#frac"><code class="docutils literal"><span class="pre">frac()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#tolerances-and-approximate-comparisons">Tolerances and approximate comparisons</a><ul>
<li><a class="reference internal" href="#chop"><code class="docutils literal"><span class="pre">chop()</span></code></a></li>
<li><a class="reference internal" href="#almosteq"><code class="docutils literal"><span class="pre">almosteq()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#properties-of-numbers">Properties of numbers</a><ul>
<li><a class="reference internal" href="#isinf"><code class="docutils literal"><span class="pre">isinf()</span></code></a></li>
<li><a class="reference internal" href="#isnan"><code class="docutils literal"><span class="pre">isnan()</span></code></a></li>
<li><a class="reference internal" href="#isnormal"><code class="docutils literal"><span class="pre">isnormal()</span></code></a></li>
<li><a class="reference internal" href="#isfinite"><code class="docutils literal"><span class="pre">isfinite()</span></code></a></li>
<li><a class="reference internal" href="#isint"><code class="docutils literal"><span class="pre">isint()</span></code></a></li>
<li><a class="reference internal" href="#ldexp"><code class="docutils literal"><span class="pre">ldexp()</span></code></a></li>
<li><a class="reference internal" href="#frexp"><code class="docutils literal"><span class="pre">frexp()</span></code></a></li>
<li><a class="reference internal" href="#mag"><code class="docutils literal"><span class="pre">mag()</span></code></a></li>
<li><a class="reference internal" href="#nint-distance"><code class="docutils literal"><span class="pre">nint_distance()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#number-generation">Number generation</a><ul>
<li><a class="reference internal" href="#fraction"><code class="docutils literal"><span class="pre">fraction()</span></code></a></li>
<li><a class="reference internal" href="#rand"><code class="docutils literal"><span class="pre">rand()</span></code></a></li>
<li><a class="reference internal" href="#arange"><code class="docutils literal"><span class="pre">arange()</span></code></a></li>
<li><a class="reference internal" href="#linspace"><code class="docutils literal"><span class="pre">linspace()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#precision-management">Precision management</a><ul>
<li><a class="reference internal" href="#autoprec"><code class="docutils literal"><span class="pre">autoprec()</span></code></a></li>
<li><a class="reference internal" href="#workprec"><code class="docutils literal"><span class="pre">workprec()</span></code></a></li>
<li><a class="reference internal" href="#workdps"><code class="docutils literal"><span class="pre">workdps()</span></code></a></li>
<li><a class="reference internal" href="#extraprec"><code class="docutils literal"><span class="pre">extraprec()</span></code></a></li>
<li><a class="reference internal" href="#extradps"><code class="docutils literal"><span class="pre">extradps()</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#performance-and-debugging">Performance and debugging</a><ul>
<li><a class="reference internal" href="#memoize"><code class="docutils literal"><span class="pre">memoize()</span></code></a></li>
<li><a class="reference internal" href="#maxcalls"><code class="docutils literal"><span class="pre">maxcalls()</span></code></a></li>
<li><a class="reference internal" href="#monitor"><code class="docutils literal"><span class="pre">monitor()</span></code></a></li>
<li><a class="reference internal" href="#timing"><code class="docutils literal"><span class="pre">timing()</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="contexts.html"
                        title="previous chapter">Contexts</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="plotting.html"
                        title="next chapter">Plotting</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/general.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="plotting.html" title="Plotting"
             >next</a> |</li>
        <li class="right" >
          <a href="contexts.html" title="Contexts"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">mpmath 1.0.0 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007-2017, Fredrik Johansson and mpmath developers.
      Last updated on Nov 26, 2017.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.5.
    </div>
  </body>
</html>