This file is indexed.

/usr/share/doc/glibc-doc/html/libc_20.html is in glibc-doc 2.15-0ubuntu10.

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
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU C library.

This is Edition 0.13, last updated 2011-07-19,
of The GNU C Library Reference Manual, for version
2.14 (Ubuntu EGLIBC 2.15-0ubuntu10) .

Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software Needs Free Documentation"
and "GNU Lesser General Public License", the Front-Cover texts being
"A GNU Manual", and with the Back-Cover Texts as in (a) below.  A
copy of the license is included in the section entitled "GNU Free
Documentation License".

(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual.  Buying copies from the FSF
supports it in developing GNU and promoting software freedom."
 -->
<!-- Created on April 20, 2012 by texi2html 1.82
texi2html was written by: 
            Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>The GNU C Library: 20. Arithmetic Functions</title>

<meta name="description" content="The GNU C Library: 20. Arithmetic Functions">
<meta name="keywords" content="The GNU C Library: 20. Arithmetic Functions">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<a name="Arithmetic"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_19.html#FP-Function-Optimizations" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Integers" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc_19.html#Mathematics" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Arithmetic-Functions-1"></a>
<h1 class="chapter">20. Arithmetic Functions</h1>

<p>This chapter contains information about functions for doing basic
arithmetic operations, such as splitting a float into its integer and
fractional parts or retrieving the imaginary part of a complex value.
These functions are declared in the header files &lsquo;<tt>math.h</tt>&rsquo; and
&lsquo;<tt>complex.h</tt>&rsquo;.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Integers">20.1 Integers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                    Basic integer types and concepts
</td></tr>
<tr><td align="left" valign="top"><a href="#Integer-Division">20.2 Integer Division</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            Integer division with guaranteed rounding.
</td></tr>
<tr><td align="left" valign="top"><a href="#Floating-Point-Numbers">20.3 Floating Point Numbers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">      Basic concepts.  IEEE 754.
</td></tr>
<tr><td align="left" valign="top"><a href="#Floating-Point-Classes">20.4 Floating-Point Number Classification Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">      The five kinds of floating-point number.
</td></tr>
<tr><td align="left" valign="top"><a href="#Floating-Point-Errors">20.5 Errors in Floating-Point Calculations</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       When something goes wrong in a calculation.
</td></tr>
<tr><td align="left" valign="top"><a href="#Rounding">20.6 Rounding Modes</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">                    Controlling how results are rounded.
</td></tr>
<tr><td align="left" valign="top"><a href="#Control-Functions">20.7 Floating-Point Control Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           Saving and restoring the FPU&rsquo;s state.
</td></tr>
<tr><td align="left" valign="top"><a href="#Arithmetic-Functions">20.8 Arithmetic Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        Fundamental operations provided by the library.
</td></tr>
<tr><td align="left" valign="top"><a href="#Complex-Numbers">20.9 Complex Numbers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">             The types.  Writing complex constants.
</td></tr>
<tr><td align="left" valign="top"><a href="#Operations-on-Complex">20.10 Projections, Conjugates, and Decomposing of Complex Numbers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       Projection, conjugation, decomposition.
</td></tr>
<tr><td align="left" valign="top"><a href="#Parsing-of-Numbers">20.11 Parsing of Numbers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">          Converting strings to numbers.
</td></tr>
<tr><td align="left" valign="top"><a href="#System-V-Number-Conversion">20.12 Old-fashioned System V number-to-string functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">  An archaic way to convert numbers to strings.
</td></tr>
</table>

<hr size="6">
<a name="Integers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Arithmetic" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Integer-Division" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Integers-1"></a>
<h2 class="section">20.1 Integers</h2>
<a name="index-integer"></a>

<p>The C language defines several integer data types: integer, short integer,
long integer, and character, all in both signed and unsigned varieties.
The GNU C compiler extends the language to contain long long integers
as well.
<a name="index-signedness"></a>
</p>
<p>The C integer types were intended to allow code to be portable among
machines with different inherent data sizes (word sizes), so each type
may have different ranges on different machines.  The problem with
this is that a program often needs to be written for a particular range
of integers, and sometimes must be written for a particular size of
storage, regardless of what machine the program runs on.
</p>
<p>To address this problem, the GNU C library contains C type definitions
you can use to declare integers that meet your exact needs.  Because the
GNU C library header files are customized to a specific machine, your
program source code doesn&rsquo;t have to be.
</p>
<p>These <code>typedef</code>s are in &lsquo;<tt>stdint.h</tt>&rsquo;.
<a name="index-stdint_002eh"></a>
</p>
<p>If you require that an integer be represented in exactly N bits, use one
of the following types, with the obvious mapping to bit size and signedness:
</p>
<ul>
<li> int8_t
</li><li> int16_t
</li><li> int32_t
</li><li> int64_t
</li><li> uint8_t
</li><li> uint16_t
</li><li> uint32_t
</li><li> uint64_t
</li></ul>

<p>If your C compiler and target machine do not allow integers of a certain
size, the corresponding above type does not exist.
</p>
<p>If you don&rsquo;t need a specific storage size, but want the smallest data
structure with <em>at least</em> N bits, use one of these:
</p>
<ul>
<li> int_least8_t
</li><li> int_least16_t
</li><li> int_least32_t
</li><li> int_least64_t
</li><li> uint_least8_t
</li><li> uint_least16_t
</li><li> uint_least32_t
</li><li> uint_least64_t
</li></ul>

<p>If you don&rsquo;t need a specific storage size, but want the data structure
that allows the fastest access while having at least N bits (and
among data structures with the same access speed, the smallest one), use
one of these:
</p>
<ul>
<li> int_fast8_t
</li><li> int_fast16_t
</li><li> int_fast32_t
</li><li> int_fast64_t
</li><li> uint_fast8_t
</li><li> uint_fast16_t
</li><li> uint_fast32_t
</li><li> uint_fast64_t
</li></ul>

<p>If you want an integer with the widest range possible on the platform on
which it is being used, use one of the following.  If you use these,
you should write code that takes into account the variable size and range
of the integer.
</p>
<ul>
<li> intmax_t
</li><li> uintmax_t
</li></ul>

<p>The GNU C library also provides macros that tell you the maximum and
minimum possible values for each integer data type.  The macro names
follow these examples: <code>INT32_MAX</code>, <code>UINT8_MAX</code>,
<code>INT_FAST32_MIN</code>, <code>INT_LEAST64_MIN</code>, <code>UINTMAX_MAX</code>,
<code>INTMAX_MAX</code>, <code>INTMAX_MIN</code>.  Note that there are no macros for
unsigned integer minima.  These are always zero.
<a name="index-maximum-possible-integer"></a>
<a name="index-minimum-possible-integer"></a>
</p>
<p>There are similar macros for use with C&rsquo;s built in integer types which
should come with your C compiler.  These are described in <a href="libc_34.html#Data-Type-Measurements">Data Type Measurements</a>.
</p>
<p>Don&rsquo;t forget you can use the C <code>sizeof</code> function with any of these
data types to get the number of bytes of storage each uses.
</p>

<hr size="6">
<a name="Integer-Division"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Integers" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Numbers" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Integer-Division-1"></a>
<h2 class="section">20.2 Integer Division</h2>
<a name="index-integer-division-functions"></a>

<p>This section describes functions for performing integer division.  These
functions are redundant when GNU CC is used, because in GNU C the
&lsquo;<samp>/</samp>&rsquo; operator always rounds towards zero.  But in other C
implementations, &lsquo;<samp>/</samp>&rsquo; may round differently with negative arguments.
<code>div</code> and <code>ldiv</code> are useful because they specify how to round
the quotient: towards zero.  The remainder has the same sign as the
numerator.
</p>
<p>These functions are specified to return a result <var>r</var> such that the value
<code><var>r</var>.quot*<var>denominator</var> + <var>r</var>.rem</code> equals
<var>numerator</var>.
</p>
<a name="index-stdlib_002eh-15"></a>
<p>To use these facilities, you should include the header file
&lsquo;<tt>stdlib.h</tt>&rsquo; in your program.
</p>
<dl>
<dt><a name="index-div_005ft"></a><u>Data Type:</u> <b>div_t</b></dt>
<dd><p>This is a structure type used to hold the result returned by the <code>div</code>
function.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>int quot</code></dt>
<dd><p>The quotient from the division.
</p>
</dd>
<dt> <code>int rem</code></dt>
<dd><p>The remainder from the division.
</p></dd>
</dl>
</dd></dl>

<dl>
<dt><a name="index-div"></a><u>Function:</u> div_t <b>div</b><i> (int <var>numerator</var>, int <var>denominator</var>)</i></dt>
<dd><p>This function <code>div</code> computes the quotient and remainder from
the division of <var>numerator</var> by <var>denominator</var>, returning the
result in a structure of type <code>div_t</code>.
</p>
<p>If the result cannot be represented (as in a division by zero), the
behavior is undefined.
</p>
<p>Here is an example, albeit not a very useful one.
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">div_t result;
result = div (20, -6);
</pre></td></tr></table>

<p>Now <code>result.quot</code> is <code>-3</code> and <code>result.rem</code> is <code>2</code>.
</p></dd></dl>

<dl>
<dt><a name="index-ldiv_005ft"></a><u>Data Type:</u> <b>ldiv_t</b></dt>
<dd><p>This is a structure type used to hold the result returned by the <code>ldiv</code>
function.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>long int quot</code></dt>
<dd><p>The quotient from the division.
</p>
</dd>
<dt> <code>long int rem</code></dt>
<dd><p>The remainder from the division.
</p></dd>
</dl>

<p>(This is identical to <code>div_t</code> except that the components are of
type <code>long int</code> rather than <code>int</code>.)
</p></dd></dl>

<dl>
<dt><a name="index-ldiv"></a><u>Function:</u> ldiv_t <b>ldiv</b><i> (long int <var>numerator</var>, long int <var>denominator</var>)</i></dt>
<dd><p>The <code>ldiv</code> function is similar to <code>div</code>, except that the
arguments are of type <code>long int</code> and the result is returned as a
structure of type <code>ldiv_t</code>.
</p></dd></dl>

<dl>
<dt><a name="index-lldiv_005ft"></a><u>Data Type:</u> <b>lldiv_t</b></dt>
<dd><p>This is a structure type used to hold the result returned by the <code>lldiv</code>
function.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>long long int quot</code></dt>
<dd><p>The quotient from the division.
</p>
</dd>
<dt> <code>long long int rem</code></dt>
<dd><p>The remainder from the division.
</p></dd>
</dl>

<p>(This is identical to <code>div_t</code> except that the components are of
type <code>long long int</code> rather than <code>int</code>.)
</p></dd></dl>

<dl>
<dt><a name="index-lldiv"></a><u>Function:</u> lldiv_t <b>lldiv</b><i> (long long int <var>numerator</var>, long long int <var>denominator</var>)</i></dt>
<dd><p>The <code>lldiv</code> function is like the <code>div</code> function, but the
arguments are of type <code>long long int</code> and the result is returned as
a structure of type <code>lldiv_t</code>.
</p>
<p>The <code>lldiv</code> function was added in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-imaxdiv_005ft"></a><u>Data Type:</u> <b>imaxdiv_t</b></dt>
<dd><p>This is a structure type used to hold the result returned by the <code>imaxdiv</code>
function.  It has the following members:
</p>
<dl compact="compact">
<dt> <code>intmax_t quot</code></dt>
<dd><p>The quotient from the division.
</p>
</dd>
<dt> <code>intmax_t rem</code></dt>
<dd><p>The remainder from the division.
</p></dd>
</dl>

<p>(This is identical to <code>div_t</code> except that the components are of
type <code>intmax_t</code> rather than <code>int</code>.)
</p>
<p>See <a href="#Integers">Integers</a> for a description of the <code>intmax_t</code> type.
</p>
</dd></dl>

<dl>
<dt><a name="index-imaxdiv"></a><u>Function:</u> imaxdiv_t <b>imaxdiv</b><i> (intmax_t <var>numerator</var>, intmax_t <var>denominator</var>)</i></dt>
<dd><p>The <code>imaxdiv</code> function is like the <code>div</code> function, but the
arguments are of type <code>intmax_t</code> and the result is returned as
a structure of type <code>imaxdiv_t</code>.
</p>
<p>See <a href="#Integers">Integers</a> for a description of the <code>intmax_t</code> type.
</p>
<p>The <code>imaxdiv</code> function was added in ISO C99.
</p></dd></dl>


<hr size="6">
<a name="Floating-Point-Numbers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Integer-Division" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Classes" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Floating-Point-Numbers-1"></a>
<h2 class="section">20.3 Floating Point Numbers</h2>
<a name="index-floating-point"></a>
<a name="index-IEEE-754"></a>
<a name="index-IEEE-floating-point"></a>

<p>Most computer hardware has support for two different kinds of numbers:
integers (<em>&hellip;-3, -2, -1, 0, 1, 2, 3&hellip;</em>) and
floating-point numbers.  Floating-point numbers have three parts: the
<em>mantissa</em>, the <em>exponent</em>, and the <em>sign bit</em>.  The real
number represented by a floating-point value is given by
<em>(s ? -1 : 1) &amp;middot; 2^e &amp;middot; M</em>
where <em>s</em> is the sign bit, <em>e</em> the exponent, and <em>M</em>
the mantissa.  See section <a href="libc_34.html#Floating-Point-Concepts">Floating Point Representation Concepts</a>, for details.  (It is
possible to have a different <em>base</em> for the exponent, but all modern
hardware uses <em>2</em>.)
</p>
<p>Floating-point numbers can represent a finite subset of the real
numbers.  While this subset is large enough for most purposes, it is
important to remember that the only reals that can be represented
exactly are rational numbers that have a terminating binary expansion
shorter than the width of the mantissa.  Even simple fractions such as
<em>1/5</em> can only be approximated by floating point.
</p>
<p>Mathematical operations and functions frequently need to produce values
that are not representable.  Often these values can be approximated
closely enough for practical purposes, but sometimes they can&rsquo;t.
Historically there was no way to tell when the results of a calculation
were inaccurate.  Modern computers implement the IEEE 754 standard
for numerical computations, which defines a framework for indicating to
the program when the results of calculation are not trustworthy.  This
framework consists of a set of <em>exceptions</em> that indicate why a
result could not be represented, and the special values <em>infinity</em>
and <em>not a number</em> (NaN).
</p>
<hr size="6">
<a name="Floating-Point-Classes"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Floating-Point-Numbers" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Errors" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Floating_002dPoint-Number-Classification-Functions"></a>
<h2 class="section">20.4 Floating-Point Number Classification Functions</h2>
<a name="index-floating_002dpoint-classes"></a>
<a name="index-classes_002c-floating_002dpoint"></a>
<a name="index-math_002eh-1"></a>

<p>ISO C99 defines macros that let you determine what sort of
floating-point number a variable holds.
</p>
<dl>
<dt><a name="index-fpclassify"></a><u>Macro:</u> int <b>fpclassify</b><i> (<em>float-type</em> <var>x</var>)</i></dt>
<dd><p>This is a generic macro which works on all floating-point types and
which returns a value of type <code>int</code>.  The possible values are:
</p>
<dl compact="compact">
<dt> <code>FP_NAN</code>
<a name="index-FP_005fNAN"></a>
</dt>
<dd><p>The floating-point number <var>x</var> is &ldquo;Not a Number&rdquo; (see section <a href="#Infinity-and-NaN">Infinity and NaN</a>)
</p></dd>
<dt> <code>FP_INFINITE</code>
<a name="index-FP_005fINFINITE"></a>
</dt>
<dd><p>The value of <var>x</var> is either plus or minus infinity (see section <a href="#Infinity-and-NaN">Infinity and NaN</a>)
</p></dd>
<dt> <code>FP_ZERO</code>
<a name="index-FP_005fZERO"></a>
</dt>
<dd><p>The value of <var>x</var> is zero.  In floating-point formats like IEEE
754, where zero can be signed, this value is also returned if
<var>x</var> is negative zero.
</p></dd>
<dt> <code>FP_SUBNORMAL</code>
<a name="index-FP_005fSUBNORMAL"></a>
</dt>
<dd><p>Numbers whose absolute value is too small to be represented in the
normal format are represented in an alternate, <em>denormalized</em> format
(see section <a href="libc_34.html#Floating-Point-Concepts">Floating Point Representation Concepts</a>).  This format is less precise but can
represent values closer to zero.  <code>fpclassify</code> returns this value
for values of <var>x</var> in this alternate format.
</p></dd>
<dt> <code>FP_NORMAL</code>
<a name="index-FP_005fNORMAL"></a>
</dt>
<dd><p>This value is returned for all other values of <var>x</var>.  It indicates
that there is nothing special about the number.
</p></dd>
</dl>

</dd></dl>

<p><code>fpclassify</code> is most useful if more than one property of a number
must be tested.  There are more specific macros which only test one
property at a time.  Generally these macros execute faster than
<code>fpclassify</code>, since there is special hardware support for them.
You should therefore use the specific macros whenever possible.
</p>
<dl>
<dt><a name="index-isfinite"></a><u>Macro:</u> int <b>isfinite</b><i> (<em>float-type</em> <var>x</var>)</i></dt>
<dd><p>This macro returns a nonzero value if <var>x</var> is finite: not plus or
minus infinity, and not NaN.  It is equivalent to
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">(fpclassify (x) != FP_NAN &amp;&amp; fpclassify (x) != FP_INFINITE)
</pre></td></tr></table>

<p><code>isfinite</code> is implemented as a macro which accepts any
floating-point type.
</p></dd></dl>

<dl>
<dt><a name="index-isnormal"></a><u>Macro:</u> int <b>isnormal</b><i> (<em>float-type</em> <var>x</var>)</i></dt>
<dd><p>This macro returns a nonzero value if <var>x</var> is finite and normalized.
It is equivalent to
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">(fpclassify (x) == FP_NORMAL)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-isnan"></a><u>Macro:</u> int <b>isnan</b><i> (<em>float-type</em> <var>x</var>)</i></dt>
<dd><p>This macro returns a nonzero value if <var>x</var> is NaN.  It is equivalent
to
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">(fpclassify (x) == FP_NAN)
</pre></td></tr></table>
</dd></dl>

<p>Another set of floating-point classification functions was provided by
BSD.  The GNU C library also supports these functions; however, we
recommend that you use the ISO C99 macros in new code.  Those are standard
and will be available more widely.  Also, since they are macros, you do
not have to worry about the type of their argument.
</p>
<dl>
<dt><a name="index-isinf"></a><u>Function:</u> int <b>isinf</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-isinff"></a><u>Function:</u> int <b>isinff</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-isinfl"></a><u>Function:</u> int <b>isinfl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>This function returns <code>-1</code> if <var>x</var> represents negative infinity,
<code>1</code> if <var>x</var> represents positive infinity, and <code>0</code> otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-isnan-1"></a><u>Function:</u> int <b>isnan</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-isnanf"></a><u>Function:</u> int <b>isnanf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-isnanl"></a><u>Function:</u> int <b>isnanl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>This function returns a nonzero value if <var>x</var> is a &ldquo;not a number&rdquo;
value, and zero otherwise.
</p>
<p><strong>NB:</strong> The <code>isnan</code> macro defined by ISO C99 overrides
the BSD function.  This is normally not a problem, because the two
routines behave identically.  However, if you really need to get the BSD
function for some reason, you can write
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">(isnan) (x)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-finite"></a><u>Function:</u> int <b>finite</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-finitef"></a><u>Function:</u> int <b>finitef</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-finitel"></a><u>Function:</u> int <b>finitel</b><i> (long double <var>x</var>)</i></dt>
<dd><p>This function returns a nonzero value if <var>x</var> is finite or a &ldquo;not a
number&rdquo; value, and zero otherwise.
</p></dd></dl>

<p><strong>Portability Note:</strong> The functions listed in this section are BSD
extensions.
</p>

<hr size="6">
<a name="Floating-Point-Errors"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Floating-Point-Classes" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#FP-Exceptions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Errors-in-Floating_002dPoint-Calculations"></a>
<h2 class="section">20.5 Errors in Floating-Point Calculations</h2>

<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#FP-Exceptions">20.5.1 FP Exceptions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">               IEEE 754 math exceptions and how to detect them.
</td></tr>
<tr><td align="left" valign="top"><a href="#Infinity-and-NaN">20.5.2 Infinity and NaN</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            Special values returned by calculations.
</td></tr>
<tr><td align="left" valign="top"><a href="#Status-bit-operations">20.5.3 Examining the FPU status word</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">       Checking for exceptions after the fact.
</td></tr>
<tr><td align="left" valign="top"><a href="#Math-Error-Reporting">20.5.4 Error Reporting by Mathematical Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">        How the math functions report errors.
</td></tr>
</table>

<hr size="6">
<a name="FP-Exceptions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Floating-Point-Errors" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Infinity-and-NaN" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Errors" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="FP-Exceptions-1"></a>
<h3 class="subsection">20.5.1 FP Exceptions</h3>
<a name="index-exception"></a>
<a name="index-signal"></a>
<a name="index-zero-divide"></a>
<a name="index-division-by-zero"></a>
<a name="index-inexact-exception"></a>
<a name="index-invalid-exception"></a>
<a name="index-overflow-exception"></a>
<a name="index-underflow-exception"></a>

<p>The IEEE 754 standard defines five <em>exceptions</em> that can occur
during a calculation.  Each corresponds to a particular sort of error,
such as overflow.
</p>
<p>When exceptions occur (when exceptions are <em>raised</em>, in the language
of the standard), one of two things can happen.  By default the
exception is simply noted in the floating-point <em>status word</em>, and
the program continues as if nothing had happened.  The operation
produces a default value, which depends on the exception (see the table
below).  Your program can check the status word to find out which
exceptions happened.
</p>
<p>Alternatively, you can enable <em>traps</em> for exceptions.  In that case,
when an exception is raised, your program will receive the <code>SIGFPE</code>
signal.  The default action for this signal is to terminate the
program.  See section <a href="libc_24.html#Signal-Handling">Signal Handling</a>, for how you can change the effect of
the signal.
</p>
<a name="index-matherr"></a>
<p>In the System V math library, the user-defined function <code>matherr</code>
is called when certain exceptions occur inside math library functions.
However, the Unix98 standard deprecates this interface.  We support it
for historical compatibility, but recommend that you do not use it in
new programs.
</p>
<p>The exceptions defined in IEEE 754 are:
</p>
<dl compact="compact">
<dt> &lsquo;<samp>Invalid Operation</samp>&rsquo;</dt>
<dd><p>This exception is raised if the given operands are invalid for the
operation to be performed.  Examples are
(see IEEE 754, section 7):
</p><ol>
<li>
Addition or subtraction: <em>&amp;infin; - &amp;infin;</em>.  (But
<em>&amp;infin; + &amp;infin; = &amp;infin;</em>).
</li><li>
Multiplication: <em>0 &amp;middot; &amp;infin;</em>.
</li><li>
Division: <em>0/0</em> or <em>&amp;infin;/&amp;infin;</em>.
</li><li>
Remainder: <em>x</em> REM <em>y</em>, where <em>y</em> is zero or <em>x</em> is
infinite.
</li><li>
Square root if the operand is less then zero.  More generally, any
mathematical function evaluated outside its domain produces this
exception.
</li><li>
Conversion of a floating-point number to an integer or decimal
string, when the number cannot be represented in the target format (due
to overflow, infinity, or NaN).
</li><li>
Conversion of an unrecognizable input string.
</li><li>
Comparison via predicates involving <em>&lt;</em> or <em>&gt;</em>, when one or
other of the operands is NaN.  You can prevent this exception by using
the unordered comparison functions instead; see <a href="#FP-Comparison-Functions">Floating-Point Comparison Functions</a>.
</li></ol>

<p>If the exception does not trap, the result of the operation is NaN.
</p>
</dd>
<dt> &lsquo;<samp>Division by Zero</samp>&rsquo;</dt>
<dd><p>This exception is raised when a finite nonzero number is divided
by zero.  If no trap occurs the result is either <em>+&amp;infin;</em> or
<em>-&amp;infin;</em>, depending on the signs of the operands.
</p>
</dd>
<dt> &lsquo;<samp>Overflow</samp>&rsquo;</dt>
<dd><p>This exception is raised whenever the result cannot be represented
as a finite value in the precision format of the destination.  If no trap
occurs the result depends on the sign of the intermediate result and the
current rounding mode (IEEE 754, section 7.3):
</p><ol>
<li>
Round to nearest carries all overflows to <em>&amp;infin;</em>
with the sign of the intermediate result.
</li><li>
Round toward <em>0</em> carries all overflows to the largest representable
finite number with the sign of the intermediate result.
</li><li>
Round toward <em>-&amp;infin;</em> carries positive overflows to the
largest representable finite number and negative overflows to
<em>-&amp;infin;</em>.

</li><li>
Round toward <em>&amp;infin;</em> carries negative overflows to the
most negative representable finite number and positive overflows
to <em>&amp;infin;</em>.
</li></ol>

<p>Whenever the overflow exception is raised, the inexact exception is also
raised.
</p>
</dd>
<dt> &lsquo;<samp>Underflow</samp>&rsquo;</dt>
<dd><p>The underflow exception is raised when an intermediate result is too
small to be calculated accurately, or if the operation&rsquo;s result rounded
to the destination precision is too small to be normalized.
</p>
<p>When no trap is installed for the underflow exception, underflow is
signaled (via the underflow flag) only when both tininess and loss of
accuracy have been detected.  If no trap handler is installed the
operation continues with an imprecise small value, or zero if the
destination precision cannot hold the small exact result.
</p>
</dd>
<dt> &lsquo;<samp>Inexact</samp>&rsquo;</dt>
<dd><p>This exception is signalled if a rounded result is not exact (such as
when calculating the square root of two) or a result overflows without
an overflow trap.
</p></dd>
</dl>

<hr size="6">
<a name="Infinity-and-NaN"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#FP-Exceptions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Status-bit-operations" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Errors" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Infinity-and-NaN-1"></a>
<h3 class="subsection">20.5.2 Infinity and NaN</h3>
<a name="index-infinity"></a>
<a name="index-not-a-number"></a>
<a name="index-NaN"></a>

<p>IEEE 754 floating point numbers can represent positive or negative
infinity, and <em>NaN</em> (not a number).  These three values arise from
calculations whose result is undefined or cannot be represented
accurately.  You can also deliberately set a floating-point variable to
any of them, which is sometimes useful.  Some examples of calculations
that produce infinity or NaN:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample"><em>1/0 = &amp;infin;</em>
<em>log (0) = -&amp;infin;</em>
<em>sqrt (-1) = NaN</em>
</pre></td></tr></table>

<p>When a calculation produces any of these values, an exception also
occurs; see <a href="#FP-Exceptions">FP Exceptions</a>.
</p>
<p>The basic operations and math functions all accept infinity and NaN and
produce sensible output.  Infinities propagate through calculations as
one would expect: for example, <em>2 + &amp;infin; = &amp;infin;</em>,
<em>4/&amp;infin; = 0</em>, atan <em>(&amp;infin;) = &amp;pi;/2</em>.  NaN, on
the other hand, infects any calculation that involves it.  Unless the
calculation would produce the same result no matter what real value
replaced NaN, the result is NaN.
</p>
<p>In comparison operations, positive infinity is larger than all values
except itself and NaN, and negative infinity is smaller than all values
except itself and NaN.  NaN is <em>unordered</em>: it is not equal to,
greater than, or less than anything, <em>including itself</em>. <code>x ==
x</code> is false if the value of <code>x</code> is NaN.  You can use this to test
whether a value is NaN or not, but the recommended way to test for NaN
is with the <code>isnan</code> function (see section <a href="#Floating-Point-Classes">Floating-Point Number Classification Functions</a>).  In
addition, <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, and <code>&gt;=</code> will raise an
exception when applied to NaNs.
</p>
<p>&lsquo;<tt>math.h</tt>&rsquo; defines macros that allow you to explicitly set a variable
to infinity or NaN.
</p>
<dl>
<dt><a name="index-INFINITY"></a><u>Macro:</u> float <b>INFINITY</b></dt>
<dd><p>An expression representing positive infinity.  It is equal to the value
produced  by mathematical operations like <code>1.0 / 0.0</code>.
<code>-INFINITY</code> represents negative infinity.
</p>
<p>You can test whether a floating-point value is infinite by comparing it
to this macro.  However, this is not recommended; you should use the
<code>isfinite</code> macro instead.  See section <a href="#Floating-Point-Classes">Floating-Point Number Classification Functions</a>.
</p>
<p>This macro was introduced in the ISO C99 standard.
</p></dd></dl>

<dl>
<dt><a name="index-NAN"></a><u>Macro:</u> float <b>NAN</b></dt>
<dd><p>An expression representing a value which is &ldquo;not a number&rdquo;.  This
macro is a GNU extension, available only on machines that support the
&ldquo;not a number&rdquo; value&mdash;that is to say, on all machines that support
IEEE floating point.
</p>
<p>You can use &lsquo;<samp>#ifdef NAN</samp>&rsquo; to test whether the machine supports
NaN.  (Of course, you must arrange for GNU extensions to be visible,
such as by defining <code>_GNU_SOURCE</code>, and then you must include
&lsquo;<tt>math.h</tt>&rsquo;.)
</p></dd></dl>

<p>IEEE 754 also allows for another unusual value: negative zero.  This
value is produced when you divide a positive number by negative
infinity, or when a negative result is smaller than the limits of
representation.  Negative zero behaves identically to zero in all
calculations, unless you explicitly test the sign bit with
<code>signbit</code> or <code>copysign</code>.
</p>
<hr size="6">
<a name="Status-bit-operations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Infinity-and-NaN" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Math-Error-Reporting" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Errors" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Examining-the-FPU-status-word"></a>
<h3 class="subsection">20.5.3 Examining the FPU status word</h3>

<p>ISO C99 defines functions to query and manipulate the
floating-point status word.  You can use these functions to check for
untrapped exceptions when it&rsquo;s convenient, rather than worrying about
them in the middle of a calculation.
</p>
<p>These constants represent the various IEEE 754 exceptions.  Not all
FPUs report all the different exceptions.  Each constant is defined if
and only if the FPU you are compiling for supports that exception, so
you can test for FPU support with &lsquo;<samp>#ifdef</samp>&rsquo;.  They are defined in
&lsquo;<tt>fenv.h</tt>&rsquo;.
</p>
<dl compact="compact">
<dt> <code>FE_INEXACT</code>
<a name="index-FE_005fINEXACT"></a>
</dt>
<dd><p> The inexact exception.
</p></dd>
<dt> <code>FE_DIVBYZERO</code>
<a name="index-FE_005fDIVBYZERO"></a>
</dt>
<dd><p> The divide by zero exception.
</p></dd>
<dt> <code>FE_UNDERFLOW</code>
<a name="index-FE_005fUNDERFLOW"></a>
</dt>
<dd><p> The underflow exception.
</p></dd>
<dt> <code>FE_OVERFLOW</code>
<a name="index-FE_005fOVERFLOW"></a>
</dt>
<dd><p> The overflow exception.
</p></dd>
<dt> <code>FE_INVALID</code>
<a name="index-FE_005fINVALID"></a>
</dt>
<dd><p> The invalid exception.
</p></dd>
</dl>

<p>The macro <code>FE_ALL_EXCEPT</code> is the bitwise OR of all exception macros
which are supported by the FP implementation.
</p>
<p>These functions allow you to clear exception flags, test for exceptions,
and save and restore the set of exceptions flagged.
</p>
<dl>
<dt><a name="index-feclearexcept"></a><u>Function:</u> int <b>feclearexcept</b><i> (int <var>excepts</var>)</i></dt>
<dd><p>This function clears all of the supported exception flags indicated by
<var>excepts</var>.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-feraiseexcept"></a><u>Function:</u> int <b>feraiseexcept</b><i> (int <var>excepts</var>)</i></dt>
<dd><p>This function raises the supported exceptions indicated by
<var>excepts</var>.  If more than one exception bit in <var>excepts</var> is set
the order in which the exceptions are raised is undefined except that
overflow (<code>FE_OVERFLOW</code>) or underflow (<code>FE_UNDERFLOW</code>) are
raised before inexact (<code>FE_INEXACT</code>).  Whether for overflow or
underflow the inexact exception is also raised is also implementation
dependent.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-fetestexcept"></a><u>Function:</u> int <b>fetestexcept</b><i> (int <var>excepts</var>)</i></dt>
<dd><p>Test whether the exception flags indicated by the parameter <var>except</var>
are currently set.  If any of them are, a nonzero value is returned
which specifies which exceptions are set.  Otherwise the result is zero.
</p></dd></dl>

<p>To understand these functions, imagine that the status word is an
integer variable named <var>status</var>.  <code>feclearexcept</code> is then
equivalent to &lsquo;<samp>status &amp;= ~excepts</samp>&rsquo; and <code>fetestexcept</code> is
equivalent to &lsquo;<samp>(status &amp; excepts)</samp>&rsquo;.  The actual implementation may
be very different, of course.
</p>
<p>Exception flags are only cleared when the program explicitly requests it,
by calling <code>feclearexcept</code>.  If you want to check for exceptions
from a set of calculations, you should clear all the flags first.  Here
is a simple example of the way to use <code>fetestexcept</code>:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">{
  double f;
  int raised;
  feclearexcept (FE_ALL_EXCEPT);
  f = compute ();
  raised = fetestexcept (FE_OVERFLOW | FE_INVALID);
  if (raised &amp; FE_OVERFLOW) { /* &hellip; */ }
  if (raised &amp; FE_INVALID) { /* &hellip; */ }
  /* &hellip; */
}
</pre></td></tr></table>

<p>You cannot explicitly set bits in the status word.  You can, however,
save the entire status word and restore it later.  This is done with the
following functions:
</p>
<dl>
<dt><a name="index-fegetexceptflag"></a><u>Function:</u> int <b>fegetexceptflag</b><i> (fexcept_t *<var>flagp</var>, int <var>excepts</var>)</i></dt>
<dd><p>This function stores in the variable pointed to by <var>flagp</var> an
implementation-defined value representing the current setting of the
exception flags indicated by <var>excepts</var>.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-fesetexceptflag"></a><u>Function:</u> int <b>fesetexceptflag</b><i> (const fexcept_t *<var>flagp</var>, int <var>excepts</var>)</i></dt>
<dd><p>This function restores the flags for the exceptions indicated by
<var>excepts</var> to the values stored in the variable pointed to by
<var>flagp</var>.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<p>Note that the value stored in <code>fexcept_t</code> bears no resemblance to
the bit mask returned by <code>fetestexcept</code>.  The type may not even be
an integer.  Do not attempt to modify an <code>fexcept_t</code> variable.
</p>
<hr size="6">
<a name="Math-Error-Reporting"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Status-bit-operations" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Rounding" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Floating-Point-Errors" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Error-Reporting-by-Mathematical-Functions"></a>
<h3 class="subsection">20.5.4 Error Reporting by Mathematical Functions</h3>
<a name="index-errors_002c-mathematical"></a>
<a name="index-domain-error"></a>
<a name="index-range-error"></a>

<p>Many of the math functions are defined only over a subset of the real or
complex numbers.  Even if they are mathematically defined, their result
may be larger or smaller than the range representable by their return
type.  These are known as <em>domain errors</em>, <em>overflows</em>, and
<em>underflows</em>, respectively.  Math functions do several things when
one of these errors occurs.  In this manual we will refer to the
complete response as <em>signalling</em> a domain error, overflow, or
underflow.
</p>
<p>When a math function suffers a domain error, it raises the invalid
exception and returns NaN.  It also sets <var>errno</var> to <code>EDOM</code>;
this is for compatibility with old systems that do not support IEEE
754 exception handling.  Likewise, when overflow occurs, math
functions raise the overflow exception and return <em>&amp;infin;</em> or
<em>-&amp;infin;</em> as appropriate.  They also set <var>errno</var> to
<code>ERANGE</code>.  When underflow occurs, the underflow exception is
raised, and zero (appropriately signed) is returned.  <var>errno</var> may be
set to <code>ERANGE</code>, but this is not guaranteed.
</p>
<p>Some of the math functions are defined mathematically to result in a
complex value over parts of their domains.  The most familiar example of
this is taking the square root of a negative number.  The complex math
functions, such as <code>csqrt</code>, will return the appropriate complex value
in this case.  The real-valued functions, such as <code>sqrt</code>, will
signal a domain error.
</p>
<p>Some older hardware does not support infinities.  On that hardware,
overflows instead return a particular very large number (usually the
largest representable number).  &lsquo;<tt>math.h</tt>&rsquo; defines macros you can use
to test for overflow on both old and new hardware.
</p>
<dl>
<dt><a name="index-HUGE_005fVAL"></a><u>Macro:</u> double <b>HUGE_VAL</b></dt>
<dt><a name="index-HUGE_005fVALF"></a><u>Macro:</u> float <b>HUGE_VALF</b></dt>
<dt><a name="index-HUGE_005fVALL"></a><u>Macro:</u> long double <b>HUGE_VALL</b></dt>
<dd><p>An expression representing a particular very large number.  On machines
that use IEEE 754 floating point format, <code>HUGE_VAL</code> is infinity.
On other machines, it&rsquo;s typically the largest positive number that can
be represented.
</p>
<p>Mathematical functions return the appropriately typed version of
<code>HUGE_VAL</code> or <code>-HUGE_VAL</code> when the result is too large
to be represented.
</p></dd></dl>

<hr size="6">
<a name="Rounding"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Math-Error-Reporting" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Control-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Rounding-Modes"></a>
<h2 class="section">20.6 Rounding Modes</h2>

<p>Floating-point calculations are carried out internally with extra
precision, and then rounded to fit into the destination type.  This
ensures that results are as precise as the input data.  IEEE 754
defines four possible rounding modes:
</p>
<dl compact="compact">
<dt> Round to nearest.</dt>
<dd><p>This is the default mode.  It should be used unless there is a specific
need for one of the others.  In this mode results are rounded to the
nearest representable value.  If the result is midway between two
representable values, the even representable is chosen. <em>Even</em> here
means the lowest-order bit is zero.  This rounding mode prevents
statistical bias and guarantees numeric stability: round-off errors in a
lengthy calculation will remain smaller than half of <code>FLT_EPSILON</code>.
</p>
</dd>
<dt> Round toward plus Infinity.</dt>
<dd><p>All results are rounded to the smallest representable value
which is greater than the result.
</p>
</dd>
<dt> Round toward minus Infinity.</dt>
<dd><p>All results are rounded to the largest representable value which is less
than the result.
</p>
</dd>
<dt> Round toward zero.</dt>
<dd><p>All results are rounded to the largest representable value whose
magnitude is less than that of the result.  In other words, if the
result is negative it is rounded up; if it is positive, it is rounded
down.
</p></dd>
</dl>

<p>&lsquo;<tt>fenv.h</tt>&rsquo; defines constants which you can use to refer to the
various rounding modes.  Each one will be defined if and only if the FPU
supports the corresponding rounding mode.
</p>
<dl compact="compact">
<dd><a name="index-FE_005fTONEAREST"></a>
</dd>
<dt> <code>FE_TONEAREST</code></dt>
<dd><p>Round to nearest.
</p>
<a name="index-FE_005fUPWARD"></a>
</dd>
<dt> <code>FE_UPWARD</code></dt>
<dd><p>Round toward <em>+&amp;infin;</em>.
</p>
<a name="index-FE_005fDOWNWARD"></a>
</dd>
<dt> <code>FE_DOWNWARD</code></dt>
<dd><p>Round toward <em>-&amp;infin;</em>.
</p>
<a name="index-FE_005fTOWARDZERO"></a>
</dd>
<dt> <code>FE_TOWARDZERO</code></dt>
<dd><p>Round toward zero.
</p></dd>
</dl>

<p>Underflow is an unusual case.  Normally, IEEE 754 floating point
numbers are always normalized (see section <a href="libc_34.html#Floating-Point-Concepts">Floating Point Representation Concepts</a>).
Numbers smaller than <em>2^r</em> (where <em>r</em> is the minimum exponent,
<code>FLT_MIN_RADIX-1</code> for <var>float</var>) cannot be represented as
normalized numbers.  Rounding all such numbers to zero or <em>2^r</em>
would cause some algorithms to fail at 0.  Therefore, they are left in
denormalized form.  That produces loss of precision, since some bits of
the mantissa are stolen to indicate the decimal point.
</p>
<p>If a result is too small to be represented as a denormalized number, it
is rounded to zero.  However, the sign of the result is preserved; if
the calculation was negative, the result is <em>negative zero</em>.
Negative zero can also result from some operations on infinity, such as
<em>4/-&amp;infin;</em>.  Negative zero behaves identically to zero except
when the <code>copysign</code> or <code>signbit</code> functions are used to check
the sign bit directly.
</p>
<p>At any time one of the above four rounding modes is selected.  You can
find out which one with this function:
</p>
<dl>
<dt><a name="index-fegetround"></a><u>Function:</u> int <b>fegetround</b><i> (void)</i></dt>
<dd><p>Returns the currently selected rounding mode, represented by one of the
values of the defined rounding mode macros.
</p></dd></dl>

<p>To change the rounding mode, use this function:
</p>
<dl>
<dt><a name="index-fesetround"></a><u>Function:</u> int <b>fesetround</b><i> (int <var>round</var>)</i></dt>
<dd><p>Changes the currently selected rounding mode to <var>round</var>.  If
<var>round</var> does not correspond to one of the supported rounding modes
nothing is changed.  <code>fesetround</code> returns zero if it changed the
rounding mode, a nonzero value if the mode is not supported.
</p></dd></dl>

<p>You should avoid changing the rounding mode if possible.  It can be an
expensive operation; also, some hardware requires you to compile your
program differently for it to work.  The resulting code may run slower.
See your compiler documentation for details.
</p>
<hr size="6">
<a name="Control-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Rounding" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Floating_002dPoint-Control-Functions"></a>
<h2 class="section">20.7 Floating-Point Control Functions</h2>

<p>IEEE 754 floating-point implementations allow the programmer to
decide whether traps will occur for each of the exceptions, by setting
bits in the <em>control word</em>.  In C, traps result in the program
receiving the <code>SIGFPE</code> signal; see <a href="libc_24.html#Signal-Handling">Signal Handling</a>.
</p>
<p><strong>NB:</strong> IEEE 754 says that trap handlers are given details of
the exceptional situation, and can set the result value.  C signals do
not provide any mechanism to pass this information back and forth.
Trapping exceptions in C is therefore not very useful.
</p>
<p>It is sometimes necessary to save the state of the floating-point unit
while you perform some calculation.  The library provides functions
which save and restore the exception flags, the set of exceptions that
generate traps, and the rounding mode.  This information is known as the
<em>floating-point environment</em>.
</p>
<p>The functions to save and restore the floating-point environment all use
a variable of type <code>fenv_t</code> to store information.  This type is
defined in &lsquo;<tt>fenv.h</tt>&rsquo;.  Its size and contents are
implementation-defined.  You should not attempt to manipulate a variable
of this type directly.
</p>
<p>To save the state of the FPU, use one of these functions:
</p>
<dl>
<dt><a name="index-fegetenv"></a><u>Function:</u> int <b>fegetenv</b><i> (fenv_t *<var>envp</var>)</i></dt>
<dd><p>Store the floating-point environment in the variable pointed to by
<var>envp</var>.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-feholdexcept"></a><u>Function:</u> int <b>feholdexcept</b><i> (fenv_t *<var>envp</var>)</i></dt>
<dd><p>Store the current floating-point environment in the object pointed to by
<var>envp</var>.  Then clear all exception flags, and set the FPU to trap no
exceptions.  Not all FPUs support trapping no exceptions; if
<code>feholdexcept</code> cannot set this mode, it returns nonzero value.  If it
succeeds, it returns zero.
</p></dd></dl>

<p>The functions which restore the floating-point environment can take these
kinds of arguments:
</p>
<ul>
<li>
Pointers to <code>fenv_t</code> objects, which were initialized previously by a
call to <code>fegetenv</code> or <code>feholdexcept</code>.
</li><li>
<a name="index-FE_005fDFL_005fENV"></a>
The special macro <code>FE_DFL_ENV</code> which represents the floating-point
environment as it was available at program start.
</li><li>
Implementation defined macros with names starting with <code>FE_</code> and
having type <code>fenv_t *</code>.

<a name="index-FE_005fNOMASK_005fENV"></a>
<p>If possible, the GNU C Library defines a macro <code>FE_NOMASK_ENV</code>
which represents an environment where every exception raised causes a
trap to occur.  You can test for this macro using <code>#ifdef</code>.  It is
only defined if <code>_GNU_SOURCE</code> is defined.
</p>
<p>Some platforms might define other predefined environments.
</p></li></ul>

<p>To set the floating-point environment, you can use either of these
functions:
</p>
<dl>
<dt><a name="index-fesetenv"></a><u>Function:</u> int <b>fesetenv</b><i> (const fenv_t *<var>envp</var>)</i></dt>
<dd><p>Set the floating-point environment to that described by <var>envp</var>.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-feupdateenv"></a><u>Function:</u> int <b>feupdateenv</b><i> (const fenv_t *<var>envp</var>)</i></dt>
<dd><p>Like <code>fesetenv</code>, this function sets the floating-point environment
to that described by <var>envp</var>.  However, if any exceptions were
flagged in the status word before <code>feupdateenv</code> was called, they
remain flagged after the call.  In other words, after <code>feupdateenv</code>
is called, the status word is the bitwise OR of the previous status word
and the one saved in <var>envp</var>.
</p>
<p>The function returns zero in case the operation was successful, a
non-zero value otherwise.
</p></dd></dl>

<p>To control for individual exceptions if raising them causes a trap to
occur, you can use the following two functions.
</p>
<p><strong>Portability Note:</strong> These functions are all GNU extensions.
</p>
<dl>
<dt><a name="index-feenableexcept"></a><u>Function:</u> int <b>feenableexcept</b><i> (int <var>excepts</var>)</i></dt>
<dd><p>This functions enables traps for each of the exceptions as indicated by
the parameter <var>except</var>.  The individual excepetions are described in
<a href="#Status-bit-operations">Examining the FPU status word</a>.  Only the specified exceptions are
enabled, the status of the other exceptions is not changed.
</p>
<p>The function returns the previous enabled exceptions in case the
operation was successful, <code>-1</code> otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-fedisableexcept"></a><u>Function:</u> int <b>fedisableexcept</b><i> (int <var>excepts</var>)</i></dt>
<dd><p>This functions disables traps for each of the exceptions as indicated by
the parameter <var>except</var>.  The individual excepetions are described in
<a href="#Status-bit-operations">Examining the FPU status word</a>.  Only the specified exceptions are
disabled, the status of the other exceptions is not changed.
</p>
<p>The function returns the previous enabled exceptions in case the
operation was successful, <code>-1</code> otherwise.
</p></dd></dl>

<dl>
<dt><a name="index-fegetexcept"></a><u>Function:</u> int <b>fegetexcept</b><i> (int <var>excepts</var>)</i></dt>
<dd><p>The function returns a bitmask of all currently enabled exceptions.  It
returns <code>-1</code> in case of failure.
</p></dd></dl>

<hr size="6">
<a name="Arithmetic-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Control-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Absolute-Value" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Arithmetic-Functions-2"></a>
<h2 class="section">20.8 Arithmetic Functions</h2>

<p>The C library provides functions to do basic operations on
floating-point numbers.  These include absolute value, maximum and minimum,
normalization, bit twiddling, rounding, and a few others.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Absolute-Value">20.8.1 Absolute Value</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">              Absolute values of integers and floats.
</td></tr>
<tr><td align="left" valign="top"><a href="#Normalization-Functions">20.8.2 Normalization Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">     Extracting exponents and putting them back.
</td></tr>
<tr><td align="left" valign="top"><a href="#Rounding-Functions">20.8.3 Rounding Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">          Rounding floats to integers.
</td></tr>
<tr><td align="left" valign="top"><a href="#Remainder-Functions">20.8.4 Remainder Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         Remainders on division, precisely defined.
</td></tr>
<tr><td align="left" valign="top"><a href="#FP-Bit-Twiddling">20.8.5 Setting and modifying single bits of FP values</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">            Sign bit adjustment.  Adding epsilon.
</td></tr>
<tr><td align="left" valign="top"><a href="#FP-Comparison-Functions">20.8.6 Floating-Point Comparison Functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">     Comparisons without risk of exceptions.
</td></tr>
<tr><td align="left" valign="top"><a href="#Misc-FP-Arithmetic">20.8.7 Miscellaneous FP arithmetic functions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">          Max, min, positive difference, multiply-add.
</td></tr>
</table>

<hr size="6">
<a name="Absolute-Value"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Normalization-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Absolute-Value-1"></a>
<h3 class="subsection">20.8.1 Absolute Value</h3>
<a name="index-absolute-value-functions"></a>

<p>These functions are provided for obtaining the <em>absolute value</em> (or
<em>magnitude</em>) of a number.  The absolute value of a real number
<var>x</var> is <var>x</var> if <var>x</var> is positive, -<var>x</var> if <var>x</var> is
negative.  For a complex number <var>z</var>, whose real part is <var>x</var> and
whose imaginary part is <var>y</var>, the absolute value is <code>sqrt
(<var>x</var>*<var>x</var> + <var>y</var>*<var>y</var>)</code>.
</p>
<a name="index-math_002eh-2"></a>
<a name="index-stdlib_002eh-16"></a>
<p>Prototypes for <code>abs</code>, <code>labs</code> and <code>llabs</code> are in &lsquo;<tt>stdlib.h</tt>&rsquo;;
<code>imaxabs</code> is declared in &lsquo;<tt>inttypes.h</tt>&rsquo;;
<code>fabs</code>, <code>fabsf</code> and <code>fabsl</code> are declared in &lsquo;<tt>math.h</tt>&rsquo;.
<code>cabs</code>, <code>cabsf</code> and <code>cabsl</code> are declared in &lsquo;<tt>complex.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-abs"></a><u>Function:</u> int <b>abs</b><i> (int <var>number</var>)</i></dt>
<dt><a name="index-labs"></a><u>Function:</u> long int <b>labs</b><i> (long int <var>number</var>)</i></dt>
<dt><a name="index-llabs"></a><u>Function:</u> long long int <b>llabs</b><i> (long long int <var>number</var>)</i></dt>
<dt><a name="index-imaxabs"></a><u>Function:</u> intmax_t <b>imaxabs</b><i> (intmax_t <var>number</var>)</i></dt>
<dd><p>These functions return the absolute value of <var>number</var>.
</p>
<p>Most computers use a two&rsquo;s complement integer representation, in which
the absolute value of <code>INT_MIN</code> (the smallest possible <code>int</code>)
cannot be represented; thus, <code>abs (INT_MIN)</code> is not defined.
</p>
<p><code>llabs</code> and <code>imaxdiv</code> are new to ISO C99.
</p>
<p>See <a href="#Integers">Integers</a> for a description of the <code>intmax_t</code> type.
</p>
</dd></dl>

<dl>
<dt><a name="index-fabs"></a><u>Function:</u> double <b>fabs</b><i> (double <var>number</var>)</i></dt>
<dt><a name="index-fabsf"></a><u>Function:</u> float <b>fabsf</b><i> (float <var>number</var>)</i></dt>
<dt><a name="index-fabsl"></a><u>Function:</u> long double <b>fabsl</b><i> (long double <var>number</var>)</i></dt>
<dd><p>This function returns the absolute value of the floating-point number
<var>number</var>.
</p></dd></dl>

<dl>
<dt><a name="index-cabs"></a><u>Function:</u> double <b>cabs</b><i> (complex double <var>z</var>)</i></dt>
<dt><a name="index-cabsf"></a><u>Function:</u> float <b>cabsf</b><i> (complex float <var>z</var>)</i></dt>
<dt><a name="index-cabsl"></a><u>Function:</u> long double <b>cabsl</b><i> (complex long double <var>z</var>)</i></dt>
<dd><p>These functions return the absolute  value of the complex number <var>z</var>
(see section <a href="#Complex-Numbers">Complex Numbers</a>).  The absolute value of a complex number is:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">sqrt (creal (<var>z</var>) * creal (<var>z</var>) + cimag (<var>z</var>) * cimag (<var>z</var>))
</pre></td></tr></table>

<p>This function should always be used instead of the direct formula
because it takes special care to avoid losing precision.  It may also
take advantage of hardware support for this operation. See <code>hypot</code>
in <a href="libc_19.html#Exponents-and-Logarithms">Exponentiation and Logarithms</a>.
</p></dd></dl>

<hr size="6">
<a name="Normalization-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Absolute-Value" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Rounding-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Normalization-Functions-1"></a>
<h3 class="subsection">20.8.2 Normalization Functions</h3>
<a name="index-normalization-functions-_0028floating_002dpoint_0029"></a>

<p>The functions described in this section are primarily provided as a way
to efficiently perform certain low-level manipulations on floating point
numbers that are represented internally using a binary radix;
see <a href="libc_34.html#Floating-Point-Concepts">Floating Point Representation Concepts</a>.  These functions are required to
have equivalent behavior even if the representation does not use a radix
of 2, but of course they are unlikely to be particularly efficient in
those cases.
</p>
<a name="index-math_002eh-3"></a>
<p>All these functions are declared in &lsquo;<tt>math.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-frexp"></a><u>Function:</u> double <b>frexp</b><i> (double <var>value</var>, int *<var>exponent</var>)</i></dt>
<dt><a name="index-frexpf"></a><u>Function:</u> float <b>frexpf</b><i> (float <var>value</var>, int *<var>exponent</var>)</i></dt>
<dt><a name="index-frexpl"></a><u>Function:</u> long double <b>frexpl</b><i> (long double <var>value</var>, int *<var>exponent</var>)</i></dt>
<dd><p>These functions are used to split the number <var>value</var>
into a normalized fraction and an exponent.
</p>
<p>If the argument <var>value</var> is not zero, the return value is <var>value</var>
times a power of two, and is always in the range 1/2 (inclusive) to 1
(exclusive).  The corresponding exponent is stored in
<code>*<var>exponent</var></code>; the return value multiplied by 2 raised to this
exponent equals the original number <var>value</var>.
</p>
<p>For example, <code>frexp (12.8, &amp;exponent)</code> returns <code>0.8</code> and
stores <code>4</code> in <code>exponent</code>.
</p>
<p>If <var>value</var> is zero, then the return value is zero and
zero is stored in <code>*<var>exponent</var></code>.
</p></dd></dl>

<dl>
<dt><a name="index-ldexp"></a><u>Function:</u> double <b>ldexp</b><i> (double <var>value</var>, int <var>exponent</var>)</i></dt>
<dt><a name="index-ldexpf"></a><u>Function:</u> float <b>ldexpf</b><i> (float <var>value</var>, int <var>exponent</var>)</i></dt>
<dt><a name="index-ldexpl"></a><u>Function:</u> long double <b>ldexpl</b><i> (long double <var>value</var>, int <var>exponent</var>)</i></dt>
<dd><p>These functions return the result of multiplying the floating-point
number <var>value</var> by 2 raised to the power <var>exponent</var>.  (It can
be used to reassemble floating-point numbers that were taken apart
by <code>frexp</code>.)
</p>
<p>For example, <code>ldexp (0.8, 4)</code> returns <code>12.8</code>.
</p></dd></dl>

<p>The following functions, which come from BSD, provide facilities
equivalent to those of <code>ldexp</code> and <code>frexp</code>.  See also the
ISO C function <code>logb</code> which originally also appeared in BSD.
</p>
<dl>
<dt><a name="index-scalb"></a><u>Function:</u> double <b>scalb</b><i> (double <var>value</var>, int <var>exponent</var>)</i></dt>
<dt><a name="index-scalbf"></a><u>Function:</u> float <b>scalbf</b><i> (float <var>value</var>, int <var>exponent</var>)</i></dt>
<dt><a name="index-scalbl"></a><u>Function:</u> long double <b>scalbl</b><i> (long double <var>value</var>, int <var>exponent</var>)</i></dt>
<dd><p>The <code>scalb</code> function is the BSD name for <code>ldexp</code>.
</p></dd></dl>

<dl>
<dt><a name="index-scalbn"></a><u>Function:</u> long long int <b>scalbn</b><i> (double <var>x</var>, int n)</i></dt>
<dt><a name="index-scalbnf"></a><u>Function:</u> long long int <b>scalbnf</b><i> (float <var>x</var>, int n)</i></dt>
<dt><a name="index-scalbnl"></a><u>Function:</u> long long int <b>scalbnl</b><i> (long double <var>x</var>, int n)</i></dt>
<dd><p><code>scalbn</code> is identical to <code>scalb</code>, except that the exponent
<var>n</var> is an <code>int</code> instead of a floating-point number.
</p></dd></dl>

<dl>
<dt><a name="index-scalbln"></a><u>Function:</u> long long int <b>scalbln</b><i> (double <var>x</var>, long int n)</i></dt>
<dt><a name="index-scalblnf"></a><u>Function:</u> long long int <b>scalblnf</b><i> (float <var>x</var>, long int n)</i></dt>
<dt><a name="index-scalblnl"></a><u>Function:</u> long long int <b>scalblnl</b><i> (long double <var>x</var>, long int n)</i></dt>
<dd><p><code>scalbln</code> is identical to <code>scalb</code>, except that the exponent
<var>n</var> is a <code>long int</code> instead of a floating-point number.
</p></dd></dl>

<dl>
<dt><a name="index-significand"></a><u>Function:</u> long long int <b>significand</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-significandf"></a><u>Function:</u> long long int <b>significandf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-significandl"></a><u>Function:</u> long long int <b>significandl</b><i> (long double <var>x</var>)</i></dt>
<dd><p><code>significand</code> returns the mantissa of <var>x</var> scaled to the range
<em>[1, 2)</em>.
It is equivalent to <code>scalb (<var>x</var>, (double) -ilogb (<var>x</var>))</code>.
</p>
<p>This function exists mainly for use in certain standardized tests
of IEEE 754 conformance.
</p></dd></dl>

<hr size="6">
<a name="Rounding-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Normalization-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Remainder-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Rounding-Functions-1"></a>
<h3 class="subsection">20.8.3 Rounding Functions</h3>
<a name="index-converting-floats-to-integers"></a>

<a name="index-math_002eh-4"></a>
<p>The functions listed here perform operations such as rounding and
truncation of floating-point values. Some of these functions convert
floating point numbers to integer values.  They are all declared in
&lsquo;<tt>math.h</tt>&rsquo;.
</p>
<p>You can also convert floating-point numbers to integers simply by
casting them to <code>int</code>.  This discards the fractional part,
effectively rounding towards zero.  However, this only works if the
result can actually be represented as an <code>int</code>&mdash;for very large
numbers, this is impossible.  The functions listed here return the
result as a <code>double</code> instead to get around this problem.
</p>
<dl>
<dt><a name="index-ceil"></a><u>Function:</u> double <b>ceil</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-ceilf"></a><u>Function:</u> float <b>ceilf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-ceill"></a><u>Function:</u> long double <b>ceill</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions round <var>x</var> upwards to the nearest integer,
returning that value as a <code>double</code>.  Thus, <code>ceil (1.5)</code>
is <code>2.0</code>.
</p></dd></dl>

<dl>
<dt><a name="index-floor"></a><u>Function:</u> double <b>floor</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-floorf"></a><u>Function:</u> float <b>floorf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-floorl"></a><u>Function:</u> long double <b>floorl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions round <var>x</var> downwards to the nearest
integer, returning that value as a <code>double</code>.  Thus, <code>floor
(1.5)</code> is <code>1.0</code> and <code>floor (-1.5)</code> is <code>-2.0</code>.
</p></dd></dl>

<dl>
<dt><a name="index-trunc"></a><u>Function:</u> double <b>trunc</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-truncf"></a><u>Function:</u> float <b>truncf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-truncl"></a><u>Function:</u> long double <b>truncl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>The <code>trunc</code> functions round <var>x</var> towards zero to the nearest
integer (returned in floating-point format).  Thus, <code>trunc (1.5)</code>
is <code>1.0</code> and <code>trunc (-1.5)</code> is <code>-1.0</code>.
</p></dd></dl>

<dl>
<dt><a name="index-rint"></a><u>Function:</u> double <b>rint</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-rintf"></a><u>Function:</u> float <b>rintf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-rintl"></a><u>Function:</u> long double <b>rintl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions round <var>x</var> to an integer value according to the
current rounding mode.  See section <a href="libc_34.html#Floating-Point-Parameters">Floating Point Parameters</a>, for
information about the various rounding modes.  The default
rounding mode is to round to the nearest integer; some machines
support other modes, but round-to-nearest is always used unless
you explicitly select another.
</p>
<p>If <var>x</var> was not initially an integer, these functions raise the
inexact exception.
</p></dd></dl>

<dl>
<dt><a name="index-nearbyint"></a><u>Function:</u> double <b>nearbyint</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-nearbyintf"></a><u>Function:</u> float <b>nearbyintf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-nearbyintl"></a><u>Function:</u> long double <b>nearbyintl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions return the same value as the <code>rint</code> functions, but
do not raise the inexact exception if <var>x</var> is not an integer.
</p></dd></dl>

<dl>
<dt><a name="index-round"></a><u>Function:</u> double <b>round</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-roundf"></a><u>Function:</u> float <b>roundf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-roundl"></a><u>Function:</u> long double <b>roundl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions are similar to <code>rint</code>, but they round halfway
cases away from zero instead of to the nearest integer (or other
current rounding mode).
</p></dd></dl>

<dl>
<dt><a name="index-lrint"></a><u>Function:</u> long int <b>lrint</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-lrintf"></a><u>Function:</u> long int <b>lrintf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-lrintl"></a><u>Function:</u> long int <b>lrintl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions are just like <code>rint</code>, but they return a
<code>long int</code> instead of a floating-point number.
</p></dd></dl>

<dl>
<dt><a name="index-llrint"></a><u>Function:</u> long long int <b>llrint</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-llrintf"></a><u>Function:</u> long long int <b>llrintf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-llrintl"></a><u>Function:</u> long long int <b>llrintl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions are just like <code>rint</code>, but they return a
<code>long long int</code> instead of a floating-point number.
</p></dd></dl>

<dl>
<dt><a name="index-lround"></a><u>Function:</u> long int <b>lround</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-lroundf"></a><u>Function:</u> long int <b>lroundf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-lroundl"></a><u>Function:</u> long int <b>lroundl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions are just like <code>round</code>, but they return a
<code>long int</code> instead of a floating-point number.
</p></dd></dl>

<dl>
<dt><a name="index-llround"></a><u>Function:</u> long long int <b>llround</b><i> (double <var>x</var>)</i></dt>
<dt><a name="index-llroundf"></a><u>Function:</u> long long int <b>llroundf</b><i> (float <var>x</var>)</i></dt>
<dt><a name="index-llroundl"></a><u>Function:</u> long long int <b>llroundl</b><i> (long double <var>x</var>)</i></dt>
<dd><p>These functions are just like <code>round</code>, but they return a
<code>long long int</code> instead of a floating-point number.
</p></dd></dl>


<dl>
<dt><a name="index-modf"></a><u>Function:</u> double <b>modf</b><i> (double <var>value</var>, double *<var>integer-part</var>)</i></dt>
<dt><a name="index-modff"></a><u>Function:</u> float <b>modff</b><i> (float <var>value</var>, float *<var>integer-part</var>)</i></dt>
<dt><a name="index-modfl"></a><u>Function:</u> long double <b>modfl</b><i> (long double <var>value</var>, long double *<var>integer-part</var>)</i></dt>
<dd><p>These functions break the argument <var>value</var> into an integer part and a
fractional part (between <code>-1</code> and <code>1</code>, exclusive).  Their sum
equals <var>value</var>.  Each of the parts has the same sign as <var>value</var>,
and the integer part is always rounded toward zero.
</p>
<p><code>modf</code> stores the integer part in <code>*<var>integer-part</var></code>, and
returns the fractional part.  For example, <code>modf (2.5, &amp;intpart)</code>
returns <code>0.5</code> and stores <code>2.0</code> into <code>intpart</code>.
</p></dd></dl>

<hr size="6">
<a name="Remainder-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Rounding-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#FP-Bit-Twiddling" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Remainder-Functions-1"></a>
<h3 class="subsection">20.8.4 Remainder Functions</h3>

<p>The functions in this section compute the remainder on division of two
floating-point numbers.  Each is a little different; pick the one that
suits your problem.
</p>
<dl>
<dt><a name="index-fmod"></a><u>Function:</u> double <b>fmod</b><i> (double <var>numerator</var>, double <var>denominator</var>)</i></dt>
<dt><a name="index-fmodf"></a><u>Function:</u> float <b>fmodf</b><i> (float <var>numerator</var>, float <var>denominator</var>)</i></dt>
<dt><a name="index-fmodl"></a><u>Function:</u> long double <b>fmodl</b><i> (long double <var>numerator</var>, long double <var>denominator</var>)</i></dt>
<dd><p>These functions compute the remainder from the division of
<var>numerator</var> by <var>denominator</var>.  Specifically, the return value is
<code><var>numerator</var> - <var>n</var> * <var>denominator</var></code>, where <var>n</var>
is the quotient of <var>numerator</var> divided by <var>denominator</var>, rounded
towards zero to an integer.  Thus, <code>fmod (6.5, 2.3)</code> returns
<code>1.9</code>, which is <code>6.5</code> minus <code>4.6</code>.
</p>
<p>The result has the same sign as the <var>numerator</var> and has magnitude
less than the magnitude of the <var>denominator</var>.
</p>
<p>If <var>denominator</var> is zero, <code>fmod</code> signals a domain error.
</p></dd></dl>

<dl>
<dt><a name="index-drem"></a><u>Function:</u> double <b>drem</b><i> (double <var>numerator</var>, double <var>denominator</var>)</i></dt>
<dt><a name="index-dremf"></a><u>Function:</u> float <b>dremf</b><i> (float <var>numerator</var>, float <var>denominator</var>)</i></dt>
<dt><a name="index-dreml"></a><u>Function:</u> long double <b>dreml</b><i> (long double <var>numerator</var>, long double <var>denominator</var>)</i></dt>
<dd><p>These functions are like <code>fmod</code> except that they round the
internal quotient <var>n</var> to the nearest integer instead of towards zero
to an integer.  For example, <code>drem (6.5, 2.3)</code> returns <code>-0.4</code>,
which is <code>6.5</code> minus <code>6.9</code>.
</p>
<p>The absolute value of the result is less than or equal to half the
absolute value of the <var>denominator</var>.  The difference between
<code>fmod (<var>numerator</var>, <var>denominator</var>)</code> and <code>drem
(<var>numerator</var>, <var>denominator</var>)</code> is always either
<var>denominator</var>, minus <var>denominator</var>, or zero.
</p>
<p>If <var>denominator</var> is zero, <code>drem</code> signals a domain error.
</p></dd></dl>

<dl>
<dt><a name="index-remainder"></a><u>Function:</u> double <b>remainder</b><i> (double <var>numerator</var>, double <var>denominator</var>)</i></dt>
<dt><a name="index-remainderf"></a><u>Function:</u> float <b>remainderf</b><i> (float <var>numerator</var>, float <var>denominator</var>)</i></dt>
<dt><a name="index-remainderl"></a><u>Function:</u> long double <b>remainderl</b><i> (long double <var>numerator</var>, long double <var>denominator</var>)</i></dt>
<dd><p>This function is another name for <code>drem</code>.
</p></dd></dl>

<hr size="6">
<a name="FP-Bit-Twiddling"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Remainder-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#FP-Comparison-Functions" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Setting-and-modifying-single-bits-of-FP-values"></a>
<h3 class="subsection">20.8.5 Setting and modifying single bits of FP values</h3>
<a name="index-FP-arithmetic"></a>

<p>There are some operations that are too complicated or expensive to
perform by hand on floating-point numbers.  ISO C99 defines
functions to do these operations, which mostly involve changing single
bits.
</p>
<dl>
<dt><a name="index-copysign"></a><u>Function:</u> double <b>copysign</b><i> (double <var>x</var>, double <var>y</var>)</i></dt>
<dt><a name="index-copysignf"></a><u>Function:</u> float <b>copysignf</b><i> (float <var>x</var>, float <var>y</var>)</i></dt>
<dt><a name="index-copysignl"></a><u>Function:</u> long double <b>copysignl</b><i> (long double <var>x</var>, long double <var>y</var>)</i></dt>
<dd><p>These functions return <var>x</var> but with the sign of <var>y</var>.  They work
even if <var>x</var> or <var>y</var> are NaN or zero.  Both of these can carry a
sign (although not all implementations support it) and this is one of
the few operations that can tell the difference.
</p>
<p><code>copysign</code> never raises an exception.
</p>
<p>This function is defined in IEC 559 (and the appendix with
recommended functions in IEEE 754/IEEE 854).
</p></dd></dl>

<dl>
<dt><a name="index-signbit"></a><u>Function:</u> int <b>signbit</b><i> (<em>float-type</em> <var>x</var>)</i></dt>
<dd><p><code>signbit</code> is a generic macro which can work on all floating-point
types.  It returns a nonzero value if the value of <var>x</var> has its sign
bit set.
</p>
<p>This is not the same as <code>x &lt; 0.0</code>, because IEEE 754 floating
point allows zero to be signed.  The comparison <code>-0.0 &lt; 0.0</code> is
false, but <code>signbit (-0.0)</code> will return a nonzero value.
</p></dd></dl>

<dl>
<dt><a name="index-nextafter"></a><u>Function:</u> double <b>nextafter</b><i> (double <var>x</var>, double <var>y</var>)</i></dt>
<dt><a name="index-nextafterf"></a><u>Function:</u> float <b>nextafterf</b><i> (float <var>x</var>, float <var>y</var>)</i></dt>
<dt><a name="index-nextafterl"></a><u>Function:</u> long double <b>nextafterl</b><i> (long double <var>x</var>, long double <var>y</var>)</i></dt>
<dd><p>The <code>nextafter</code> function returns the next representable neighbor of
<var>x</var> in the direction towards <var>y</var>.  The size of the step between
<var>x</var> and the result depends on the type of the result.  If
<em><var>x</var> = <var>y</var></em> the function simply returns <var>y</var>.  If either
value is <code>NaN</code>, <code>NaN</code> is returned.  Otherwise
a value corresponding to the value of the least significant bit in the
mantissa is added or subtracted, depending on the direction.
<code>nextafter</code> will signal overflow or underflow if the result goes
outside of the range of normalized numbers.
</p>
<p>This function is defined in IEC 559 (and the appendix with
recommended functions in IEEE 754/IEEE 854).
</p></dd></dl>

<dl>
<dt><a name="index-nexttoward"></a><u>Function:</u> double <b>nexttoward</b><i> (double <var>x</var>, long double <var>y</var>)</i></dt>
<dt><a name="index-nexttowardf"></a><u>Function:</u> float <b>nexttowardf</b><i> (float <var>x</var>, long double <var>y</var>)</i></dt>
<dt><a name="index-nexttowardl"></a><u>Function:</u> long double <b>nexttowardl</b><i> (long double <var>x</var>, long double <var>y</var>)</i></dt>
<dd><p>These functions are identical to the corresponding versions of
<code>nextafter</code> except that their second argument is a <code>long
double</code>.
</p></dd></dl>

<a name="index-NaN-1"></a>
<dl>
<dt><a name="index-nan"></a><u>Function:</u> double <b>nan</b><i> (const char *<var>tagp</var>)</i></dt>
<dt><a name="index-nanf"></a><u>Function:</u> float <b>nanf</b><i> (const char *<var>tagp</var>)</i></dt>
<dt><a name="index-nanl"></a><u>Function:</u> long double <b>nanl</b><i> (const char *<var>tagp</var>)</i></dt>
<dd><p>The <code>nan</code> function returns a representation of NaN, provided that
NaN is supported by the target platform.
<code>nan (&quot;<var>n-char-sequence</var>&quot;)</code> is equivalent to
<code>strtod (&quot;NAN(<var>n-char-sequence</var>)&quot;)</code>.
</p>
<p>The argument <var>tagp</var> is used in an unspecified manner.  On IEEE
754 systems, there are many representations of NaN, and <var>tagp</var>
selects one.  On other systems it may do nothing.
</p></dd></dl>

<hr size="6">
<a name="FP-Comparison-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#FP-Bit-Twiddling" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Misc-FP-Arithmetic" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Floating_002dPoint-Comparison-Functions"></a>
<h3 class="subsection">20.8.6 Floating-Point Comparison Functions</h3>
<a name="index-unordered-comparison"></a>

<p>The standard C comparison operators provoke exceptions when one or other
of the operands is NaN.  For example,
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">int v = a &lt; 1.0;
</pre></td></tr></table>

<p>will raise an exception if <var>a</var> is NaN.  (This does <em>not</em>
happen with <code>==</code> and <code>!=</code>; those merely return false and true,
respectively, when NaN is examined.)  Frequently this exception is
undesirable.  ISO C99 therefore defines comparison functions that
do not raise exceptions when NaN is examined.  All of the functions are
implemented as macros which allow their arguments to be of any
floating-point type.  The macros are guaranteed to evaluate their
arguments only once.
</p>
<dl>
<dt><a name="index-isgreater"></a><u>Macro:</u> int <b>isgreater</b><i> (<em>real-floating</em> <var>x</var>, <em>real-floating</em> <var>y</var>)</i></dt>
<dd><p>This macro determines whether the argument <var>x</var> is greater than
<var>y</var>.  It is equivalent to <code>(<var>x</var>) &gt; (<var>y</var>)</code>, but no
exception is raised if <var>x</var> or <var>y</var> are NaN.
</p></dd></dl>

<dl>
<dt><a name="index-isgreaterequal"></a><u>Macro:</u> int <b>isgreaterequal</b><i> (<em>real-floating</em> <var>x</var>, <em>real-floating</em> <var>y</var>)</i></dt>
<dd><p>This macro determines whether the argument <var>x</var> is greater than or
equal to <var>y</var>.  It is equivalent to <code>(<var>x</var>) &gt;= (<var>y</var>)</code>, but no
exception is raised if <var>x</var> or <var>y</var> are NaN.
</p></dd></dl>

<dl>
<dt><a name="index-isless"></a><u>Macro:</u> int <b>isless</b><i> (<em>real-floating</em> <var>x</var>, <em>real-floating</em> <var>y</var>)</i></dt>
<dd><p>This macro determines whether the argument <var>x</var> is less than <var>y</var>.
It is equivalent to <code>(<var>x</var>) &lt; (<var>y</var>)</code>, but no exception is
raised if <var>x</var> or <var>y</var> are NaN.
</p></dd></dl>

<dl>
<dt><a name="index-islessequal"></a><u>Macro:</u> int <b>islessequal</b><i> (<em>real-floating</em> <var>x</var>, <em>real-floating</em> <var>y</var>)</i></dt>
<dd><p>This macro determines whether the argument <var>x</var> is less than or equal
to <var>y</var>.  It is equivalent to <code>(<var>x</var>) &lt;= (<var>y</var>)</code>, but no
exception is raised if <var>x</var> or <var>y</var> are NaN.
</p></dd></dl>

<dl>
<dt><a name="index-islessgreater"></a><u>Macro:</u> int <b>islessgreater</b><i> (<em>real-floating</em> <var>x</var>, <em>real-floating</em> <var>y</var>)</i></dt>
<dd><p>This macro determines whether the argument <var>x</var> is less or greater
than <var>y</var>.  It is equivalent to <code>(<var>x</var>) &lt; (<var>y</var>) ||
(<var>x</var>) &gt; (<var>y</var>)</code> (although it only evaluates <var>x</var> and <var>y</var>
once), but no exception is raised if <var>x</var> or <var>y</var> are NaN.
</p>
<p>This macro is not equivalent to <code><var>x</var> != <var>y</var></code>, because that
expression is true if <var>x</var> or <var>y</var> are NaN.
</p></dd></dl>

<dl>
<dt><a name="index-isunordered"></a><u>Macro:</u> int <b>isunordered</b><i> (<em>real-floating</em> <var>x</var>, <em>real-floating</em> <var>y</var>)</i></dt>
<dd><p>This macro determines whether its arguments are unordered.  In other
words, it is true if <var>x</var> or <var>y</var> are NaN, and false otherwise.
</p></dd></dl>

<p>Not all machines provide hardware support for these operations.  On
machines that don&rsquo;t, the macros can be very slow.  Therefore, you should
not use these functions when NaN is not a concern.
</p>
<p><strong>NB:</strong> There are no macros <code>isequal</code> or <code>isunequal</code>.
They are unnecessary, because the <code>==</code> and <code>!=</code> operators do
<em>not</em> throw an exception if one or both of the operands are NaN.
</p>
<hr size="6">
<a name="Misc-FP-Arithmetic"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#FP-Comparison-Functions" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Complex-Numbers" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Miscellaneous-FP-arithmetic-functions"></a>
<h3 class="subsection">20.8.7 Miscellaneous FP arithmetic functions</h3>
<a name="index-minimum"></a>
<a name="index-maximum"></a>
<a name="index-positive-difference"></a>
<a name="index-multiply_002dadd"></a>

<p>The functions in this section perform miscellaneous but common
operations that are awkward to express with C operators.  On some
processors these functions can use special machine instructions to
perform these operations faster than the equivalent C code.
</p>
<dl>
<dt><a name="index-fmin"></a><u>Function:</u> double <b>fmin</b><i> (double <var>x</var>, double <var>y</var>)</i></dt>
<dt><a name="index-fminf"></a><u>Function:</u> float <b>fminf</b><i> (float <var>x</var>, float <var>y</var>)</i></dt>
<dt><a name="index-fminl"></a><u>Function:</u> long double <b>fminl</b><i> (long double <var>x</var>, long double <var>y</var>)</i></dt>
<dd><p>The <code>fmin</code> function returns the lesser of the two values <var>x</var>
and <var>y</var>.  It is similar to the expression
</p><table><tr><td>&nbsp;</td><td><pre class="smallexample">((x) &lt; (y) ? (x) : (y))
</pre></td></tr></table>
<p>except that <var>x</var> and <var>y</var> are only evaluated once.
</p>
<p>If an argument is NaN, the other argument is returned.  If both arguments
are NaN, NaN is returned.
</p></dd></dl>

<dl>
<dt><a name="index-fmax"></a><u>Function:</u> double <b>fmax</b><i> (double <var>x</var>, double <var>y</var>)</i></dt>
<dt><a name="index-fmaxf"></a><u>Function:</u> float <b>fmaxf</b><i> (float <var>x</var>, float <var>y</var>)</i></dt>
<dt><a name="index-fmaxl"></a><u>Function:</u> long double <b>fmaxl</b><i> (long double <var>x</var>, long double <var>y</var>)</i></dt>
<dd><p>The <code>fmax</code> function returns the greater of the two values <var>x</var>
and <var>y</var>.
</p>
<p>If an argument is NaN, the other argument is returned.  If both arguments
are NaN, NaN is returned.
</p></dd></dl>

<dl>
<dt><a name="index-fdim"></a><u>Function:</u> double <b>fdim</b><i> (double <var>x</var>, double <var>y</var>)</i></dt>
<dt><a name="index-fdimf"></a><u>Function:</u> float <b>fdimf</b><i> (float <var>x</var>, float <var>y</var>)</i></dt>
<dt><a name="index-fdiml"></a><u>Function:</u> long double <b>fdiml</b><i> (long double <var>x</var>, long double <var>y</var>)</i></dt>
<dd><p>The <code>fdim</code> function returns the positive difference between
<var>x</var> and <var>y</var>.  The positive difference is <em><var>x</var> -
<var>y</var></em> if <var>x</var> is greater than <var>y</var>, and <em>0</em> otherwise.
</p>
<p>If <var>x</var>, <var>y</var>, or both are NaN, NaN is returned.
</p></dd></dl>

<dl>
<dt><a name="index-fma"></a><u>Function:</u> double <b>fma</b><i> (double <var>x</var>, double <var>y</var>, double <var>z</var>)</i></dt>
<dt><a name="index-fmaf"></a><u>Function:</u> float <b>fmaf</b><i> (float <var>x</var>, float <var>y</var>, float <var>z</var>)</i></dt>
<dt><a name="index-fmal"></a><u>Function:</u> long double <b>fmal</b><i> (long double <var>x</var>, long double <var>y</var>, long double <var>z</var>)</i></dt>
<dd><a name="index-butterfly"></a>
<p>The <code>fma</code> function performs floating-point multiply-add.  This is
the operation <em>(<var>x</var> &amp;middot; <var>y</var>) + <var>z</var></em>, but the
intermediate result is not rounded to the destination type.  This can
sometimes improve the precision of a calculation.
</p>
<p>This function was introduced because some processors have a special
instruction to perform multiply-add.  The C compiler cannot use it
directly, because the expression &lsquo;<samp>x*y + z</samp>&rsquo; is defined to round the
intermediate result.  <code>fma</code> lets you choose when you want to round
only once.
</p>
<a name="index-FP_005fFAST_005fFMA"></a>
<p>On processors which do not implement multiply-add in hardware,
<code>fma</code> can be very slow since it must avoid intermediate rounding.
&lsquo;<tt>math.h</tt>&rsquo; defines the symbols <code>FP_FAST_FMA</code>,
<code>FP_FAST_FMAF</code>, and <code>FP_FAST_FMAL</code> when the corresponding
version of <code>fma</code> is no slower than the expression &lsquo;<samp>x*y + z</samp>&rsquo;.
In the GNU C library, this always means the operation is implemented in
hardware.
</p></dd></dl>

<hr size="6">
<a name="Complex-Numbers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Misc-FP-Arithmetic" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Operations-on-Complex" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Complex-Numbers-1"></a>
<h2 class="section">20.9 Complex Numbers</h2>
<a name="index-complex_002eh-1"></a>
<a name="index-complex-numbers"></a>

<p>ISO C99 introduces support for complex numbers in C.  This is done
with a new type qualifier, <code>complex</code>.  It is a keyword if and only
if &lsquo;<tt>complex.h</tt>&rsquo; has been included.  There are three complex types,
corresponding to the three real types:  <code>float complex</code>,
<code>double complex</code>, and <code>long double complex</code>.
</p>
<p>To construct complex numbers you need a way to indicate the imaginary
part of a number.  There is no standard notation for an imaginary
floating point constant.  Instead, &lsquo;<tt>complex.h</tt>&rsquo; defines two macros
that can be used to create complex numbers.
</p>
<dl>
<dt><a name="index-_005fComplex_005fI"></a><u>Macro:</u> const float complex <b>_Complex_I</b></dt>
<dd><p>This macro is a representation of the complex number &ldquo;<em>0+1i</em>&rdquo;.
Multiplying a real floating-point value by <code>_Complex_I</code> gives a
complex number whose value is purely imaginary.  You can use this to
construct complex constants:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample"><em>3.0 + 4.0i</em> = <code>3.0 + 4.0 * _Complex_I</code>
</pre></td></tr></table>

<p>Note that <code>_Complex_I * _Complex_I</code> has the value <code>-1</code>, but
the type of that value is <code>complex</code>.
</p></dd></dl>


<p><code>_Complex_I</code> is a bit of a mouthful.  &lsquo;<tt>complex.h</tt>&rsquo; also defines
a shorter name for the same constant.
</p>
<dl>
<dt><a name="index-I"></a><u>Macro:</u> const float complex <b>I</b></dt>
<dd><p>This macro has exactly the same value as <code>_Complex_I</code>.  Most of the
time it is preferable.  However, it causes problems if you want to use
the identifier <code>I</code> for something else.  You can safely write
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">#include &lt;complex.h&gt;
#undef I
</pre></td></tr></table>

<p>if you need <code>I</code> for your own purposes.  (In that case we recommend
you also define some other short name for <code>_Complex_I</code>, such as
<code>J</code>.)
</p>
</dd></dl>

<hr size="6">
<a name="Operations-on-Complex"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Complex-Numbers" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Parsing-of-Numbers" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Projections_002c-Conjugates_002c-and-Decomposing-of-Complex-Numbers"></a>
<h2 class="section">20.10 Projections, Conjugates, and Decomposing of Complex Numbers</h2>
<a name="index-project-complex-numbers"></a>
<a name="index-conjugate-complex-numbers"></a>
<a name="index-decompose-complex-numbers"></a>
<a name="index-complex_002eh-2"></a>

<p>ISO C99 also defines functions that perform basic operations on
complex numbers, such as decomposition and conjugation.  The prototypes
for all these functions are in &lsquo;<tt>complex.h</tt>&rsquo;.  All functions are
available in three variants, one for each of the three complex types.
</p>
<dl>
<dt><a name="index-creal"></a><u>Function:</u> double <b>creal</b><i> (complex double <var>z</var>)</i></dt>
<dt><a name="index-crealf"></a><u>Function:</u> float <b>crealf</b><i> (complex float <var>z</var>)</i></dt>
<dt><a name="index-creall"></a><u>Function:</u> long double <b>creall</b><i> (complex long double <var>z</var>)</i></dt>
<dd><p>These functions return the real part of the complex number <var>z</var>.
</p></dd></dl>

<dl>
<dt><a name="index-cimag"></a><u>Function:</u> double <b>cimag</b><i> (complex double <var>z</var>)</i></dt>
<dt><a name="index-cimagf"></a><u>Function:</u> float <b>cimagf</b><i> (complex float <var>z</var>)</i></dt>
<dt><a name="index-cimagl"></a><u>Function:</u> long double <b>cimagl</b><i> (complex long double <var>z</var>)</i></dt>
<dd><p>These functions return the imaginary part of the complex number <var>z</var>.
</p></dd></dl>

<dl>
<dt><a name="index-conj"></a><u>Function:</u> complex double <b>conj</b><i> (complex double <var>z</var>)</i></dt>
<dt><a name="index-conjf"></a><u>Function:</u> complex float <b>conjf</b><i> (complex float <var>z</var>)</i></dt>
<dt><a name="index-conjl"></a><u>Function:</u> complex long double <b>conjl</b><i> (complex long double <var>z</var>)</i></dt>
<dd><p>These functions return the conjugate value of the complex number
<var>z</var>.  The conjugate of a complex number has the same real part and a
negated imaginary part.  In other words, &lsquo;<samp>conj(a + bi) = a + -bi</samp>&rsquo;.
</p></dd></dl>

<dl>
<dt><a name="index-carg"></a><u>Function:</u> double <b>carg</b><i> (complex double <var>z</var>)</i></dt>
<dt><a name="index-cargf"></a><u>Function:</u> float <b>cargf</b><i> (complex float <var>z</var>)</i></dt>
<dt><a name="index-cargl"></a><u>Function:</u> long double <b>cargl</b><i> (complex long double <var>z</var>)</i></dt>
<dd><p>These functions return the argument of the complex number <var>z</var>.
The argument of a complex number is the angle in the complex plane
between the positive real axis and a line passing through zero and the
number.  This angle is measured in the usual fashion and ranges from <em>0</em>
to <em>2&amp;pi;</em>.
</p>
<p><code>carg</code> has a branch cut along the positive real axis.
</p></dd></dl>

<dl>
<dt><a name="index-cproj"></a><u>Function:</u> complex double <b>cproj</b><i> (complex double <var>z</var>)</i></dt>
<dt><a name="index-cprojf"></a><u>Function:</u> complex float <b>cprojf</b><i> (complex float <var>z</var>)</i></dt>
<dt><a name="index-cprojl"></a><u>Function:</u> complex long double <b>cprojl</b><i> (complex long double <var>z</var>)</i></dt>
<dd><p>These functions return the projection of the complex value <var>z</var> onto
the Riemann sphere.  Values with a infinite imaginary part are projected
to positive infinity on the real axis, even if the real part is NaN.  If
the real part is infinite, the result is equivalent to
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">INFINITY + I * copysign (0.0, cimag (z))
</pre></td></tr></table>
</dd></dl>

<hr size="6">
<a name="Parsing-of-Numbers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Operations-on-Complex" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Parsing-of-Integers" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-of-Numbers-1"></a>
<h2 class="section">20.11 Parsing of Numbers</h2>
<a name="index-parsing-numbers-_0028in-formatted-input_0029"></a>
<a name="index-converting-strings-to-numbers"></a>
<a name="index-number-syntax_002c-parsing"></a>
<a name="index-syntax_002c-for-reading-numbers"></a>

<p>This section describes functions for &ldquo;reading&rdquo; integer and
floating-point numbers from a string.  It may be more convenient in some
cases to use <code>sscanf</code> or one of the related functions; see
<a href="libc_12.html#Formatted-Input">Formatted Input</a>.  But often you can make a program more robust by
finding the tokens in the string by hand, then converting the numbers
one by one.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Parsing-of-Integers">20.11.1 Parsing of Integers</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">         Functions for conversion of integer values.
</td></tr>
<tr><td align="left" valign="top"><a href="#Parsing-of-Floats">20.11.2 Parsing of Floats</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">           Functions for conversion of floating-point
				 values.
</td></tr>
</table>

<hr size="6">
<a name="Parsing-of-Integers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Parsing-of-Numbers" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Parsing-of-Floats" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Parsing-of-Numbers" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-of-Integers-1"></a>
<h3 class="subsection">20.11.1 Parsing of Integers</h3>

<a name="index-stdlib_002eh-17"></a>
<a name="index-wchar_002eh-15"></a>
<p>The &lsquo;<samp>str</samp>&rsquo; functions are declared in &lsquo;<tt>stdlib.h</tt>&rsquo; and those
beginning with &lsquo;<samp>wcs</samp>&rsquo; are declared in &lsquo;<tt>wchar.h</tt>&rsquo;.  One might
wonder about the use of <code>restrict</code> in the prototypes of the
functions in this section.  It is seemingly useless but the ISO C
standard uses it (for the functions defined there) so we have to do it
as well.
</p>
<dl>
<dt><a name="index-strtol"></a><u>Function:</u> long int <b>strtol</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>strtol</code> (&ldquo;string-to-long&rdquo;) function converts the initial
part of <var>string</var> to a signed integer, which is returned as a value
of type <code>long int</code>.
</p>
<p>This function attempts to decompose <var>string</var> as follows:
</p>
<ul>
<li>
A (possibly empty) sequence of whitespace characters.  Which characters
are whitespace is determined by the <code>isspace</code> function
(see section <a href="libc_4.html#Classification-of-Characters">Classification of Characters</a>).  These are discarded.

</li><li>
An optional plus or minus sign (&lsquo;<samp>+</samp>&rsquo; or &lsquo;<samp>-</samp>&rsquo;).

</li><li>
A nonempty sequence of digits in the radix specified by <var>base</var>.

<p>If <var>base</var> is zero, decimal radix is assumed unless the series of
digits begins with &lsquo;<samp>0</samp>&rsquo; (specifying octal radix), or &lsquo;<samp>0x</samp>&rsquo; or
&lsquo;<samp>0X</samp>&rsquo; (specifying hexadecimal radix); in other words, the same
syntax used for integer constants in C.
</p>
<p>Otherwise <var>base</var> must have a value between <code>2</code> and <code>36</code>.
If <var>base</var> is <code>16</code>, the digits may optionally be preceded by
&lsquo;<samp>0x</samp>&rsquo; or &lsquo;<samp>0X</samp>&rsquo;.  If base has no legal value the value returned
is <code>0l</code> and the global variable <code>errno</code> is set to <code>EINVAL</code>.
</p>
</li><li>
Any remaining characters in the string.  If <var>tailptr</var> is not a null
pointer, <code>strtol</code> stores a pointer to this tail in
<code>*<var>tailptr</var></code>.
</li></ul>

<p>If the string is empty, contains only whitespace, or does not contain an
initial substring that has the expected syntax for an integer in the
specified <var>base</var>, no conversion is performed.  In this case,
<code>strtol</code> returns a value of zero and the value stored in
<code>*<var>tailptr</var></code> is the value of <var>string</var>.
</p>
<p>In a locale other than the standard <code>&quot;C&quot;</code> locale, this function
may recognize additional implementation-dependent syntax.
</p>
<p>If the string has valid syntax for an integer but the value is not
representable because of overflow, <code>strtol</code> returns either
<code>LONG_MAX</code> or <code>LONG_MIN</code> (see section <a href="libc_34.html#Range-of-Type">Range of an Integer Type</a>), as
appropriate for the sign of the value.  It also sets <code>errno</code>
to <code>ERANGE</code> to indicate there was overflow.
</p>
<p>You should not check for errors by examining the return value of
<code>strtol</code>, because the string might be a valid representation of
<code>0l</code>, <code>LONG_MAX</code>, or <code>LONG_MIN</code>.  Instead, check whether
<var>tailptr</var> points to what you expect after the number
(e.g. <code>'\0'</code> if the string should end after the number).  You also
need to clear <var>errno</var> before the call and check it afterward, in
case there was overflow.
</p>
<p>There is an example at the end of this section.
</p></dd></dl>

<dl>
<dt><a name="index-wcstol"></a><u>Function:</u> long int <b>wcstol</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstol</code> function is equivalent to the <code>strtol</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstol</code> function was introduced in Amendment 1 of ISO C90.
</p></dd></dl>

<dl>
<dt><a name="index-strtoul"></a><u>Function:</u> unsigned long int <b>strtoul</b><i> (const char *retrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>strtoul</code> (&ldquo;string-to-unsigned-long&rdquo;) function is like
<code>strtol</code> except it converts to an <code>unsigned long int</code> value.
The syntax is the same as described above for <code>strtol</code>.  The value
returned on overflow is <code>ULONG_MAX</code> (see section <a href="libc_34.html#Range-of-Type">Range of an Integer Type</a>).
</p>
<p>If <var>string</var> depicts a negative number, <code>strtoul</code> acts the same
as <var>strtol</var> but casts the result to an unsigned integer.  That means
for example that <code>strtoul</code> on <code>&quot;-1&quot;</code> returns <code>ULONG_MAX</code>
and an input more negative than <code>LONG_MIN</code> returns
(<code>ULONG_MAX</code> + 1) / 2.
</p>
<p><code>strtoul</code> sets <var>errno</var> to <code>EINVAL</code> if <var>base</var> is out of
range, or <code>ERANGE</code> on overflow.
</p></dd></dl>

<dl>
<dt><a name="index-wcstoul"></a><u>Function:</u> unsigned long int <b>wcstoul</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstoul</code> function is equivalent to the <code>strtoul</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstoul</code> function was introduced in Amendment 1 of ISO C90.
</p></dd></dl>

<dl>
<dt><a name="index-strtoll"></a><u>Function:</u> long long int <b>strtoll</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>strtoll</code> function is like <code>strtol</code> except that it returns
a <code>long long int</code> value, and accepts numbers with a correspondingly
larger range.
</p>
<p>If the string has valid syntax for an integer but the value is not
representable because of overflow, <code>strtoll</code> returns either
<code>LONG_LONG_MAX</code> or <code>LONG_LONG_MIN</code> (see section <a href="libc_34.html#Range-of-Type">Range of an Integer Type</a>), as
appropriate for the sign of the value.  It also sets <code>errno</code> to
<code>ERANGE</code> to indicate there was overflow.
</p>
<p>The <code>strtoll</code> function was introduced in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-wcstoll"></a><u>Function:</u> long long int <b>wcstoll</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstoll</code> function is equivalent to the <code>strtoll</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstoll</code> function was introduced in Amendment 1 of ISO C90.
</p></dd></dl>

<dl>
<dt><a name="index-strtoq"></a><u>Function:</u> long long int <b>strtoq</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p><code>strtoq</code> (&ldquo;string-to-quad-word&rdquo;) is the BSD name for <code>strtoll</code>.
</p></dd></dl>

<dl>
<dt><a name="index-wcstoq"></a><u>Function:</u> long long int <b>wcstoq</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstoq</code> function is equivalent to the <code>strtoq</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstoq</code> function is a GNU extension.
</p></dd></dl>

<dl>
<dt><a name="index-strtoull"></a><u>Function:</u> unsigned long long int <b>strtoull</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>strtoull</code> function is related to <code>strtoll</code> the same way
<code>strtoul</code> is related to <code>strtol</code>.
</p>
<p>The <code>strtoull</code> function was introduced in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-wcstoull"></a><u>Function:</u> unsigned long long int <b>wcstoull</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstoull</code> function is equivalent to the <code>strtoull</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstoull</code> function was introduced in Amendment 1 of ISO C90.
</p></dd></dl>

<dl>
<dt><a name="index-strtouq"></a><u>Function:</u> unsigned long long int <b>strtouq</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p><code>strtouq</code> is the BSD name for <code>strtoull</code>.
</p></dd></dl>

<dl>
<dt><a name="index-wcstouq"></a><u>Function:</u> unsigned long long int <b>wcstouq</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstouq</code> function is equivalent to the <code>strtouq</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstouq</code> function is a GNU extension.
</p></dd></dl>

<dl>
<dt><a name="index-strtoimax"></a><u>Function:</u> intmax_t <b>strtoimax</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>strtoimax</code> function is like <code>strtol</code> except that it returns
a <code>intmax_t</code> value, and accepts numbers of a corresponding range.
</p>
<p>If the string has valid syntax for an integer but the value is not
representable because of overflow, <code>strtoimax</code> returns either
<code>INTMAX_MAX</code> or <code>INTMAX_MIN</code> (see section <a href="#Integers">Integers</a>), as
appropriate for the sign of the value.  It also sets <code>errno</code> to
<code>ERANGE</code> to indicate there was overflow.
</p>
<p>See <a href="#Integers">Integers</a> for a description of the <code>intmax_t</code> type.  The
<code>strtoimax</code> function was introduced in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-wcstoimax"></a><u>Function:</u> intmax_t <b>wcstoimax</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstoimax</code> function is equivalent to the <code>strtoimax</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstoimax</code> function was introduced in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-strtoumax"></a><u>Function:</u> uintmax_t <b>strtoumax</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>strtoumax</code> function is related to <code>strtoimax</code>
the same way that <code>strtoul</code> is related to <code>strtol</code>.
</p>
<p>See <a href="#Integers">Integers</a> for a description of the <code>intmax_t</code> type.  The
<code>strtoumax</code> function was introduced in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-wcstoumax"></a><u>Function:</u> uintmax_t <b>wcstoumax</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>, int <var>base</var>)</i></dt>
<dd><p>The <code>wcstoumax</code> function is equivalent to the <code>strtoumax</code> function
in nearly all aspects but handles wide character strings.
</p>
<p>The <code>wcstoumax</code> function was introduced in ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-atol"></a><u>Function:</u> long int <b>atol</b><i> (const char *<var>string</var>)</i></dt>
<dd><p>This function is similar to the <code>strtol</code> function with a <var>base</var>
argument of <code>10</code>, except that it need not detect overflow errors.
The <code>atol</code> function is provided mostly for compatibility with
existing code; using <code>strtol</code> is more robust.
</p></dd></dl>

<dl>
<dt><a name="index-atoi"></a><u>Function:</u> int <b>atoi</b><i> (const char *<var>string</var>)</i></dt>
<dd><p>This function is like <code>atol</code>, except that it returns an <code>int</code>.
The <code>atoi</code> function is also considered obsolete; use <code>strtol</code>
instead.
</p></dd></dl>

<dl>
<dt><a name="index-atoll"></a><u>Function:</u> long long int <b>atoll</b><i> (const char *<var>string</var>)</i></dt>
<dd><p>This function is similar to <code>atol</code>, except it returns a <code>long
long int</code>.
</p>
<p>The <code>atoll</code> function was introduced in ISO C99.  It too is
obsolete (despite having just been added); use <code>strtoll</code> instead.
</p></dd></dl>

<p>All the functions mentioned in this section so far do not handle
alternative representations of characters as described in the locale
data.  Some locales specify thousands separator and the way they have to
be used which can help to make large numbers more readable.  To read
such numbers one has to use the <code>scanf</code> functions with the &lsquo;<samp>'</samp>&rsquo;
flag.
</p>
<p>Here is a function which parses a string as a sequence of integers and
returns the sum of them:
</p>
<table><tr><td>&nbsp;</td><td><pre class="smallexample">int
sum_ints_from_string (char *string)
{
  int sum = 0;

  while (1) {
    char *tail;
    int next;

    /* <span class="roman">Skip whitespace by hand, to detect the end.</span>  */
    while (isspace (*string)) string++;
    if (*string == 0)
      break;

    /* <span class="roman">There is more nonwhitespace,</span>  */
    /* <span class="roman">so it ought to be another number.</span>  */
    errno = 0;
    /* <span class="roman">Parse it.</span>  */
    next = strtol (string, &amp;tail, 0);
    /* <span class="roman">Add it in, if not overflow.</span>  */
    if (errno)
      printf (&quot;Overflow\n&quot;);
    else
      sum += next;
    /* <span class="roman">Advance past it.</span>  */
    string = tail;
  }

  return sum;
}
</pre></td></tr></table>

<hr size="6">
<a name="Parsing-of-Floats"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Parsing-of-Integers" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#System-V-Number-Conversion" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Parsing-of-Numbers" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-of-Floats-1"></a>
<h3 class="subsection">20.11.2 Parsing of Floats</h3>

<a name="index-stdlib_002eh-18"></a>
<p>The &lsquo;<samp>str</samp>&rsquo; functions are declared in &lsquo;<tt>stdlib.h</tt>&rsquo; and those
beginning with &lsquo;<samp>wcs</samp>&rsquo; are declared in &lsquo;<tt>wchar.h</tt>&rsquo;.  One might
wonder about the use of <code>restrict</code> in the prototypes of the
functions in this section.  It is seemingly useless but the ISO C
standard uses it (for the functions defined there) so we have to do it
as well.
</p>
<dl>
<dt><a name="index-strtod"></a><u>Function:</u> double <b>strtod</b><i> (const char *restrict <var>string</var>, char **restrict <var>tailptr</var>)</i></dt>
<dd><p>The <code>strtod</code> (&ldquo;string-to-double&rdquo;) function converts the initial
part of <var>string</var> to a floating-point number, which is returned as a
value of type <code>double</code>.
</p>
<p>This function attempts to decompose <var>string</var> as follows:
</p>
<ul>
<li>
A (possibly empty) sequence of whitespace characters.  Which characters
are whitespace is determined by the <code>isspace</code> function
(see section <a href="libc_4.html#Classification-of-Characters">Classification of Characters</a>).  These are discarded.

</li><li>
An optional plus or minus sign (&lsquo;<samp>+</samp>&rsquo; or &lsquo;<samp>-</samp>&rsquo;).

</li><li> A floating point number in decimal or hexadecimal format.  The
decimal format is:
<ul class="toc">
<li>-
A nonempty sequence of digits optionally containing a decimal-point
character&mdash;normally &lsquo;<samp>.</samp>&rsquo;, but it depends on the locale
(see section <a href="libc_7.html#General-Numeric">Generic Numeric Formatting Parameters</a>).

</li><li>-
An optional exponent part, consisting of a character &lsquo;<samp>e</samp>&rsquo; or
&lsquo;<samp>E</samp>&rsquo;, an optional sign, and a sequence of digits.

</li></ul>

<p>The hexadecimal format is as follows:
</p><ul class="toc">
<li>-
A 0x or 0X followed by a nonempty sequence of hexadecimal digits
optionally containing a decimal-point character&mdash;normally &lsquo;<samp>.</samp>&rsquo;, but
it depends on the locale (see section <a href="libc_7.html#General-Numeric">Generic Numeric Formatting Parameters</a>).

</li><li>-
An optional binary-exponent part, consisting of a character &lsquo;<samp>p</samp>&rsquo; or
&lsquo;<samp>P</samp>&rsquo;, an optional sign, and a sequence of digits.

</li></ul>

</li><li>
Any remaining characters in the string.  If <var>tailptr</var> is not a null
pointer, a pointer to this tail of the string is stored in
<code>*<var>tailptr</var></code>.
</li></ul>

<p>If the string is empty, contains only whitespace, or does not contain an
initial substring that has the expected syntax for a floating-point
number, no conversion is performed.  In this case, <code>strtod</code> returns
a value of zero and the value returned in <code>*<var>tailptr</var></code> is the
value of <var>string</var>.
</p>
<p>In a locale other than the standard <code>&quot;C&quot;</code> or <code>&quot;POSIX&quot;</code> locales,
this function may recognize additional locale-dependent syntax.
</p>
<p>If the string has valid syntax for a floating-point number but the value
is outside the range of a <code>double</code>, <code>strtod</code> will signal
overflow or underflow as described in <a href="#Math-Error-Reporting">Error Reporting by Mathematical Functions</a>.
</p>
<p><code>strtod</code> recognizes four special input strings.  The strings
<code>&quot;inf&quot;</code> and <code>&quot;infinity&quot;</code> are converted to <em>&amp;infin;</em>,
or to the largest representable value if the floating-point format
doesn&rsquo;t support infinities.  You can prepend a <code>&quot;+&quot;</code> or <code>&quot;-&quot;</code>
to specify the sign.  Case is ignored when scanning these strings.
</p>
<p>The strings <code>&quot;nan&quot;</code> and <code>&quot;nan(<var>chars&hellip;</var>)&quot;</code> are converted
to NaN.  Again, case is ignored.  If <var>chars&hellip;</var> are provided, they
are used in some unspecified fashion to select a particular
representation of NaN (there can be several).
</p>
<p>Since zero is a valid result as well as the value returned on error, you
should check for errors in the same way as for <code>strtol</code>, by
examining <var>errno</var> and <var>tailptr</var>.
</p></dd></dl>

<dl>
<dt><a name="index-strtof"></a><u>Function:</u> float <b>strtof</b><i> (const char *<var>string</var>, char **<var>tailptr</var>)</i></dt>
<dt><a name="index-strtold"></a><u>Function:</u> long double <b>strtold</b><i> (const char *<var>string</var>, char **<var>tailptr</var>)</i></dt>
<dd><p>These functions are analogous to <code>strtod</code>, but return <code>float</code>
and <code>long double</code> values respectively.  They report errors in the
same way as <code>strtod</code>.  <code>strtof</code> can be substantially faster
than <code>strtod</code>, but has less precision; conversely, <code>strtold</code>
can be much slower but has more precision (on systems where <code>long
double</code> is a separate type).
</p>
<p>These functions have been GNU extensions and are new to ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-wcstod"></a><u>Function:</u> double <b>wcstod</b><i> (const wchar_t *restrict <var>string</var>, wchar_t **restrict <var>tailptr</var>)</i></dt>
<dt><a name="index-wcstof"></a><u>Function:</u> float <b>wcstof</b><i> (const wchar_t *<var>string</var>, wchar_t **<var>tailptr</var>)</i></dt>
<dt><a name="index-wcstold"></a><u>Function:</u> long double <b>wcstold</b><i> (const wchar_t *<var>string</var>, wchar_t **<var>tailptr</var>)</i></dt>
<dd><p>The <code>wcstod</code>, <code>wcstof</code>, and <code>wcstol</code> functions are
equivalent in nearly all aspect to the <code>strtod</code>, <code>strtof</code>, and
<code>strtold</code> functions but it handles wide character string.
</p>
<p>The <code>wcstod</code> function was introduced in Amendment 1 of ISO
C90.  The <code>wcstof</code> and <code>wcstold</code> functions were introduced in
ISO C99.
</p></dd></dl>

<dl>
<dt><a name="index-atof"></a><u>Function:</u> double <b>atof</b><i> (const char *<var>string</var>)</i></dt>
<dd><p>This function is similar to the <code>strtod</code> function, except that it
need not detect overflow and underflow errors.  The <code>atof</code> function
is provided mostly for compatibility with existing code; using
<code>strtod</code> is more robust.
</p></dd></dl>

<p>The GNU C library also provides &lsquo;<samp>_l</samp>&rsquo; versions of these functions,
which take an additional argument, the locale to use in conversion.
See section <a href="#Parsing-of-Integers">Parsing of Integers</a>.
</p>
<hr size="6">
<a name="System-V-Number-Conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Parsing-of-Floats" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Arithmetic" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Old_002dfashioned-System-V-number_002dto_002dstring-functions"></a>
<h2 class="section">20.12 Old-fashioned System V number-to-string functions</h2>

<p>The old System V C library provided three functions to convert
numbers to strings, with unusual and hard-to-use semantics.  The GNU C
library also provides these functions and some natural extensions.
</p>
<p>These functions are only available in glibc and on systems descended
from AT&amp;T Unix.  Therefore, unless these functions do precisely what you
need, it is better to use <code>sprintf</code>, which is standard.
</p>
<p>All these functions are defined in &lsquo;<tt>stdlib.h</tt>&rsquo;.
</p>
<dl>
<dt><a name="index-ecvt"></a><u>Function:</u> char * <b>ecvt</b><i> (double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>)</i></dt>
<dd><p>The function <code>ecvt</code> converts the floating-point number <var>value</var>
to a string with at most <var>ndigit</var> decimal digits.  The
returned string contains no decimal point or sign. The first digit of
the string is non-zero (unless <var>value</var> is actually zero) and the
last digit is rounded to nearest.  <code>*<var>decpt</var></code> is set to the
index in the string of the first digit after the decimal point.
<code>*<var>neg</var></code> is set to a nonzero value if <var>value</var> is negative,
zero otherwise.
</p>
<p>If <var>ndigit</var> decimal digits would exceed the precision of a
<code>double</code> it is reduced to a system-specific value.
</p>
<p>The returned string is statically allocated and overwritten by each call
to <code>ecvt</code>.
</p>
<p>If <var>value</var> is zero, it is implementation defined whether
<code>*<var>decpt</var></code> is <code>0</code> or <code>1</code>.
</p>
<p>For example: <code>ecvt (12.3, 5, &amp;d, &amp;n)</code> returns <code>&quot;12300&quot;</code>
and sets <var>d</var> to <code>2</code> and <var>n</var> to <code>0</code>.
</p></dd></dl>

<dl>
<dt><a name="index-fcvt"></a><u>Function:</u> char * <b>fcvt</b><i> (double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>)</i></dt>
<dd><p>The function <code>fcvt</code> is like <code>ecvt</code>, but <var>ndigit</var> specifies
the number of digits after the decimal point.  If <var>ndigit</var> is less
than zero, <var>value</var> is rounded to the <em><var>ndigit</var>+1</em>&rsquo;th place to the
left of the decimal point.  For example, if <var>ndigit</var> is <code>-1</code>,
<var>value</var> will be rounded to the nearest 10.  If <var>ndigit</var> is
negative and larger than the number of digits to the left of the decimal
point in <var>value</var>, <var>value</var> will be rounded to one significant digit.
</p>
<p>If <var>ndigit</var> decimal digits would exceed the precision of a
<code>double</code> it is reduced to a system-specific value.
</p>
<p>The returned string is statically allocated and overwritten by each call
to <code>fcvt</code>.
</p></dd></dl>

<dl>
<dt><a name="index-gcvt"></a><u>Function:</u> char * <b>gcvt</b><i> (double <var>value</var>, int <var>ndigit</var>, char *<var>buf</var>)</i></dt>
<dd><p><code>gcvt</code> is functionally equivalent to &lsquo;<samp>sprintf(buf, &quot;%*g&quot;,
ndigit, value</samp>&rsquo;.  It is provided only for compatibility&rsquo;s sake.  It
returns <var>buf</var>.
</p>
<p>If <var>ndigit</var> decimal digits would exceed the precision of a
<code>double</code> it is reduced to a system-specific value.
</p></dd></dl>

<p>As extensions, the GNU C library provides versions of these three
functions that take <code>long double</code> arguments.
</p>
<dl>
<dt><a name="index-qecvt"></a><u>Function:</u> char * <b>qecvt</b><i> (long double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>)</i></dt>
<dd><p>This function is equivalent to <code>ecvt</code> except that it takes a
<code>long double</code> for the first parameter and that <var>ndigit</var> is
restricted by the precision of a <code>long double</code>.
</p></dd></dl>

<dl>
<dt><a name="index-qfcvt"></a><u>Function:</u> char * <b>qfcvt</b><i> (long double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>)</i></dt>
<dd><p>This function is equivalent to <code>fcvt</code> except that it
takes a <code>long double</code> for the first parameter and that <var>ndigit</var> is
restricted by the precision of a <code>long double</code>.
</p></dd></dl>

<dl>
<dt><a name="index-qgcvt"></a><u>Function:</u> char * <b>qgcvt</b><i> (long double <var>value</var>, int <var>ndigit</var>, char *<var>buf</var>)</i></dt>
<dd><p>This function is equivalent to <code>gcvt</code> except that it takes a
<code>long double</code> for the first parameter and that <var>ndigit</var> is
restricted by the precision of a <code>long double</code>.
</p></dd></dl>


<a name="index-gcvt_005fr"></a>
<p>The <code>ecvt</code> and <code>fcvt</code> functions, and their <code>long double</code>
equivalents, all return a string located in a static buffer which is
overwritten by the next call to the function.  The GNU C library
provides another set of extended functions which write the converted
string into a user-supplied buffer.  These have the conventional
<code>_r</code> suffix.
</p>
<p><code>gcvt_r</code> is not necessary, because <code>gcvt</code> already uses a
user-supplied buffer.
</p>
<dl>
<dt><a name="index-ecvt_005fr"></a><u>Function:</u> int <b>ecvt_r</b><i> (double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>, char *<var>buf</var>, size_t <var>len</var>)</i></dt>
<dd><p>The <code>ecvt_r</code> function is the same as <code>ecvt</code>, except
that it places its result into the user-specified buffer pointed to by
<var>buf</var>, with length <var>len</var>.  The return value is <code>-1</code> in
case of an error and zero otherwise.
</p>
<p>This function is a GNU extension.
</p></dd></dl>

<dl>
<dt><a name="index-fcvt_005fr"></a><u>Function:</u> int <b>fcvt_r</b><i> (double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>, char *<var>buf</var>, size_t <var>len</var>)</i></dt>
<dd><p>The <code>fcvt_r</code> function is the same as <code>fcvt</code>, except that it
places its result into the user-specified buffer pointed to by
<var>buf</var>, with length <var>len</var>.  The return value is <code>-1</code> in
case of an error and zero otherwise.
</p>
<p>This function is a GNU extension.
</p></dd></dl>

<dl>
<dt><a name="index-qecvt_005fr"></a><u>Function:</u> int <b>qecvt_r</b><i> (long double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>, char *<var>buf</var>, size_t <var>len</var>)</i></dt>
<dd><p>The <code>qecvt_r</code> function is the same as <code>qecvt</code>, except
that it places its result into the user-specified buffer pointed to by
<var>buf</var>, with length <var>len</var>.  The return value is <code>-1</code> in
case of an error and zero otherwise.
</p>
<p>This function is a GNU extension.
</p></dd></dl>

<dl>
<dt><a name="index-qfcvt_005fr"></a><u>Function:</u> int <b>qfcvt_r</b><i> (long double <var>value</var>, int <var>ndigit</var>, int *<var>decpt</var>, int *<var>neg</var>, char *<var>buf</var>, size_t <var>len</var>)</i></dt>
<dd><p>The <code>qfcvt_r</code> function is the same as <code>qfcvt</code>, except
that it places its result into the user-specified buffer pointed to by
<var>buf</var>, with length <var>len</var>.  The return value is <code>-1</code> in
case of an error and zero otherwise.
</p>
<p>This function is a GNU extension.
</p></dd></dl>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Arithmetic" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="libc_21.html#Date-and-Time" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
 <font size="-1">
  This document was generated by <em>root</em> on <em>April 20, 2012</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
 </font>
 <br>

</p>
</body>
</html>