From 9ce91ffc87894318edb89295225e334269bc8922 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 24 Nov 2014 18:24:19 +0000 Subject: [PATCH 1/4] fix bigfloat itrunc error --- base/mpfr.jl | 2 +- base/random.jl | 248 ++++++++++++++++++++++++------------------------- test/mpfr.jl | 2 +- 3 files changed, 126 insertions(+), 126 deletions(-) diff --git a/base/mpfr.jl b/base/mpfr.jl index eb6a03f410672..95b5b71bc8daa 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -655,7 +655,7 @@ end function itrunc(x::BigFloat) z = BigInt() - ccall((:mpfr_get_z, :libmpfr), Int32, (Ptr{BigInt}, Ptr{BigFloat}, Int32), &z, &x, 0) + ccall((:mpfr_get_z, :libmpfr), Int32, (Ptr{BigInt}, Ptr{BigFloat}, Int32), &z, &x, to_mpfr(RoundToZero)) return z end diff --git a/base/random.jl b/base/random.jl index 44911d0b56f06..ef3b7bb28f93a 100644 --- a/base/random.jl +++ b/base/random.jl @@ -457,70 +457,70 @@ rand(r::MersenneTwister, ::Type{Bool}) = randbool(r) # randmtzig (covers also exponential variates) ## Tables for normal variates const ki = - UInt64[0x0007799ec012f7b3, 0,0x0006045f4c7de363,0x0006d1aa7d5ec0a6, - 0x000728fb3f60f778,0x0007592af4e9fbc0,0x000777a5c0bf655d,0x00078ca3857d2256, - 0x00079bf6b0ffe58c,0x0007a7a34ab092ae,0x0007b0d2f20dd1cb,0x0007b83d3aa9cb52, - 0x0007be597614224e,0x0007c3788631abea,0x0007c7d32bc192ef,0x0007cb9263a6e86d, + UInt64[0x0007799ec012f7b2,0x0000000000000000,0x0006045f4c7de363,0x0006d1aa7d5ec0a5, + 0x000728fb3f60f777,0x0007592af4e9fbc0,0x000777a5c0bf655d,0x00078ca3857d2256, + 0x00079bf6b0ffe58b,0x0007a7a34ab092ad,0x0007b0d2f20dd1cb,0x0007b83d3aa9cb52, + 0x0007be597614224d,0x0007c3788631abe9,0x0007c7d32bc192ee,0x0007cb9263a6e86d, 0x0007ced483edfa84,0x0007d1b07ac0fd39,0x0007d437ef2da5fc,0x0007d678b069aa6e, - 0x0007d87db38c5c87,0x0007da4fc6a9ba63,0x0007dbf611b37f3c,0x0007dd7674d0f286, - 0x0007ded5ce8205f7,0x0007e018307fb62c,0x0007e141081bd124,0x0007e2533d712de9, - 0x0007e3514bbd7718,0x0007e43d54944b52,0x0007e5192f25ef43,0x0007e5e67481118d, - 0x0007e6a6897c1ce2,0x0007e75aa6c7f64d,0x0007e803df8ee498,0x0007e8a326eb6273, - 0x0007e93954717a28,0x0007e9c727f8648f,0x0007ea4d4cc85a3d,0x0007eacc5c4907a9, - 0x0007eb44e0474cf6,0x0007ebb754e47419,0x0007ec242a3d8474,0x0007ec8bc5d69646, - 0x0007ecee83d3d6e9,0x0007ed4cb8082f46,0x0007eda6aee01710,0x0007edfcae2dfe68, - 0x0007ee4ef5dccd3f,0x0007ee9dc08c394f,0x0007eee9441a17c8,0x0007ef31b21b4fb2, - 0x0007ef773846a8a7,0x0007efba00d35a17,0x0007effa32ccf69f,0x0007f037f25e1279, - 0x0007f0736112d12c,0x0007f0ac9e145c26,0x0007f0e3c65e1fcc,0x0007f118f4ed8e55, - 0x0007f14c42ed0dc9,0x0007f17dc7daa0c3,0x0007f1ad99aac6a5,0x0007f1dbcce80015, - 0x0007f20874cf56bf,0x0007f233a36a3b9a,0x0007f25d69a604ae,0x0007f285d7694a92, - 0x0007f2acfba75e3c,0x0007f2d2e4720909,0x0007f2f79f09c345,0x0007f31b37ec883c, - 0x0007f33dbae36abc,0x0007f35f330f08d5,0x0007f37faaf2fa79,0x0007f39f2c805381, - 0x0007f3bdc11f4f1d,0x0007f3db71b83851,0x0007f3f846bba121,0x0007f4144829f846, - 0x0007f42f7d9a8b9e,0x0007f449ee420432,0x0007f463a0f8675e,0x0007f47c9c3ea77b, - 0x0007f494e643cd8e,0x0007f4ac84e9c475,0x0007f4c37dc9cd51,0x0007f4d9d638a433, - 0x0007f4ef934a5b6b,0x0007f504b9d5f33e,0x0007f5194e78b352,0x0007f52d55994a96, - 0x0007f540d36aba0d,0x0007f553cbef0e78,0x0007f56642f9ec90,0x0007f5783c32f31e, - 0x0007f589bb17f609,0x0007f59ac2ff1525,0x0007f5ab5718b15a,0x0007f5bb7a71427d, - 0x0007f5cb2ff3100a,0x0007f5da7a67cebe,0x0007f5e95c7a24e8,0x0007f5f7d8b7171f, - 0x0007f605f18f5ef4,0x0007f613a958ad0a,0x0007f621024ed7ea,0x0007f62dfe94f8cb, + 0x0007d87db38c5c87,0x0007da4fc6a9ba62,0x0007dbf611b37f3b,0x0007dd7674d0f286, + 0x0007ded5ce8205f6,0x0007e018307fb62b,0x0007e141081bd124,0x0007e2533d712de8, + 0x0007e3514bbd7718,0x0007e43d54944b52,0x0007e5192f25ef42,0x0007e5e67481118d, + 0x0007e6a6897c1ce2,0x0007e75aa6c7f64c,0x0007e803df8ee498,0x0007e8a326eb6272, + 0x0007e93954717a28,0x0007e9c727f8648f,0x0007ea4d4cc85a3c,0x0007eacc5c4907a9, + 0x0007eb44e0474cf6,0x0007ebb754e47419,0x0007ec242a3d8474,0x0007ec8bc5d69645, + 0x0007ecee83d3d6e9,0x0007ed4cb8082f45,0x0007eda6aee0170f,0x0007edfcae2dfe68, + 0x0007ee4ef5dccd3e,0x0007ee9dc08c394e,0x0007eee9441a17c7,0x0007ef31b21b4fb1, + 0x0007ef773846a8a7,0x0007efba00d35a17,0x0007effa32ccf69f,0x0007f037f25e1278, + 0x0007f0736112d12c,0x0007f0ac9e145c25,0x0007f0e3c65e1fcc,0x0007f118f4ed8e54, + 0x0007f14c42ed0dc8,0x0007f17dc7daa0c3,0x0007f1ad99aac6a5,0x0007f1dbcce80015, + 0x0007f20874cf56bf,0x0007f233a36a3b9a,0x0007f25d69a604ad,0x0007f285d7694a92, + 0x0007f2acfba75e3b,0x0007f2d2e4720909,0x0007f2f79f09c344,0x0007f31b37ec883b, + 0x0007f33dbae36abc,0x0007f35f330f08d5,0x0007f37faaf2fa79,0x0007f39f2c805380, + 0x0007f3bdc11f4f1c,0x0007f3db71b83850,0x0007f3f846bba121,0x0007f4144829f846, + 0x0007f42f7d9a8b9d,0x0007f449ee420432,0x0007f463a0f8675e,0x0007f47c9c3ea77b, + 0x0007f494e643cd8e,0x0007f4ac84e9c475,0x0007f4c37dc9cd50,0x0007f4d9d638a432, + 0x0007f4ef934a5b6a,0x0007f504b9d5f33d,0x0007f5194e78b352,0x0007f52d55994a96, + 0x0007f540d36aba0c,0x0007f553cbef0e77,0x0007f56642f9ec8f,0x0007f5783c32f31e, + 0x0007f589bb17f609,0x0007f59ac2ff1525,0x0007f5ab5718b15a,0x0007f5bb7a71427c, + 0x0007f5cb2ff31009,0x0007f5da7a67cebe,0x0007f5e95c7a24e7,0x0007f5f7d8b7171e, + 0x0007f605f18f5ef4,0x0007f613a958ad0a,0x0007f621024ed7e9,0x0007f62dfe94f8cb, 0x0007f63aa036777a,0x0007f646e928065a,0x0007f652db488f88,0x0007f65e786213ff, - 0x0007f669c22a7d8b,0x0007f674ba44645a,0x0007f67f623fc8dc,0x0007f689bb9ac294, - 0x0007f693c7c22482,0x0007f69d881217a6,0x0007f6a6fdd6ac37,0x0007f6b02a4c61ee, - 0x0007f6b90ea0a7f4,0x0007f6c1abf254c1,0x0007f6ca03521664,0x0007f6d215c2db82, - 0x0007f6d9e43a355a,0x0007f6e16fa0b329,0x0007f6e8b8d23729,0x0007f6efc09e4569, - 0x0007f6f687c84cc0,0x0007f6fd0f07ea0a,0x0007f703570925e2,0x0007f709606cad03, - 0x0007f70f2bc80370,0x0007f714b9a5b292,0x0007f71a0a85725e,0x0007f71f1edc4d9e, - 0x0007f723f714c17a,0x0007f728938ed843,0x0007f72cf4a03fa1,0x0007f7311a945a17, - 0x0007f73505ac4bf8,0x0007f738b61f03bd,0x0007f73c2c193dc1,0x0007f73f67bd835d, - 0x0007f74269242559,0x0007f745305b31a1,0x0007f747bd666429,0x0007f74a103f12ed, - 0x0007f74c28d414f6,0x0007f74e0709a42e,0x0007f74faab939fa,0x0007f75113b16657, - 0x0007f75241b5a156,0x0007f753347e16b9,0x0007f753ebb76b7d,0x0007f75467027d05, - 0x0007f754a5f4199e,0x0007f754a814b208,0x0007f7546ce003af,0x0007f753f3c4bb2a, - 0x0007f7533c240e92,0x0007f75245514f41,0x0007f7510e91726d,0x0007f74f971a9012, - 0x0007f74dde135797,0x0007f74be2927972,0x0007f749a39e051d,0x0007f747202aba8b, - 0x0007f744571b4e3d,0x0007f741473f9efe,0x0007f73def53dc44,0x0007f73a4dff9c00, - 0x0007f73661d4deaf,0x0007f732294f0040,0x0007f72da2d19444,0x0007f728cca72bdb, - 0x0007f723a5000367,0x0007f71e29f09628,0x0007f7185970156c,0x0007f7123156c102, - 0x0007f70baf5c1e2d,0x0007f704d1150a24,0x0007f6fd93f1a4e6,0x0007f6f5f53b10b6, - 0x0007f6edf211023f,0x0007f6e587671cea,0x0007f6dcb202167a,0x0007f6d36e749c65, - 0x0007f6c9b91bf4c6,0x0007f6bf8e1c541b,0x0007f6b4e95ce015,0x0007f6a9c6835700, - 0x0007f69e20ef5211,0x0007f691f3b517ec,0x0007f6853997f322,0x0007f677ed03ff1a, + 0x0007f669c22a7d8a,0x0007f674ba446459,0x0007f67f623fc8db,0x0007f689bb9ac294, + 0x0007f693c7c22481,0x0007f69d881217a6,0x0007f6a6fdd6ac36,0x0007f6b02a4c61ee, + 0x0007f6b90ea0a7f4,0x0007f6c1abf254c0,0x0007f6ca03521664,0x0007f6d215c2db82, + 0x0007f6d9e43a3559,0x0007f6e16fa0b329,0x0007f6e8b8d23729,0x0007f6efc09e4569, + 0x0007f6f687c84cbf,0x0007f6fd0f07ea09,0x0007f703570925e2,0x0007f709606cad03, + 0x0007f70f2bc8036f,0x0007f714b9a5b292,0x0007f71a0a85725d,0x0007f71f1edc4d9e, + 0x0007f723f714c179,0x0007f728938ed843,0x0007f72cf4a03fa0,0x0007f7311a945a16, + 0x0007f73505ac4bf8,0x0007f738b61f03bd,0x0007f73c2c193dc0,0x0007f73f67bd835c, + 0x0007f74269242559,0x0007f745305b31a1,0x0007f747bd666428,0x0007f74a103f12ed, + 0x0007f74c28d414f5,0x0007f74e0709a42d,0x0007f74faab939f9,0x0007f75113b16657, + 0x0007f75241b5a155,0x0007f753347e16b8,0x0007f753ebb76b7c,0x0007f75467027d05, + 0x0007f754a5f4199d,0x0007f754a814b207,0x0007f7546ce003ae,0x0007f753f3c4bb29, + 0x0007f7533c240e92,0x0007f75245514f41,0x0007f7510e91726c,0x0007f74f971a9012, + 0x0007f74dde135797,0x0007f74be2927971,0x0007f749a39e051c,0x0007f747202aba8a, + 0x0007f744571b4e3c,0x0007f741473f9efe,0x0007f73def53dc43,0x0007f73a4dff9bff, + 0x0007f73661d4deaf,0x0007f732294f003f,0x0007f72da2d19444,0x0007f728cca72bda, + 0x0007f723a5000367,0x0007f71e29f09627,0x0007f7185970156b,0x0007f7123156c102, + 0x0007f70baf5c1e2c,0x0007f704d1150a23,0x0007f6fd93f1a4e5,0x0007f6f5f53b10b6, + 0x0007f6edf211023e,0x0007f6e587671ce9,0x0007f6dcb2021679,0x0007f6d36e749c64, + 0x0007f6c9b91bf4c6,0x0007f6bf8e1c541b,0x0007f6b4e95ce015,0x0007f6a9c68356ff, + 0x0007f69e20ef5211,0x0007f691f3b517eb,0x0007f6853997f321,0x0007f677ed03ff19, 0x0007f66a08075bdc,0x0007f65b844ab75a,0x0007f64c5b091860,0x0007f63c8506d4bc, - 0x0007f62bfa8798ff,0x0007f61ab34364b1,0x0007f608a65a599a,0x0007f5f5ca4737e8, - 0x0007f5e214d05b49,0x0007f5cd7af7066e,0x0007f5b7f0e4c2a1,0x0007f5a169d68fcf, - 0x0007f589d80596a6,0x0007f5712c8d0174,0x0007f557574c912b,0x0007f53c46c77193, - 0x0007f51fe7feb9f3,0x0007f5022646ecfb,0x0007f4e2eb17ab1d,0x0007f4c21dd4a3d2, + 0x0007f62bfa8798fe,0x0007f61ab34364b0,0x0007f608a65a599a,0x0007f5f5ca4737e8, + 0x0007f5e214d05b48,0x0007f5cd7af7066e,0x0007f5b7f0e4c2a1,0x0007f5a169d68fcf, + 0x0007f589d80596a5,0x0007f5712c8d0174,0x0007f557574c912b,0x0007f53c46c77193, + 0x0007f51fe7feb9f2,0x0007f5022646ecfb,0x0007f4e2eb17ab1d,0x0007f4c21dd4a3d1, 0x0007f49fa38ea394,0x0007f47b5ebb62eb,0x0007f4552ee27473,0x0007f42cf03d58f5, - 0x0007f4027b4854a0,0x0007f3d5a44119e0,0x0007f3a63a8fb553,0x0007f37408155101, - 0x0007f33ed05b55ec,0x0007f3064f9c183f,0x0007f2ca399c7ba1,0x0007f28a384bb940, - 0x0007f245ea1b7a2c,0x0007f1fcdffe8f1c,0x0007f1ae9af758ce,0x0007f15a8917f27f, - 0x0007f10001ccaaac,0x0007f09e413c418a,0x0007f034627733d8,0x0007efc15815b8d5, - 0x0007ef43e2bf7f55,0x0007eeba84e31dff,0x0007ee237294df89,0x0007ed7c7c170141, - 0x0007ecc2f0d95d3b,0x0007ebf377a46782,0x0007eb09d6deb286,0x0007ea00a4f17809, - 0x0007e8d0d3da63d6,0x0007e771023b0fcf,0x0007e5d46c2f08d9,0x0007e3e937669691, - 0x0007e195978f1176,0x0007deb2c0e05c1d,0x0007db0362002a1a,0x0007d6202c15143a, - 0x0007cf4b8f00a2cc,0x0007c4fd24520efe,0x0007b362fbf81816,0x00078d2d25998e25] + 0x0007f4027b48549f,0x0007f3d5a44119df,0x0007f3a63a8fb552,0x0007f37408155100, + 0x0007f33ed05b55ec,0x0007f3064f9c183e,0x0007f2ca399c7ba1,0x0007f28a384bb940, + 0x0007f245ea1b7a2b,0x0007f1fcdffe8f1b,0x0007f1ae9af758cd,0x0007f15a8917f27e, + 0x0007f10001ccaaab,0x0007f09e413c418a,0x0007f034627733d7,0x0007efc15815b8d5, + 0x0007ef43e2bf7f55,0x0007eeba84e31dfe,0x0007ee237294df89,0x0007ed7c7c170141, + 0x0007ecc2f0d95d3a,0x0007ebf377a46782,0x0007eb09d6deb285,0x0007ea00a4f17808, + 0x0007e8d0d3da63d6,0x0007e771023b0fcf,0x0007e5d46c2f08d8,0x0007e3e937669691, + 0x0007e195978f1176,0x0007deb2c0e05c1c,0x0007db0362002a19,0x0007d6202c151439, + 0x0007cf4b8f00a2cb,0x0007c4fd24520efd,0x0007b362fbf81816,0x00078d2d25998e24] const wi = [1.7367254121602630e-15,9.5586603514556339e-17,1.2708704834810623e-16, 1.4909740962495474e-16,1.6658733631586268e-16,1.8136120810119029e-16, @@ -698,70 +698,70 @@ const fi = ## Tables for exponential variates const ke = -UInt64[0x000e290a13924be4,0 ,0x0009beadebce18c0,0x000c377ac71f9e08, - 0x000d4ddb99075857,0x000de893fb8ca23e,0x000e4a8e87c4328e,0x000e8dff16ae1cba, - 0x000ebf2deab58c5a,0x000ee49a6e8b9639,0x000f0204efd64ee5,0x000f19bdb8ea3c1c, - 0x000f2d458bbe5bd2,0x000f3da104b78236,0x000f4b86d784571f,0x000f577ad8a7784f, - 0x000f61de83da32ac,0x000f6afb7843cce7,0x000f730a57372b44,0x000f7a37651b0e68, - 0x000f80a5bb6eea52,0x000f867189d3cb5c,0x000f8bb1b4f8fbbd,0x000f9079062292b9, - 0x000f94d70ca8d43a,0x000f98d8c7dcaa99,0x000f9c8928abe083,0x000f9ff175b734a6, - 0x000fa319996bc47e,0x000fa6085f8e9d08,0x000fa8c3a62e1991,0x000fab5084e1f660, - 0x000fadb36c84cccb,0x000faff041086846,0x000fb20a6ea22bb9,0x000fb404fb42cb3d, - 0x000fb5e295158174,0x000fb7a59e99727a,0x000fb95038c8789d,0x000fbae44ba684ec, - 0x000fbc638d822e60,0x000fbdcf89209ffb,0x000fbf29a303cfc5,0x000fc0731df1089d, - 0x000fc1ad1ed6c8b1,0x000fc2d8b02b5c8a,0x000fc3f6c4d92131,0x000fc5083ac9ba7d, - 0x000fc60ddd1e9cd7,0x000fc7086622e825,0x000fc7f881009f0c,0x000fc8decb41ac71, - 0x000fc9bbd623d7ec,0x000fca9027c5b26e,0x000fcb5c3c319c49,0x000fcc20864b4449, - 0x000fccdd70a35d41,0x000fcd935e34bf80,0x000fce42ab0db8bd,0x000fceebace7ec02, - 0x000fcf8eb3b0d0e7,0x000fd02c0a049b60,0x000fd0c3f59d199d,0x000fd156b7b5e27e, - 0x000fd1e48d670342,0x000fd26daff73552,0x000fd2f2552684bf,0x000fd372af7233c2, - 0x000fd3eeee528f62,0x000fd4673e73543b,0x000fd4dbc9e72ff8,0x000fd54cb856dc2c, - 0x000fd5ba2f2c4119,0x000fd62451ba02c3,0x000fd68b415fcff5,0x000fd6ef1dabc161, - 0x000fd75004790eb7,0x000fd7ae120c583f,0x000fd809612dbd0a,0x000fd8620b40effa, - 0x000fd8b8285b78fe,0x000fd90bcf594b1d,0x000fd95d15efd426,0x000fd9ac10bfa70c, - 0x000fd9f8d364df06,0x000fda437086566c,0x000fda8bf9e3c9ff,0x000fdad28062fed5, - 0x000fdb17141bff2d,0x000fdb59c4648085,0x000fdb9a9fda83cd,0x000fdbd9b46e3ed4, - 0x000fdc170f6b5d05,0x000fdc52bd81a3fb,0x000fdc8ccacd07ba,0x000fdcc542dd3902, - 0x000fdcfc30bcb794,0x000fdd319ef77143,0x000fdd6597a0f60c,0x000fdd98245a48a3, - 0x000fddc94e575271,0x000fddf91e64014f,0x000fde279ce914cb,0x000fde54d1f0a06b, - 0x000fde80c52a47d0,0x000fdeab7def394e,0x000fded50345eb36,0x000fdefd5be59fa1, - 0x000fdf248e39b26f,0x000fdf4aa064b4b0,0x000fdf6f98435894,0x000fdf937b6f30bb, - 0x000fdfb64f414572,0x000fdfd818d48262,0x000fdff8dd07fed8,0x000fe018a08122c5, - 0x000fe03767adaa5a,0x000fe05536c58a14,0x000fe07211ccb4c5,0x000fe08dfc94c532, - 0x000fe0a8fabe8ca2,0x000fe0c30fbb87a6,0x000fe0dc3ecf3a5a,0x000fe0f48b107522, - 0x000fe10bf76a82ef,0x000fe122869e4200,0x000fe1383b4327e1,0x000fe14d17c83188, - 0x000fe1611e74c023,0x000fe1745169635a,0x000fe186b2a09177,0x000fe19843ef4e08, - 0x000fe1a90705bf64,0x000fe1b8fd6fb37c,0x000fe1c828951444,0x000fe1d689ba4bfd, - 0x000fe1e4220099a5,0x000fe1f0f26655a0,0x000fe1fcfbc726d4,0x000fe2083edc2831, - 0x000fe212bc3bfeb4,0x000fe21c745adfe4,0x000fe225678a8895,0x000fe22d95fa23f4, - 0x000fe234ffb62282,0x000fe23ba4a800d9,0x000fe2418495fddd,0x000fe2469f22bffb, - 0x000fe24af3cce90e,0x000fe24e81ee9859,0x000fe25148bcda1a,0x000fe253474703fe, - 0x000fe2547c75fdc6,0x000fe254e70b7550,0x000fe25485a0fd1a,0x000fe25356a71450, - 0x000fe2515864173b,0x000fe24e88f316f2,0x000fe24ae64296fb,0x000fe2466e132f61, - 0x000fe2411df611bd,0x000fe23af34b6f73,0x000fe233eb40bf42,0x000fe22c02cee01c, - 0x000fe22336b81711,0x000fe2198385e5cd,0x000fe20ee586b707,0x000fe20358cb5dfc, - 0x000fe1f6d92465b1,0x000fe1e9621f2c9e,0x000fe1daef02c8da,0x000fe1cb7accb0a6, - 0x000fe1bb002d22ca,0x000fe1a9798349b9,0x000fe196e0d9140d,0x000fe1832fdebc44, - 0x000fe16e5fe5f932,0x000fe15869dccfcf,0x000fe1414647fe78,0x000fe128ed3cf8b2, - 0x000fe10f565b69cf,0x000fe0f478c633ab,0x000fe0d84b1bdd9e,0x000fe0bac36e6688, - 0x000fe09bd73a6b5c,0x000fe07b7b5d920b,0x000fe059a40c26d2,0x000fe03644c5d7f9, - 0x000fe011504979b3,0x000fdfeab887b95d,0x000fdfc26e94a448,0x000fdf986297e306, - 0x000fdf6c83bb8663,0x000fdf3ec0193eee,0x000fdf0f04a5d30a,0x000fdedd3d1aa204, - 0x000fdea953dcfc13,0x000fde7331e3100e,0x000fde3abe9626f3,0x000fddffdfb1dbd5, - 0x000fddc2791ff351,0x000fdd826cd068c7,0x000fdd3f9a8d3857,0x000fdcf9dfc95b0d, - 0x000fdcb1176a55fe,0x000fdc65198ba50c,0x000fdc15bb3b2daa,0x000fdbc2ce2dc4ae, - 0x000fdb6c206aaaca,0x000fdb117becb4a2,0x000fdab2a6379bf1,0x000fda4f5fdfb4e9, - 0x000fd9e76401f3a4,0x000fd97a67a9ce20,0x000fd9081922142a,0x000fd8901f2d4b02, - 0x000fd812182170e1,0x000fd78d98e23cd4,0x000fd7022bb3f083,0x000fd66f4edf96ba, - 0x000fd5d473200306,0x000fd530f9ccff94,0x000fd48432b7b351,0x000fd3cd59a8469f, - 0x000fd30b9368f90a,0x000fd23dea45f500,0x000fd16349e2e04b,0x000fd07a7a3ef98b, - 0x000fcf8219b5df06,0x000fce7895bcfcdf,0x000fcd5c220ad5e3,0x000fcc2aadbc17dd, - 0x000fcae1d5e81fbd,0x000fc97ed4e778fa,0x000fc7fe6d4d720f,0x000fc65ccf39c2fc, - 0x000fc4957623cb04,0x000fc2a2fc826dc8,0x000fc07ee19b01ce,0x000fbe213c1cf493, - 0x000fbb8051ac1566,0x000fb890078d120e,0x000fb5411a5b9a96,0x000fb18000547134, - 0x000fad334827f1e2,0x000fa839276708b9,0x000fa263b32e37ee,0x000f9b72d1c52cd1, - 0x000f930a1a281a05,0x000f889f023d820a,0x000f7b577d2be5f4,0x000f69c650c40a8f, - 0x000f51530f0916d9,0x000f2cb0e3c5933e,0x000eeefb15d605d9,0x000e6da6ecf27460] + UInt64[0x000e290a13924be3,0x0000000000000000,0x0009beadebce18bf,0x000c377ac71f9e08, + 0x000d4ddb99075857,0x000de893fb8ca23e,0x000e4a8e87c4328d,0x000e8dff16ae1cb9, + 0x000ebf2deab58c59,0x000ee49a6e8b9638,0x000f0204efd64ee4,0x000f19bdb8ea3c1b, + 0x000f2d458bbe5bd1,0x000f3da104b78236,0x000f4b86d784571f,0x000f577ad8a7784f, + 0x000f61de83da32ab,0x000f6afb7843cce7,0x000f730a57372b44,0x000f7a37651b0e68, + 0x000f80a5bb6eea52,0x000f867189d3cb5b,0x000f8bb1b4f8fbbd,0x000f9079062292b8, + 0x000f94d70ca8d43a,0x000f98d8c7dcaa99,0x000f9c8928abe083,0x000f9ff175b734a6, + 0x000fa319996bc47d,0x000fa6085f8e9d07,0x000fa8c3a62e1991,0x000fab5084e1f660, + 0x000fadb36c84cccb,0x000faff041086846,0x000fb20a6ea22bb9,0x000fb404fb42cb3c, + 0x000fb5e295158173,0x000fb7a59e99727a,0x000fb95038c8789d,0x000fbae44ba684eb, + 0x000fbc638d822e60,0x000fbdcf89209ffa,0x000fbf29a303cfc5,0x000fc0731df1089c, + 0x000fc1ad1ed6c8b1,0x000fc2d8b02b5c89,0x000fc3f6c4d92131,0x000fc5083ac9ba7d, + 0x000fc60ddd1e9cd6,0x000fc7086622e825,0x000fc7f881009f0b,0x000fc8decb41ac70, + 0x000fc9bbd623d7ec,0x000fca9027c5b26d,0x000fcb5c3c319c49,0x000fcc20864b4449, + 0x000fccdd70a35d40,0x000fcd935e34bf80,0x000fce42ab0db8bd,0x000fceebace7ec01, + 0x000fcf8eb3b0d0e7,0x000fd02c0a049b60,0x000fd0c3f59d199c,0x000fd156b7b5e27e, + 0x000fd1e48d670341,0x000fd26daff73551,0x000fd2f2552684be,0x000fd372af7233c1, + 0x000fd3eeee528f62,0x000fd4673e73543a,0x000fd4dbc9e72ff7,0x000fd54cb856dc2c, + 0x000fd5ba2f2c4119,0x000fd62451ba02c2,0x000fd68b415fcff4,0x000fd6ef1dabc160, + 0x000fd75004790eb6,0x000fd7ae120c583f,0x000fd809612dbd09,0x000fd8620b40effa, + 0x000fd8b8285b78fd,0x000fd90bcf594b1d,0x000fd95d15efd425,0x000fd9ac10bfa70c, + 0x000fd9f8d364df06,0x000fda437086566b,0x000fda8bf9e3c9fe,0x000fdad28062fed5, + 0x000fdb17141bff2c,0x000fdb59c4648085,0x000fdb9a9fda83cc,0x000fdbd9b46e3ed4, + 0x000fdc170f6b5d04,0x000fdc52bd81a3fb,0x000fdc8ccacd07ba,0x000fdcc542dd3902, + 0x000fdcfc30bcb793,0x000fdd319ef77143,0x000fdd6597a0f60b,0x000fdd98245a48a2, + 0x000fddc94e575271,0x000fddf91e64014f,0x000fde279ce914ca,0x000fde54d1f0a06a, + 0x000fde80c52a47cf,0x000fdeab7def394e,0x000fded50345eb35,0x000fdefd5be59fa0, + 0x000fdf248e39b26f,0x000fdf4aa064b4af,0x000fdf6f98435894,0x000fdf937b6f30ba, + 0x000fdfb64f414571,0x000fdfd818d48262,0x000fdff8dd07fed8,0x000fe018a08122c4, + 0x000fe03767adaa59,0x000fe05536c58a13,0x000fe07211ccb4c5,0x000fe08dfc94c532, + 0x000fe0a8fabe8ca1,0x000fe0c30fbb87a5,0x000fe0dc3ecf3a5a,0x000fe0f48b107521, + 0x000fe10bf76a82ef,0x000fe122869e41ff,0x000fe1383b4327e1,0x000fe14d17c83187, + 0x000fe1611e74c023,0x000fe1745169635a,0x000fe186b2a09176,0x000fe19843ef4e07, + 0x000fe1a90705bf63,0x000fe1b8fd6fb37c,0x000fe1c828951443,0x000fe1d689ba4bfd, + 0x000fe1e4220099a4,0x000fe1f0f26655a0,0x000fe1fcfbc726d4,0x000fe2083edc2830, + 0x000fe212bc3bfeb4,0x000fe21c745adfe3,0x000fe225678a8895,0x000fe22d95fa23f4, + 0x000fe234ffb62282,0x000fe23ba4a800d9,0x000fe2418495fddc,0x000fe2469f22bffb, + 0x000fe24af3cce90d,0x000fe24e81ee9858,0x000fe25148bcda19,0x000fe253474703fe, + 0x000fe2547c75fdc6,0x000fe254e70b754f,0x000fe25485a0fd1a,0x000fe25356a71450, + 0x000fe2515864173a,0x000fe24e88f316f1,0x000fe24ae64296fa,0x000fe2466e132f60, + 0x000fe2411df611bd,0x000fe23af34b6f73,0x000fe233eb40bf41,0x000fe22c02cee01b, + 0x000fe22336b81710,0x000fe2198385e5cc,0x000fe20ee586b707,0x000fe20358cb5dfb, + 0x000fe1f6d92465b1,0x000fe1e9621f2c9e,0x000fe1daef02c8da,0x000fe1cb7accb0a6, + 0x000fe1bb002d22c9,0x000fe1a9798349b8,0x000fe196e0d9140c,0x000fe1832fdebc44, + 0x000fe16e5fe5f931,0x000fe15869dccfcf,0x000fe1414647fe78,0x000fe128ed3cf8b2, + 0x000fe10f565b69cf,0x000fe0f478c633ab,0x000fe0d84b1bdd9e,0x000fe0bac36e6688, + 0x000fe09bd73a6b5b,0x000fe07b7b5d920a,0x000fe059a40c26d2,0x000fe03644c5d7f8, + 0x000fe011504979b2,0x000fdfeab887b95c,0x000fdfc26e94a447,0x000fdf986297e305, + 0x000fdf6c83bb8663,0x000fdf3ec0193eed,0x000fdf0f04a5d30a,0x000fdedd3d1aa204, + 0x000fdea953dcfc13,0x000fde7331e3100d,0x000fde3abe9626f2,0x000fddffdfb1dbd5, + 0x000fddc2791ff351,0x000fdd826cd068c6,0x000fdd3f9a8d3856,0x000fdcf9dfc95b0c, + 0x000fdcb1176a55fe,0x000fdc65198ba50b,0x000fdc15bb3b2daa,0x000fdbc2ce2dc4ae, + 0x000fdb6c206aaaca,0x000fdb117becb4a1,0x000fdab2a6379bf0,0x000fda4f5fdfb4e9, + 0x000fd9e76401f3a3,0x000fd97a67a9ce1f,0x000fd90819221429,0x000fd8901f2d4b02, + 0x000fd812182170e1,0x000fd78d98e23cd3,0x000fd7022bb3f082,0x000fd66f4edf96b9, + 0x000fd5d473200305,0x000fd530f9ccff94,0x000fd48432b7b351,0x000fd3cd59a8469e, + 0x000fd30b9368f90a,0x000fd23dea45f500,0x000fd16349e2e04a,0x000fd07a7a3ef98a, + 0x000fcf8219b5df05,0x000fce7895bcfcde,0x000fcd5c220ad5e2,0x000fcc2aadbc17dc, + 0x000fcae1d5e81fbc,0x000fc97ed4e778f9,0x000fc7fe6d4d720e,0x000fc65ccf39c2fc, + 0x000fc4957623cb03,0x000fc2a2fc826dc7,0x000fc07ee19b01cd,0x000fbe213c1cf493, + 0x000fbb8051ac1566,0x000fb890078d120e,0x000fb5411a5b9a95,0x000fb18000547133, + 0x000fad334827f1e2,0x000fa839276708b9,0x000fa263b32e37ed,0x000f9b72d1c52cd1, + 0x000f930a1a281a05,0x000f889f023d820a,0x000f7b577d2be5f3,0x000f69c650c40a8f, + 0x000f51530f0916d8,0x000f2cb0e3c5933e,0x000eeefb15d605d8,0x000e6da6ecf27460] const we = [1.9311480126418366e-15,1.4178028487910829e-17,2.3278824993382448e-17, diff --git a/test/mpfr.jl b/test/mpfr.jl index 059d97b008e4d..7a05b2528d929 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -446,7 +446,7 @@ with_bigfloat_precision(53) do end # ceil / iceil / floor / ifloor / trunc / itrunc -x = BigFloat("28273.2312487489135135135") +x = BigFloat("28273.7312487489135135135") y = BigInt(28273) z = BigInt(28274) a = BigFloat("123456789012345678901234567890.2414") From b7eac028dacf352c346cb2602fb2f52d23e29aed Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 24 Nov 2014 16:45:23 -0500 Subject: [PATCH 2/4] juliatypes.jl: implement exhaustive search over unions currently this breaks unions in varargs we also need to handle multiple lexical occurrences of the same Union, e.g. U = Union(Int,Int8); (U, U, U) since I'm replacing Unions based on object id. hopefully this fix can also restore varargs, by recording multiple occurrences of the union in (A, B, C) <: (Union(C,D)...) --- examples/juliatypes.jl | 164 +++++++++++++++++++++++++++++------------ 1 file changed, 118 insertions(+), 46 deletions(-) diff --git a/examples/juliatypes.jl b/examples/juliatypes.jl index ea520fab63759..375bf3d50c0c2 100644 --- a/examples/juliatypes.jl +++ b/examples/juliatypes.jl @@ -108,6 +108,8 @@ tupletype(xs...) = inst(TupleName, xs...) # subtype +isequal_type(x::Ty, y::Ty) = issub(x, y) && issub(y, x) + type Bounds # record current lower and upper bounds of a Var # depth: invariant position nesting depth of a Var's UnionAll @@ -118,49 +120,77 @@ type Bounds right::Bool end -Base.copy(b::Bounds) = Bounds(b.lb, b.ub, b.depth, b.right) +type Env + vars::Dict{Var,Bounds} + depth::Int + # mapping giving element of each union to be considered + unions::ObjectIdDict -# maps Var to Bounds, and `:depth` to current depth -const Env = Dict{Any,Any} + Env() = new(Dict{Var,Bounds}(), 1, ObjectIdDict()) +end -isequal_type(x::Ty, y::Ty) = issub(x, y) && issub(y, x) +function find_unions(t, invariant, left, right, both) + if isa(t,UnionT) && t !== BottomT + push!(invariant ? both : left, t) + for x in t.types; find_unions(x, invariant, left, right, both); end + elseif isa(t,TagT) + for x in t.params; find_unions(x, true, left, right, both); end + elseif isa(t,UnionAllT) + find_unions(t.var.lb, invariant, + both, # TODO: doesn't seem right, but works so far + left, both) + find_unions(t.var.ub, invariant, left, right, both) + find_unions(t.T, invariant, left, right, both) + end +end function issub(x, y) - env = Env() - env[:depth] = 1 - ans = issub(x, y, env) - #if ans && !isempty(env) - # println("subject to") - # Base.showdict(env, limit=false); println() - #end - ans + LU = []; RU = [] + LB = []; RB = [] + find_unions(x, false, LU, RU, LB) + find_unions(y, false, RU, LU, RB) + + return (forall_exists_issub(x, y, vcat(LU, LB), vcat(RU, RB)) && + ((isempty(RB) && isempty(LB)) || + forall_exists_issub(x, y, vcat(LU, RB), vcat(RU, LB)))) end -issub(x, y, env) = (x === y) +function forall_exists_issub(x, y, LU, RU) + env = Env() -function union_issub(x::UnionT, t::Ty, env) - for tt in x.types - if !issub(tt, t, env) + lspace = Base.IteratorsMD.IndexIterator(tuple(map(u->length(u.types),LU)...)) + rspace = Base.IteratorsMD.IndexIterator(tuple(map(u->length(u.types),RU)...)) + + # for all combinations of elements from Unions on the left, there must + # exist a combination of elements from Unions on the right that makes + # issub() true. Unions in invariant position are on both the left and + # the right in this formula. + + for forall in lspace + for i=1:length(LU) + env.unions[LU[i]] = LU[i].types[forall.(i)] + end + found = false + for exists in rspace + for i=1:length(RU) + env.unions[RU[i]] = RU[i].types[exists.(i)] + end + if issub(x, y, env) + found = true; break + end + end + if !found return false end end return true end -issub(x::UnionT, t::UnionT, env) = union_issub(x, t, env) -issub(x::UnionT, t::Ty, env) = union_issub(x, t, env) +issub(x, y, env) = (x === y) -function issub(x::Ty, t::UnionT, env) - # TODO: distribute Union over tuple - for tt in t.types - e′ = Env([ k => copy(v) for (k,v) in env]) - if issub(x, tt, e′) - merge!(env, e′) - return true - end - end - return false -end +issub(x::UnionT, t::UnionT, env) = x===BottomT || issub(env.unions[x], t, env) +issub(x::UnionT, t::Ty, env) = x===BottomT || issub(env.unions[x], t, env) +issub(x::Ty, t::UnionT, env) = t!==BottomT && issub(x, env.unions[t], env) function issub(a::TagT, b::TagT, env) a === b && return true @@ -193,26 +223,27 @@ function issub(a::TagT, b::TagT, env) end @assert false else - env = copy(env) - env[:depth] = env[:depth]+1 # crossing invariant constructor, increment depth + env.depth += 1 # crossing invariant constructor, increment depth for i = 1:length(a.params) ai, bi = a.params[i], b.params[i] # use issub in both directions to test equality if !(ai===bi || (issub(ai, bi, env) && issub(bi, ai, env))) + env.depth -= 1 return false end end + env.depth -= 1 end return true end function issub(a::Var, b::Ty, env) - aa = env[a] + aa = env.vars[a] # Vars are fully checked by the "forward" direction of A<:B in # invariant position. So just return true when checking the "flipped" # direction B<:A. aa.right && return true - d = env[:depth] + d = env.depth if d != aa.depth # && d > 1 ??? # Var <: non-Var can only be true when there are no invariant # constructors between the UnionAll and this occurrence of Var. @@ -223,10 +254,10 @@ end function issub(a::Var, b::Var, env) a === b && return true - aa = env[a] + aa = env.vars[a] aa.right && return true - bb = env[b] - d = env[:depth] + bb = env.vars[b] + d = env.depth if aa.depth != bb.depth # && d > 1 ??? # Vars must occur at same depth return false @@ -239,7 +270,7 @@ function issub(a::Var, b::Var, env) # (L,L) <: (T,S) but not (T,S) <: (R,R) return false end - if !(issub(bb.lb, aa.ub, env) && issub(aa.ub, bb.ub, env)) + if !(issub(bb.lb, aa.lb, env) && issub(aa.ub, bb.ub, env)) # make sure equality constraint is within the current bounds of Var return false end @@ -257,9 +288,9 @@ function issub(a::Var, b::Var, env) end function issub(a::Ty, b::Var, env) - bb = env[b] + bb = env.vars[b] !bb.right && return true - if env[:depth] > bb.depth + if env.depth > bb.depth if isa(bb.ub,Var) return false end @@ -277,16 +308,16 @@ function issub(a::Ty, b::Var, env) end function issub_unionall(a::Ty, b::UnionAllT, env) - env[b.var] = Bounds(b.var.lb, b.var.ub, env[:depth], true) + env.vars[b.var] = Bounds(b.var.lb, b.var.ub, env.depth, true) ans = issub(a, b.T, env) - delete!(env, b.var) + delete!(env.vars, b.var) return ans end function unionall_issub(a::UnionAllT, b::Ty, env) - env[a.var] = Bounds(a.var.lb, a.var.ub, env[:depth], false) + env.vars[a.var] = Bounds(a.var.lb, a.var.ub, env.depth, false) ans = issub(a.T, b, env) - delete!(env, a.var) + delete!(env.vars, a.var) return ans end @@ -532,6 +563,31 @@ function test_3() @test issub(Ty(Array{Array{Int,1},Int}), (@UnionAll T inst(ArrayT,inst(ArrayT,T,1),T))) + + @test issub(Ty((Integer,Int)), @UnionAll T<:Ty(Integer) @UnionAll S<:T tupletype(T,S)) + @test !issub(Ty((Integer,Int)), @UnionAll T<:Ty(Int) @UnionAll S<:T tupletype(T,S)) + @test !issub(Ty((Integer,Int)), @UnionAll T<:Ty(String) @UnionAll S<:T tupletype(T,S)) + + @test issub(Ty((Float32,Array{Float32,1})), + @UnionAll T<:Ty(Real) @UnionAll S<:inst(AbstractArrayT,T,1) tupletype(T,S)) + + @test !issub(Ty((Float32,Array{Float64,1})), + @UnionAll T<:Ty(Real) @UnionAll S<:inst(AbstractArrayT,T,1) tupletype(T,S)) + + @test issub(Ty((Float32,Array{Real,1})), + @UnionAll T<:Ty(Real) @UnionAll S<:inst(AbstractArrayT,T,1) tupletype(T,S)) + + @test !issub(Ty((Number,Array{Real,1})), + @UnionAll T<:Ty(Real) @UnionAll S<:inst(AbstractArrayT,T,1) tupletype(T,S)) + + @test issub((@UnionAll Ty(Int)<:T<:Ty(Integer) T), @UnionAll T<:Ty(Real) T) + @test issub((@UnionAll Ty(Int)<:T<:Ty(Integer) inst(ArrayT,T,1)), + (@UnionAll T<:Ty(Real) inst(ArrayT,T,1))) + + @test issub((@UnionAll Ty(Int)<:T<:Ty(Integer) T), + (@UnionAll Ty(Integer)<:T<:Ty(Real) T)) + @test !issub((@UnionAll Ty(Int)<:T<:Ty(Integer) inst(ArrayT,T,1)), + (@UnionAll Ty(Integer)<:T<:Ty(Real) inst(ArrayT,T,1))) end # level 4: Union @@ -543,8 +599,7 @@ function test_4() @test isequal_type(UnionT((Ty(Int),Ty(Integer))), Ty(Integer)) - @test issub_strict((Int,Int8,Int), (Union(Int,Int8)...,)) - @test issub_strict((Int,Int8,Int), (Union(Int,Int8,Int16)...,)) + @test issub((Union(Int,Int8),Int16), Union((Int,Int16),(Int8,Int16))) end # level 5: union and UnionAll @@ -552,9 +607,26 @@ function test_5() @test issub(Ty((String,Array{Int,1})), (@UnionAll T UnionT((tupletype(T,inst(ArrayT,T,1)), tupletype(T,inst(ArrayT,Ty(Int),1)))))) + + @test issub(Ty((Union(Vector{Int},Vector{Int8}),)), + @UnionAll T tupletype(inst(ArrayT,T,1),)) + + @test !issub(Ty((Union(Vector{Int},Vector{Int8}),Vector{Int})), + @UnionAll T tupletype(inst(ArrayT,T,1), inst(ArrayT,T,1))) + + @test !issub(Ty((Union(Vector{Int},Vector{Int8}),Vector{Int8})), + @UnionAll T tupletype(inst(ArrayT,T,1), inst(ArrayT,T,1))) + + @test !issub(Ty(Vector{Int}), @UnionAll T>:Ty(Union(Int,Int8)) inst(ArrayT,T,1)) + @test issub(Ty(Vector{Integer}), @UnionAll T>:Ty(Union(Int,Int8)) inst(ArrayT,T,1)) + @test issub(Ty(Vector{Union(Int,Int8)}), @UnionAll T>:Ty(Union(Int,Int8)) inst(ArrayT,T,1)) + + @test issub((@UnionAll Ty(Int)<:T<:Ty(Union(Int,Int8)) inst(ArrayT,T,1)), + (@UnionAll Ty(Int)<:T<:Ty(Union(Int,Int8)) inst(ArrayT,T,1))) end # tests that don't pass yet function test_failing() - @test issub((Union(Int,Int8),Int16), Union((Int,Int16),(Int8,Int16))) + @test issub_strict((Int,Int8,Int), (Union(Int,Int8)...,)) + @test issub_strict((Int,Int8,Int), (Union(Int,Int8,Int16)...,)) end From 56852d8a13688ba74ffab230a5da5bf015a9672d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sun, 23 Nov 2014 22:14:07 +0000 Subject: [PATCH 3/4] itrunc -> trunc, etc, improve iround --- base/abstractarray.jl | 10 +- base/bitarray.jl | 5 +- base/char.jl | 2 +- base/complex.jl | 4 +- base/darray.jl | 2 +- base/dates/ranges.jl | 2 +- base/deprecated.jl | 9 ++ base/exports.jl | 4 - base/float.jl | 111 ++++++++-------- base/float16.jl | 6 +- base/floatfuncs.jl | 22 +-- base/grisu/float.jl | 2 +- base/int.jl | 33 +---- base/interactiveutil.jl | 2 +- base/intfuncs.jl | 4 +- base/libc.jl | 4 +- base/linalg/bidiag.jl | 8 +- base/linalg/dense.jl | 2 +- base/linalg/givens.jl | 2 +- base/linalg/matmul.jl | 2 +- base/linalg/tridiag.jl | 21 +-- base/mmap.jl | 2 +- base/mpfr.jl | 98 +++++++++----- base/pkg/resolve/maxsum.jl | 2 +- base/poll.jl | 2 +- base/primes.jl | 2 +- base/printf.jl | 2 +- base/profile.jl | 12 +- base/range.jl | 12 +- base/rational.jl | 20 +-- base/sort.jl | 8 +- base/sparse/sparsematrix.jl | 50 +++++-- base/special/gamma.jl | 8 +- base/statistics.jl | 12 +- doc/helpdb.jl | 110 +++++++++------ doc/manual/arrays.rst | 1 - doc/manual/mathematical-operations.rst | 16 +-- doc/stdlib/base.rst | 46 ++++--- src/intrinsics.cpp | 126 +----------------- test/arrayperf.jl | 36 ++--- test/complex.jl | 2 +- test/euler.jl | 2 +- test/file.jl | 2 +- test/linalg/triangular.jl | 2 +- test/mpfr.jl | 177 ++++++++++++++----------- test/numbers.jl | 115 ++++++++-------- test/random.jl | 8 +- test/rounding.jl | 14 +- test/sparse.jl | 9 +- test/strings.jl | 2 +- 50 files changed, 575 insertions(+), 580 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 23fc292d379e9..fd8464199cb83 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1385,7 +1385,7 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real) empty!(S) p == 0 && return S nexpected = p * length(A) - sizehint(S, iround(nexpected + 5*sqrt(nexpected))) + sizehint(S, round(Int,nexpected + 5*sqrt(nexpected))) if p > 0.15 # empirical threshold for trivial O(n) algorithm to be better for i = 1:n rand() <= p && push!(S, A[i]) @@ -1396,14 +1396,14 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real) # s==k (k > 0) is (1-p)^(k-1) * p, and hence the probability (CDF) that # s is in {1,...,k} is 1-(1-p)^k = F(k). Thus, we can draw the skip s # from this probability distribution via the discrete inverse-transform - # method: s = iceil(F^{-1}(u)) where u = rand(), which is simply - # s = iceil(log(rand()) / log1p(-p)). + # method: s = ceil(F^{-1}(u)) where u = rand(), which is simply + # s = ceil(log(rand()) / log1p(-p)). L = 1 / log1p(-p) i = 0 while true s = log(rand()) * L # note that rand() < 1, so s > 0 - s >= n - i && return S # compare before iceil to avoid overflow - push!(S, A[i += iceil(s)]) + s >= n - i && return S # compare before ceil to avoid overflow + push!(S, A[i += ceil(Int,s)]) end # [This algorithm is similar in spirit to, but much simpler than, # the one by Vitter for a related problem in "Faster methods for diff --git a/base/bitarray.jl b/base/bitarray.jl index 40794ba4100e1..e10008a41fe35 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -790,10 +790,7 @@ function empty!(B::BitVector) end ## Misc functions - -for f in (:iround, :itrunc, :ifloor, :iceil, :abs) - @eval ($f)(B::BitArray) = copy(B) -end +abs(B::BitArray) = copy(B) ## Unary operators ## diff --git a/base/char.jl b/base/char.jl index f61a7cfd98475..778a1db06dd40 100644 --- a/base/char.jl +++ b/base/char.jl @@ -1,5 +1,5 @@ char(x) = convert(Char, x) -char(x::FloatingPoint) = char(iround(x)) +char(x::FloatingPoint) = char(round(UInt32,x)) integer(x::Char) = int(x) diff --git a/base/complex.jl b/base/complex.jl index 544211050eb9b..b108c5c34d634 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -477,7 +477,7 @@ function ^{T<:Complex}(z::T, p::T) # apply some corrections to force known zeros if pim == 0 - ip = itrunc(pr) + ip = trunc(pr) if ip == pr if zi == 0 im = copysign(zero(im), im) @@ -490,7 +490,7 @@ function ^{T<:Complex}(z::T, p::T) end else dr = pr*2 - ip = itrunc(dr) + ip = trunc(dr) if ip == dr && zi == 0 if zr < 0 re = copysign(zero(re), re) diff --git a/base/darray.jl b/base/darray.jl index c96096209c923..cfb854ceea212 100644 --- a/base/darray.jl +++ b/base/darray.jl @@ -94,7 +94,7 @@ end # get array of start indexes for dividing sz into nc chunks function defaultdist(sz::Int, nc::Int) if sz >= nc - iround(linspace(1, sz+1, nc+1)) + round(Int,linspace(1, sz+1, nc+1)) else [[1:(sz+1)], zeros(Int, nc-sz)] end diff --git a/base/dates/ranges.jl b/base/dates/ranges.jl index 719c680705fa4..a9baf97915aea 100644 --- a/base/dates/ranges.jl +++ b/base/dates/ranges.jl @@ -4,7 +4,7 @@ Base.colon{T<:DateTime}(start::T, stop::T) = StepRange(start, Day(1), stop) # Given a start and end date, how many steps/periods are in between -guess(a::DateTime,b::DateTime,c) = ifloor(Int64,(int128(b) - int128(a))/toms(c)) +guess(a::DateTime,b::DateTime,c) = floor(Int64,(int128(b) - int128(a))/toms(c)) guess(a::Date,b::Date,c) = int64(div(int64(b - a),days(c))) function len(a,b,c) lo, hi, st = min(a,b), max(a,b), abs(c) diff --git a/base/deprecated.jl b/base/deprecated.jl index 9dfe8988dd03e..ab873f30b4c05 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -231,3 +231,12 @@ const Uint128 = UInt128 @deprecate rand!(r::Range, A::AbstractArray) rand!(A, r) @deprecate rand!(mt::MersenneTwister, r::Range, A::AbstractArray) rand!(mt, A, r) + +@deprecate itrunc(x) trunc(Integer,x) +@deprecate itrunc{T<:Integer}(::Type{T},x::Real) trunc(T,x) +@deprecate iceil(x) ceil(Integer,x) +@deprecate iceil{T}(::Type{T},x) ceil(T,x) +@deprecate ifloor(x) floor(Integer,x) +@deprecate ifloor{T}(::Type{T},x) floor(T,x) +@deprecate iround(x) round(Integer,x) +@deprecate iround{T}(::Type{T},x) round(T,x) diff --git a/base/exports.jl b/base/exports.jl index c16c24591f2a2..7e2f9340909f9 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -360,8 +360,6 @@ export gcdx, hex2num, hypot, - iceil, - ifloor, imag, int, int128, @@ -373,7 +371,6 @@ export inv, invdigamma, invmod, - iround, isapprox, iseltype, iseven, @@ -387,7 +384,6 @@ export isqrt, isreal, issubnormal, - itrunc, lcm, ldexp, leading_ones, diff --git a/base/float.jl b/base/float.jl index 295a1f29f4221..32107f407aac4 100644 --- a/base/float.jl +++ b/base/float.jl @@ -1,28 +1,17 @@ ## conversions to floating-point ## - -convert(::Type{Float32}, x::Int128) = float32(reinterpret(UInt128,abs(x)))*(1-2(x<0)) -convert(::Type{Float32}, x::UInt128) = float32(uint64(x&0xffffffffffffffff)) + ldexp(float32(uint64(x>>>64)),64) -promote_rule(::Type{Float32}, ::Type{Int128} ) = Float32 -promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32 - -convert(::Type{Float64}, x::Int128) = float64(reinterpret(UInt128,abs(x)))*(1-2(x<0)) -convert(::Type{Float64}, x::UInt128) = float64(uint64(x&0xffffffffffffffff)) + ldexp(float64(uint64(x>>>64)),64) -promote_rule(::Type{Float64}, ::Type{Int128} ) = Float64 -promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64 - convert(::Type{Float16}, x::Integer) = convert(Float16, convert(Float32,x)) for t in (Bool,Char,Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64) @eval promote_rule(::Type{Float16}, ::Type{$t}) = Float32 end for t1 in (Float32,Float64) - for st in (Int8,Int16,Int32,Int64) + for st in (Int8,Int16,Int32,Int64,Int128) @eval begin convert(::Type{$t1},x::($st)) = box($t1,sitofp($t1,unbox($st,x))) promote_rule(::Type{$t1}, ::Type{$st} ) = $t1 end end - for ut in (Bool,Char,UInt8,UInt16,UInt32,UInt64) + for ut in (Bool,Char,UInt8,UInt16,UInt32,UInt64,UInt128) @eval begin convert(::Type{$t1},x::($ut)) = box($t1,uitofp($t1,unbox($ut,x))) promote_rule(::Type{$t1}, ::Type{$ut} ) = $t1 @@ -55,55 +44,39 @@ float32(x) = convert(Float32, x) float64(x) = convert(Float64, x) float(x) = convert(FloatingPoint, x) -## conversions from floating-point ## - -# fallbacks using only convert, trunc, ceil, floor, round -itrunc(x::FloatingPoint) = convert(Integer,trunc(x)) -iceil (x::FloatingPoint) = convert(Integer,ceil(x)) # TODO: fast primitive for iceil -ifloor(x::FloatingPoint) = convert(Integer,floor(x)) # TOOD: fast primitive for ifloor -iround(x::FloatingPoint) = convert(Integer,round(x)) - -itrunc{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,trunc(x)) -iceil {T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,ceil(x)) -ifloor{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,floor(x)) -iround{T<:Integer}(::Type{T}, x::FloatingPoint) = convert(T,round(x)) - -## fast specific type conversions ## - -iround(x::Float32) = iround(Int, x) -iround(x::Float64) = iround(Int, x) -itrunc(x::Float32) = itrunc(Int, x) -itrunc(x::Float64) = itrunc(Int, x) - -for to in (Int8, Int16, Int32, Int64) +for Ti in (Int8, Int16, Int32, Int64, Int128) @eval begin - iround(::Type{$to}, x::Float32) = box($to,fpsiround($to,unbox(Float32,x))) - iround(::Type{$to}, x::Float64) = box($to,fpsiround($to,unbox(Float64,x))) - itrunc(::Type{$to}, x::Float32) = box($to,fptosi($to,unbox(Float32,x))) - itrunc(::Type{$to}, x::Float64) = box($to,fptosi($to,unbox(Float64,x))) + unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptosi($Ti,unbox(Float32,x))) + unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptosi($Ti,unbox(Float64,x))) end end - -for to in (UInt8, UInt16, UInt32, UInt64) +for Ti in (UInt8, UInt16, UInt32, UInt64, UInt128) @eval begin - iround(::Type{$to}, x::Float32) = box($to,fpuiround($to,unbox(Float32,x))) - iround(::Type{$to}, x::Float64) = box($to,fpuiround($to,unbox(Float64,x))) - itrunc(::Type{$to}, x::Float32) = box($to,fptoui($to,unbox(Float32,x))) - itrunc(::Type{$to}, x::Float64) = box($to,fptoui($to,unbox(Float64,x))) + unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptoui($Ti,unbox(Float32,x))) + unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptoui($Ti,unbox(Float64,x))) end end -iround(::Type{Int128}, x::Float32) = convert(Int128,round(x)) -iround(::Type{Int128}, x::Float64) = convert(Int128,round(x)) -iround(::Type{UInt128}, x::Float32) = convert(UInt128,round(x)) -iround(::Type{UInt128}, x::Float64) = convert(UInt128,round(x)) +# matches convert methods +# also determines floor, ceil, round +trunc(::Type{Signed}, x::Float32) = trunc(Int,x) +trunc(::Type{Signed}, x::Float64) = trunc(Int,x) +trunc(::Type{Unsigned}, x::Float32) = trunc(UInt,x) +trunc(::Type{Unsigned}, x::Float64) = trunc(UInt,x) +trunc(::Type{Integer}, x::Float32) = trunc(Int,x) +trunc(::Type{Integer}, x::Float64) = trunc(Int,x) + +# fallbacks +floor{T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,floor(x)) +ceil {T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,ceil(x)) +round {T<:Integer}(::Type{T}, x::FloatingPoint) = trunc(T,round(x)) + # this is needed very early because it is used by Range and colon round(x::Float64) = ccall((:round, Base.libm_name), Float64, (Float64,), x) floor(x::Float64) = ccall((:floor, Base.libm_name), Float64, (Float64,), x) ## floating point promotions ## - promote_rule(::Type{Float32}, ::Type{Float16}) = Float32 promote_rule(::Type{Float64}, ::Type{Float16}) = Float64 promote_rule(::Type{Float64}, ::Type{Float32}) = Float64 @@ -112,7 +85,6 @@ widen(::Type{Float16}) = Float32 widen(::Type{Float32}) = Float64 ## floating point arithmetic ## - -(x::Float32) = box(Float32,neg_float(unbox(Float32,x))) -(x::Float64) = box(Float64,neg_float(unbox(Float64,x))) @@ -128,7 +100,6 @@ widen(::Type{Float32}) = Float64 # TODO: faster floating point div? # TODO: faster floating point fld? # TODO: faster floating point mod? - rem(x::Float32, y::Float32) = box(Float32,rem_float(unbox(Float32,x),unbox(Float32,y))) rem(x::Float64, y::Float64) = box(Float64,rem_float(unbox(Float64,x),unbox(Float64,y))) @@ -147,7 +118,6 @@ end ## floating point comparisons ## - ==(x::Float32, y::Float32) = eq_float(unbox(Float32,x),unbox(Float32,y)) ==(x::Float64, y::Float64) = eq_float(unbox(Float64,x),unbox(Float64,y)) !=(x::Float32, y::Float32) = ne_float(unbox(Float32,x),unbox(Float32,y)) @@ -177,31 +147,31 @@ function cmp(x::FloatingPoint, y::Real) ifelse(xy, 1, 0)) end -for Ti in (Int64,UInt64) +for Ti in (Int64,UInt64,Int128,UInt128) for Tf in (Float32,Float64) @eval begin function ==(x::$Tf, y::$Ti) fy = ($Tf)(y) - (x == fy) & (y == itrunc($Ti,fy)) + (x == fy) & (y == unsafe_trunc($Ti,fy)) end ==(y::$Ti, x::$Tf) = x==y function <(x::$Ti, y::$Tf) fx = ($Tf)(x) - (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < itrunc($Ti,fx)) )) + (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) )) end function <=(x::$Ti, y::$Tf) fx = ($Tf)(x) - (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= itrunc($Ti,fx)) )) + (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) )) end function <(x::$Tf, y::$Ti) fy = ($Tf)(y) - (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) < y)) + (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y)) end function <=(x::$Tf, y::$Ti) fy = ($Tf)(y) - (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) <= y)) + (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y)) end end end @@ -263,6 +233,31 @@ nextfloat(x::Float64, i::Integer) = nextfloat(x::FloatingPoint) = nextfloat(x,1) prevfloat(x::FloatingPoint) = nextfloat(x,-1) +for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128) + for Tf in (Float32, Float64) + if sizeof(Ti) < sizeof(Tf) || Ti <: Unsigned # Tf(typemin(Ti))-1 is exact + @eval function trunc(::Type{$Ti},x::$Tf) + $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf)) || throw(InexactError()) + unsafe_trunc($Ti,x) + end + else + @eval function trunc(::Type{$Ti},x::$Tf) + $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))) || throw(InexactError()) + unsafe_trunc($Ti,x) + end + end + end +end + +# adding prevfloat(0.5) will prevent prevfloat(0.5) and odd x with eps(x)=1.0 +# from rounding in the wrong direction in RoundToNearest +for Tf in (Float32,Float64) + @eval function round{T<:Integer}(::Type{T}, x::$Tf) + trunc(T,x+copysign($(prevfloat(Tf(0.5))),x)) + end +end + + @eval begin issubnormal(x::Float32) = (abs(x) < $(box(Float32,unbox(UInt32,0x00800000)))) & (x!=0) issubnormal(x::Float64) = (abs(x) < $(box(Float64,unbox(UInt64,0x0010000000000000)))) & (x!=0) diff --git a/base/float16.jl b/base/float16.jl index 0001394e2bf6f..177517b6fd7d8 100644 --- a/base/float16.jl +++ b/base/float16.jl @@ -102,8 +102,10 @@ convert(::Type{UInt128}, x::Float16) = convert(UInt128, float32(x)) convert{T<:Integer}(::Type{T}, x::Float16) = convert(T, float32(x)) -iround{T<:Integer}(::Type{T}, x::Float16) = iround(T, float32(x)) -itrunc{T<:Integer}(::Type{T}, x::Float16) = itrunc(T, float32(x)) +round{T<:Integer}(::Type{T}, x::Float16) = round(T, float32(x)) +trunc{T<:Integer}(::Type{T}, x::Float16) = trunc(T, float32(x)) +floor{T<:Integer}(::Type{T}, x::Float16) = floor(T, float32(x)) +ceil {T<:Integer}(::Type{T}, x::Float16) = ceil(T, float32(x)) round(x::Float16) = float16(round(float32(x))) trunc(x::Float16) = float16(trunc(float32(x))) diff --git a/base/floatfuncs.jl b/base/floatfuncs.jl index 25efe7168046c..5194ecd503d82 100644 --- a/base/floatfuncs.jl +++ b/base/floatfuncs.jl @@ -35,11 +35,6 @@ function hex2num(s::AbstractString) return box(Float64,unbox(Int64,parseint(Int64,s,16))) end -@vectorize_1arg Real iround -@vectorize_1arg Real itrunc -@vectorize_1arg Real ifloor -@vectorize_1arg Real iceil - @vectorize_1arg Number abs @vectorize_1arg Number abs2 @vectorize_1arg Number angle @@ -48,9 +43,20 @@ end @vectorize_1arg Number isinf @vectorize_1arg Number isfinite -iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R,1}) = [ iround(T, x[i]) for i = 1:length(x) ] -iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R,2}) = [ iround(T, x[i,j]) for i = 1:size(x,1), j = 1:size(x,2) ] -iround{T<:Integer,R<:Real}(::Type{T}, x::AbstractArray{R}) = reshape([ iround(T, x[i]) for i = 1:length(x) ], size(x)) +for f in (:trunc,:floor,:ceil,:round) + @eval begin + function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R,1}) + [ ($f)(T, x[i]) for i = 1:length(x) ] + end + function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R,2}) + [ ($f)(T, x[i,j]) for i = 1:size(x,1), j = 1:size(x,2) ] + end + function ($f){T,R<:Real}(::Type{T}, x::AbstractArray{R}) + reshape([ ($f)(T, x[i]) for i = 1:length(x) ], size(x)) + end + end +end + # adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212 # for round, og is the power of 10 relative to the decimal point diff --git a/base/grisu/float.jl b/base/grisu/float.jl index e23520dcdeb02..1cac1c4e6cd58 100644 --- a/base/grisu/float.jl +++ b/base/grisu/float.jl @@ -219,7 +219,7 @@ const MinDecimalExponent = -348 const MaxDecimalExponent = 340 function binexp_cache(min_exponent,max_exponent) - k = iceil((min_exponent+63)*D_1_LOG2_10) + k = ceil(Integer,(min_exponent+63)*D_1_LOG2_10) index = div(CachedPowersOffset+k-1,DecimalExponentDistance) + 1 cp = CachedPowers[index+1] return cp diff --git a/base/int.jl b/base/int.jl index 46b731ad79219..fb55bd8f46232 100644 --- a/base/int.jl +++ b/base/int.jl @@ -179,38 +179,20 @@ rem{T<:Integer}(x::T, ::Type{T}) = x rem(x::Integer, ::Type{Bool}) = ((x&1)!=0) mod{T<:Integer}(x::Integer, ::Type{T}) = rem(x, T) -for to in (Int8, Int16, Int32, Int64) +for to in (Int8, Int16, Int32, Int64, Int128) @eval begin convert(::Type{$to}, x::Float32) = box($to,checked_fptosi($to,unbox(Float32,x))) convert(::Type{$to}, x::Float64) = box($to,checked_fptosi($to,unbox(Float64,x))) end end -for to in (UInt8, UInt16, UInt32, UInt64) +for to in (UInt8, UInt16, UInt32, UInt64, UInt128) @eval begin convert(::Type{$to}, x::Float32) = box($to,checked_fptoui($to,unbox(Float32,x))) convert(::Type{$to}, x::Float64) = box($to,checked_fptoui($to,unbox(Float64,x))) end end -function convert(::Type{Int128}, x::FloatingPoint) - ax = abs(x) - top = trunc(ldexp(ax,-64)) - bot = ax - ldexp(top,64) - n = int128(convert(UInt64,top))<<64 + int128(convert(UInt64,bot)) - return x<0 ? -n : n -end -convert(::Type{Int128}, x::Float32) = convert(Int128, float64(x)) - -function convert(::Type{UInt128}, x::FloatingPoint) - ax = abs(x) - top = trunc(ldexp(ax,-64)) - bot = ax - ldexp(top,64) - n = uint128(convert(UInt64,top))<<64 + uint128(convert(UInt64,bot)) - return x<0 ? -n : n -end -convert(::Type{UInt128}, x::Float32) = convert(UInt128, float64(x)) - convert(::Type{Signed}, x::UInt8 ) = convert(Int8,x) convert(::Type{Signed}, x::UInt16 ) = convert(Int16,x) convert(::Type{Signed}, x::UInt32 ) = convert(Int32,x) @@ -257,11 +239,10 @@ trunc(x::Integer) = x floor(x::Integer) = x ceil(x::Integer) = x -iround(x::Integer) = x -iround{T<:Integer}(::Type{T}, x::Integer) = convert(T, x) -itrunc(x::Integer) = x -ifloor(x::Integer) = x - iceil(x::Integer) = x +round{T<:Integer}(::Type{T},x::Integer) = convert(T,x) +trunc{T<:Integer}(::Type{T},x::Integer) = convert(T,x) +floor{T<:Integer}(::Type{T},x::Integer) = convert(T,x) + ceil{T<:Integer}(::Type{T},x::Integer) = convert(T,x) ## integer construction ## @@ -382,7 +363,7 @@ for (f,t) in ((:uint8,:UInt8), (:uint16,:UInt16), (:uint32,:UInt32), (:uint64,:U (:int128,:Int128), (:uint128,:UInt128), (:signed,:Int), (:unsigned,:UInt), (:integer,:Int), (:int,:Int), (:uint,:UInt)) - @eval ($f)(x::FloatingPoint) = iround($t,x) + @eval ($f)(x::FloatingPoint) = round($t,x) end ## wide multiplication, Int128 multiply and divide ## diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index 4af05d3eccc3d..4d990fac5d851 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -368,7 +368,7 @@ end # testing -function runtests(tests = ["all"], numcores = iceil(CPU_CORES/2)) +function runtests(tests = ["all"], numcores = ceil(Int,CPU_CORES/2)) if isa(tests,AbstractString) tests = split(tests) end diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 11e3cb937979f..e312ff251c401 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -140,7 +140,7 @@ ispow2(x::Integer) = count_ones(x)==1 function nextpow(a::Real, x::Real) (a <= 1 || x <= 0) && throw(DomainError()) x <= 1 && return one(a) - n = iceil(log(a, x)) + n = ceil(Integer,log(a, x)) p = a^(n-1) # guard against roundoff error, e.g., with a=5 and x=125 p >= x ? p : a^n @@ -148,7 +148,7 @@ end # largest a^n <= x, with integer n function prevpow(a::Real, x::Real) (a <= 1 || x < 1) && throw(DomainError()) - n = ifloor(log(a, x)) + n = floor(Integer,log(a, x)) p = a^(n+1) p <= x ? p : a^n end diff --git a/base/libc.jl b/base/libc.jl index 19eebd12a38d9..29bf59aa3ce4e 100644 --- a/base/libc.jl +++ b/base/libc.jl @@ -1,8 +1,8 @@ ## time-related functions ## # TODO: check for usleep errors? -@unix_only systemsleep(s::Real) = ccall(:usleep, Int32, (UInt32,), uint32(iround(s*1e6))) -@windows_only systemsleep(s::Real) = (ccall(:Sleep, stdcall, Void, (UInt32,), uint32(iround(s*1e3))); return int32(0)) +@unix_only systemsleep(s::Real) = ccall(:usleep, Int32, (UInt32,), round(UInt32,s*1e6)) +@windows_only systemsleep(s::Real) = (ccall(:Sleep, stdcall, Void, (UInt32,), round(UInt32,s*1e3)); return int32(0)) type TmStruct sec::Int32 diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index 56c8417b43663..a6ef673ca4151 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -64,9 +64,13 @@ size(M::Bidiagonal) = (length(M.dv), length(M.dv)) size(M::Bidiagonal, d::Integer) = d<1 ? error("dimension out of range") : (d<=2 ? length(M.dv) : 1) #Elementary operations -for func in (conj, copy, round, iround) - func(M::Bidiagonal) = Bidiagonal(func(M.dv), func(M.ev), M.isupper) +for func in (:conj, :copy, :round, :trunc, :floor, :ceil) + @eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper) end +for func in (:round, :trunc, :floor, :ceil) + @eval ($func){T<:Integer}(::Type{T}, M::Bidiagonal) = Bidiagonal(($func)(T,M.dv), ($func)(T,M.ev), M.isupper) +end + transpose(M::Bidiagonal) = Bidiagonal(M.dv, M.ev, !M.isupper) ctranspose(M::Bidiagonal) = Bidiagonal(conj(M.dv), conj(M.ev), !M.isupper) diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index 4f00ee4014910..a360d56ed1f17 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -245,7 +245,7 @@ function expm!{T<:BlasFloat}(A::StridedMatrix{T}) else s = log2(nA/5.4) # power of 2 later reversed by squaring if s > 0 - si = iceil(s) + si = ceil(Int,s) A /= convert(T,2^si) end CC = T[64764752532480000.,32382376266240000.,7771770303897600., diff --git a/base/linalg/givens.jl b/base/linalg/givens.jl index 4ad93cd3a13d5..f8e17b3a490ca 100644 --- a/base/linalg/givens.jl +++ b/base/linalg/givens.jl @@ -12,7 +12,7 @@ typealias AbstractRotation Union(Givens, Rotation) realmin2(::Type{Float32}) = reinterpret(Float32, 0x26000000) realmin2(::Type{Float64}) = reinterpret(Float64, 0x21a0000000000000) -realmin2{T}(::Type{T}) = (twopar = 2one(T); twopar^itrunc(log(realmin(T)/eps(T))/log(twopar)/twopar)) +realmin2{T}(::Type{T}) = (twopar = 2one(T); twopar^trunc(Integer,log(realmin(T)/eps(T))/log(twopar)/twopar)) function givensAlgorithm{T<:FloatingPoint}(f::T, g::T) zeropar = zero(T) diff --git a/base/linalg/matmul.jl b/base/linalg/matmul.jl index 1af6d5281ee01..c59f9c34a4512 100644 --- a/base/linalg/matmul.jl +++ b/base/linalg/matmul.jl @@ -373,7 +373,7 @@ function generic_matmatmul!{T,S,R}(C::AbstractVecOrMat{R}, tA, tB, A::AbstractVe @inbounds begin if isbits(R) - tile_size = int(ifloor(sqrt(tilebufsize/sizeof(R)))) + tile_size = floor(Int,sqrt(tilebufsize/sizeof(R))) sz = (tile_size, tile_size) Atile = pointer_to_array(convert(Ptr{R}, pointer(Abuf)), sz) Btile = pointer_to_array(convert(Ptr{R}, pointer(Bbuf)), sz) diff --git a/base/linalg/tridiag.jl b/base/linalg/tridiag.jl index b1b64cd4a8675..f63873c00b81a 100644 --- a/base/linalg/tridiag.jl +++ b/base/linalg/tridiag.jl @@ -28,12 +28,12 @@ size(m::SymTridiagonal) = (length(m.dv), length(m.dv)) size(m::SymTridiagonal, d::Integer) = d<1 ? error("dimension out of range") : (d<=2 ? length(m.dv) : 1) #Elementary operations -for func in (:copy, :round, :iround, :conj) - @eval begin - ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev)) - end +for func in (:conj, :copy, :round, :trunc, :floor, :ceil) + @eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev)) +end +for func in (:round, :trunc, :floor, :ceil) + @eval ($func){T<:Integer}(::Type{T},M::SymTridiagonal) = SymTridiagonal(($func)(T,M.dv), (T,$func)(M.ev)) end - transpose(M::SymTridiagonal) = M #Identity operation ctranspose(M::SymTridiagonal) = conj(M) @@ -203,9 +203,14 @@ end copy!(dest::Tridiagonal, src::Tridiagonal) = Tridiagonal(copy!(dest.dl, src.dl), copy!(dest.d, src.d), copy!(dest.du, src.du), copy!(dest.du2, src.du2)) #Elementary operations -for func in (:copy, :round, :iround, :conj) - @eval begin - ($func)(M::Tridiagonal) = Tridiagonal(map(($func), (M.dl, M.d, M.du, M.du2))...) +for func in (:conj, :copy, :round, :trunc, :floor, :ceil) + @eval function ($func)(M::Tridiagonal) + Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2)) + end +end +for func in (:round, :trunc, :floor, :ceil) + @eval function ($func){T<:Integer}(::Type{T},M::Tridiagonal) + Tridiagonal(($func)(T,M.dl), ($func)(T,M.d), ($func)(T,M.du), ($func)(T,M.du2)) end end diff --git a/base/mmap.jl b/base/mmap.jl index fb223ba012f2f..3e8709e94a161 100644 --- a/base/mmap.jl +++ b/base/mmap.jl @@ -28,7 +28,7 @@ function mmap(len::Integer, prot::Integer, flags::Integer, fd, offset::Integer) error("requested size is too large") end # Set the offset to a page boundary - offset_page::FileOffset = ifloor(offset/pagesize)*pagesize + offset_page::FileOffset = floor(Integer,offset/pagesize)*pagesize len_page::Int = (offset-offset_page) + len # Mmap the file p = ccall(:jl_mmap, Ptr{Void}, (Ptr{Void}, Csize_t, Cint, Cint, Cint, FileOffset), C_NULL, len_page, prot, flags, fd, offset_page) diff --git a/base/mpfr.jl b/base/mpfr.jl index 95b5b71bc8daa..3f732d6528779 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -9,14 +9,14 @@ export import Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, bessely0, bessely1, ceil, cmp, convert, copysign, deg2rad, - exp, exp2, exponent, factorial, floor, hypot, isinteger, iround, + exp, exp2, exponent, factorial, floor, hypot, isinteger, isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat, prevfloat, promote_rule, rad2deg, rem, round, show, showcompact, sum, sqrt, string, print, trunc, precision, exp10, expm1, - gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai, iceil, ifloor, - itrunc, eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan, + gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai, + eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan, cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2, - serialize, deserialize, cbrt, typemax, typemin, + serialize, deserialize, cbrt, typemax, typemin, unsafe_trunc, realmin, realmax, get_rounding, set_rounding, maxintfloat, widen, significand, frexp @@ -54,6 +54,7 @@ widen(::Type{BigFloat}) = BigFloat BigFloat(x::BigFloat) = x +# convert to BigFloat for (fJ, fC) in ((:si,:Clong), (:ui,:Culong), (:d,:Float64)) @eval begin function BigFloat(x::($fC)) @@ -91,30 +92,75 @@ convert{S}(::Type{BigFloat}, x::Rational{S}) = BigFloat(x) # to resolve ambiguit convert(::Type{BigFloat}, x::Real) = BigFloat(x) convert(::Type{FloatingPoint}, x::BigInt) = BigFloat(x) -for to in (Int8, Int16, Int32, Int64) - @eval begin - function convert(::Type{$to}, x::BigFloat) - (isinteger(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) - convert($to, ccall((:__gmpfr_mpfr_get_sj,:libmpfr), - Cintmax_t, (Ptr{BigFloat}, Int32), &x, 0)) - end - end +## BigFloat -> Integer +function unsafe_cast(::Type{Int64}, x::BigFloat, r::RoundingMode) + ccall((:__gmpfr_mpfr_get_sj,:libmpfr), Cintmax_t, + (Ptr{BigFloat}, Int32), &x, to_mpfr(r)) +end +function unsafe_cast(::Type{UInt64}, x::BigFloat, r::RoundingMode) + ccall((:__gmpfr_mpfr_get_uj,:libmpfr), Cuintmax_t, + (Ptr{BigFloat}, Int32), &x, to_mpfr(r)) +end + +function unsafe_cast{T<:Signed}(::Type{T}, x::BigFloat, r::RoundingMode) + unsafe_cast(Int64, x, r) % T +end +function unsafe_cast{T<:Unsigned}(::Type{T}, x::BigFloat, r::RoundingMode) + unsafe_cast(UInt64, x, r) % T +end + +function unsafe_cast(::Type{BigInt}, x::BigFloat, r::RoundingMode) + # actually safe, just keep naming consistent + z = BigInt() + ccall((:mpfr_get_z, :libmpfr), Int32, (Ptr{BigInt}, Ptr{BigFloat}, Int32), + &z, &x, to_mpfr(r)) + z +end + +unsafe_trunc{T<:Integer}(::Type{T}, x::BigFloat) = unsafe_cast(T,x,RoundToZero) + +function trunc{T<:Union(Signed,Unsigned)}(::Type{T}, x::BigFloat) + (typemin(T) <= x <= typemax(T)) || throw(InexactError()) + unsafe_cast(T,x,RoundToZero) +end +function floor{T<:Union(Signed,Unsigned)}(::Type{T}, x::BigFloat) + (typemin(T) <= x <= typemax(T)) || throw(InexactError()) + unsafe_cast(T,x,RoundDown) +end +function ceil{T<:Union(Signed,Unsigned)}(::Type{T}, x::BigFloat) + (typemin(T) <= x <= typemax(T)) || throw(InexactError()) + unsafe_cast(T,x,RoundUp) end -for to in (UInt8, UInt16, UInt32, UInt64) +trunc(::Type{BigInt}, x::BigFloat) = unsafe_cast(BigInt, x, RoundToZero) +floor(::Type{BigInt}, x::BigFloat) = unsafe_cast(BigInt, x, RoundDown) +ceil(::Type{BigInt}, x::BigFloat) = unsafe_cast(BigInt, x, RoundUp) +# convert/round/trunc/floor/ceil(Integer, x) should return a BigInt +trunc(::Type{Integer}, x::BigFloat) = trunc(BigInt, x) +floor(::Type{Integer}, x::BigFloat) = floor(BigInt, x) +ceil(::Type{Integer}, x::BigFloat) = ceil(BigInt, x) + +for Ti in (Int128,UInt128) @eval begin - function convert(::Type{$to}, x::BigFloat) - (isinteger(x) && (typemin($to) <= x <= typemax($to))) || throw(InexactError()) - convert($to, ccall((:__gmpfr_mpfr_get_uj,:libmpfr), - Cuintmax_t, (Ptr{BigFloat}, Int32), &x, 0)) - end + trunc(::Type{$Ti}, x::BigFloat) = ($Ti)(trunc(BigInt, x)) + floor(::Type{$Ti}, x::BigFloat) = ($Ti)(floor(BigInt, x)) + ceil(::Type{$Ti}, x::BigFloat) = ($Ti)(ceil(BigInt, x)) end end -function Base.BigInt(x::BigFloat) - !isinteger(x) && throw(InexactError()) - return itrunc(x) +convert(::Type{Bool}, x::BigFloat) = (x != 0) +function convert(::Type{BigInt},x::BigFloat) + isinteger(x) || throw(InexactError()) + trunc(BigInt,x) end +Base.BigInt(x::BigFloat) = convert(BigInt,x) + +function convert{T<:Integer}(::Type{T},x::BigFloat) + isinteger(x) || throw(InexactError()) + trunc(T,x) +end + +## BigFloat -> FloatingPoint convert(::Type{Float64}, x::BigFloat) = ccall((:mpfr_get_d,:libmpfr), Float64, (Ptr{BigFloat},Int32), &x, ROUNDING_MODE[end]) convert(::Type{Float32}, x::BigFloat) = @@ -125,8 +171,6 @@ call(::Type{Float64}, x::BigFloat, r::RoundingMode) = call(::Type{Float32}, x::BigFloat, r::RoundingMode) = ccall((:mpfr_get_flt,:libmpfr), Float32, (Ptr{BigFloat},Int32), &x, to_mpfr(r)) -convert(::Type{Integer}, x::BigFloat) = convert(BigInt, x) - promote_rule{T<:Real}(::Type{BigFloat}, ::Type{T}) = BigFloat promote_rule{T<:FloatingPoint}(::Type{BigInt},::Type{T}) = BigFloat promote_rule{T<:FloatingPoint}(::Type{BigFloat},::Type{T}) = BigFloat @@ -653,14 +697,6 @@ for f in (:ceil, :floor, :trunc, :round) end end -function itrunc(x::BigFloat) - z = BigInt() - ccall((:mpfr_get_z, :libmpfr), Int32, (Ptr{BigInt}, Ptr{BigFloat}, Int32), &z, &x, to_mpfr(RoundToZero)) - return z -end - -iround(x::BigFloat) = itrunc(round(x)) - function isinf(x::BigFloat) return ccall((:mpfr_inf_p, :libmpfr), Int32, (Ptr{BigFloat},), &x) != 0 end diff --git a/base/pkg/resolve/maxsum.jl b/base/pkg/resolve/maxsum.jl index 61cc615294b16..504535a9b2c31 100644 --- a/base/pkg/resolve/maxsum.jl +++ b/base/pkg/resolve/maxsum.jl @@ -461,7 +461,7 @@ function maxsum(graph::Graph, msgs::Messages) end if it >= params.nondec_iterations && (it - params.nondec_iterations) % params.dec_interval == 0 - numdec = clamp(ifloor(params.dec_fraction * graph.np), 1, msgs.num_nondecimated) + numdec = clamp(floor(Int,params.dec_fraction * graph.np), 1, msgs.num_nondecimated) decimate(numdec, graph, msgs) if msgs.num_nondecimated == 0 break diff --git a/base/poll.jl b/base/poll.jl index 4f4cfb8113408..9a8aa0bc085a8 100644 --- a/base/poll.jl +++ b/base/poll.jl @@ -250,7 +250,7 @@ function start_watching(t::PollingFileWatcher, interval) associate_julia_struct(t.handle, t) uv_error("start_watching (File)", ccall(:jl_fs_poll_start, Int32, (Ptr{Void},Ptr{UInt8},UInt32), - t.handle, t.file, iround(interval*1000))) + t.handle, t.file, round(UInt32,interval*1000))) end start_watching(f::Function, t::PollingFileWatcher, interval) = (t.cb = f;start_watching(t,interval)) diff --git a/base/primes.jl b/base/primes.jl index c42b62d5e307c..fc9bf4ed0f447 100644 --- a/base/primes.jl +++ b/base/primes.jl @@ -8,7 +8,7 @@ function primesmask(s::AbstractVector{Bool}) n = length(s) n < 2 && return s; s[2] = true n < 3 && return s; s[3] = true - r = ifloor(sqrt(n)) + r = floor(Int,sqrt(n)) for x = 1:r xx = x*x for y = 1:r diff --git a/base/printf.jl b/base/printf.jl index 7bb251bd77f77..b57f26c3c3b99 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -888,7 +888,7 @@ function ini_dec(x::BigInt, n::Int) ccall(:memset, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), p, '0', n - info[2]) return info end - return (n, d, decode_dec(iround(x/big(10)^(d-n)))[3]) + return (n, d, decode_dec(round(BigInt,x/big(10)^(d-n)))[3]) end diff --git a/base/profile.jl b/base/profile.jl index 66eac14921986..6aecdcb979dd4 100644 --- a/base/profile.jl +++ b/base/profile.jl @@ -33,7 +33,7 @@ function init(; n::Union(Void,Integer) = nothing, delay::Union(Void,Float64) = n end function init(n::Integer, delay::Float64) - status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), n, iround(10^9*delay)) + status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), n, round(UInt64,10^9*delay)) if status == -1 error("could not allocate space for ", n, " instruction pointers") end @@ -245,8 +245,8 @@ function print_flat(io::IO, lilist::Vector{LineInfo}, n::Vector{Int}, combine::B wfile = maxfile wfunc = maxfunc else - wfile = ifloor(2*ntext/5) - wfunc = ifloor(3*ntext/5) + wfile = floor(Integer,2*ntext/5) + wfunc = floor(Integer,3*ntext/5) end println(io, lpad("Count", wcounts, " "), " ", rpad("File", wfile, " "), " ", rpad("Function", wfunc, " "), " ", lpad("Line", wline, " ")) for i = 1:length(n) @@ -280,12 +280,12 @@ end tree_format_linewidth(x::LineInfo) = ndigits(x.line)+6 function tree_format(lilist::Vector{LineInfo}, counts::Vector{Int}, level::Int, cols::Integer) - nindent = min(ifloor(cols/2), level) + nindent = min(cols>>1, level) ndigcounts = ndigits(maximum(counts)) ndigline = maximum([tree_format_linewidth(x) for x in lilist]) ntext = cols-nindent-ndigcounts-ndigline-5 - widthfile = ifloor(0.4ntext) - widthfunc = ifloor(0.6ntext) + widthfile = floor(Integer,0.4ntext) + widthfunc = floor(Integer,0.6ntext) strs = Array(ByteString, length(lilist)) showextra = false if level > nindent diff --git a/base/range.jl b/base/range.jl index 7c21201509bff..75ed0e2fec1a7 100644 --- a/base/range.jl +++ b/base/range.jl @@ -105,7 +105,7 @@ function rat(x) b = c = 0 m = maxintfloat(Float32) while abs(y) <= m - f = itrunc(y) + f = trunc(Int,y) y -= f a, c = f*a + c, a b, d = f*b + d, b @@ -415,11 +415,11 @@ function _findin{T1<:Integer, T2<:Integer}(r::Range{T1}, span::UnitRange{T2}) lr = last(r) sr = step(r) if sr > 0 - ifirst = fr >= fspan ? 1 : iceil((fspan-fr)/sr)+1 - ilast = lr <= lspan ? length(r) : length(r) - iceil((lr-lspan)/sr) + ifirst = fr >= fspan ? 1 : ceil(Integer,(fspan-fr)/sr)+1 + ilast = lr <= lspan ? length(r) : length(r) - ceil(Integer,(lr-lspan)/sr) elseif sr < 0 - ifirst = fr <= lspan ? 1 : iceil((lspan-fr)/sr)+1 - ilast = lr >= fspan ? length(r) : length(r) - iceil((lr-fspan)/sr) + ifirst = fr <= lspan ? 1 : ceil(Integer,(lspan-fr)/sr)+1 + ilast = lr >= fspan ? length(r) : length(r) - ceil(Integer,(lr-fspan)/sr) else ifirst = fr >= fspan ? 1 : length(r)+1 ilast = fr <= lspan ? length(r) : 0 @@ -557,7 +557,7 @@ function map!(f::Callable, dest, r::Range) end function in(x, r::Range) - n = step(r) == 0 ? 1 : iround((x-first(r))/step(r))+1 + n = step(r) == 0 ? 1 : round(Integer,(x-first(r))/step(r))+1 n >= 1 && n <= length(r) && r[n] == x end diff --git a/base/rational.jl b/base/rational.jl index 8f16c89c01948..63f901a89b5b1 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -220,26 +220,26 @@ for op in (:div, :fld, :cld) end end -itrunc(x::Rational) = div(x.num,x.den) -ifloor(x::Rational) = fld(x.num,x.den) -iceil (x::Rational) = cld(x.num,x.den) -function iround(x::Rational) - t = itrunc(x) +trunc{T<:Integer}(::Type{T}, x::Rational) = convert(T,div(x.num,x.den)) +floor{T<:Integer}(::Type{T}, x::Rational) = convert(T,fld(x.num,x.den)) +ceil {T<:Integer}(::Type{T}, x::Rational) = convert(T,cld(x.num,x.den)) +function round{T<:Integer}(::Type{T}, x::Rational) + t = trunc(T,x) r = x-t abs(r.num) > (r.den-one(r.den))>>1 ? t + copysign(one(t),x) : t end -trunc(x::Rational) = Rational(itrunc(x)) -floor(x::Rational) = Rational(ifloor(x)) -ceil (x::Rational) = Rational(iceil(x)) -round(x::Rational) = Rational(iround(x)) +trunc{T}(x::Rational{T}) = Rational(trunc(T,x)) +floor{T}(x::Rational{T}) = Rational(floor(T,x)) +ceil {T}(x::Rational{T}) = Rational(ceil(T,x)) +round{T}(x::Rational{T}) = Rational(round(T,x)) ## rational to int coercion ## for f in (:int8, :int16, :int32, :int64, :int128, :uint8, :uint16, :uint32, :uint64, :uint128, :signed, :integer, :unsigned, :int, :uint) - @eval ($f)(x::Rational) = ($f)(iround(x)) + @eval ($f)(x::Rational) = ($f)(round(Integer,x)) end function ^(x::Rational, n::Integer) diff --git a/base/sort.jl b/base/sort.jl index 542646b563f6b..4015816555aa4 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -180,7 +180,7 @@ function searchsortedlast{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, x, first(a)) ? 0 : length(a) else - n = max(min(iround((x-first(a))/step(a))+1,length(a)),1) + n = max(min(round(Integer,(x-first(a))/step(a))+1,length(a)),1) lt(o, x, a[n]) ? n-1 : n end end @@ -189,7 +189,7 @@ function searchsortedfirst{T<:Real}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, first(a), x) ? length(a)+1 : 1 else - n = max(min(iround((x-first(a))/step(a))+1,length(a)),1) + n = max(min(round(Integer,(x-first(a))/step(a))+1,length(a)),1) lt(o, a[n] ,x) ? n+1 : n end end @@ -198,7 +198,7 @@ function searchsortedlast{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, x, first(a)) ? 0 : length(a) else - max(min(fld(ifloor(x)-first(a),step(a))+1,length(a)),0) + max(min(fld(floor(Integer,x)-first(a),step(a))+1,length(a)),0) end end @@ -206,7 +206,7 @@ function searchsortedfirst{T<:Integer}(a::Range{T}, x::Real, o::DirectOrdering) if step(a) == 0 lt(o, first(a), x) ? length(a)+1 : 1 else - max(min(-fld(ifloor(-x)+first(a),step(a))+1,length(a)+1),1) + max(min(-fld(floor(Integer,-x)+first(a),step(a))+1,length(a)+1),1) end end diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 2760b67ef3a49..35b030bd50590 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -433,24 +433,46 @@ end # Operations that may map nonzeros to zero, and zero to zero # Result is sparse -for (op, restype) in ((:iceil, Int), (:ceil, Void), - (:ifloor, Int), (:floor, Void), - (:itrunc, Int), (:trunc, Void), - (:iround, Int), (:round, Void), - (:sin, Void), (:tan, Void), - (:sinh, Void), (:tanh, Void), - (:asin, Void), (:atan, Void), - (:asinh, Void), (:atanh, Void), - (:sinpi, Void), (:cosc, Void), - (:sind, Void), (:tand, Void), - (:asind, Void), (:atand, Void) ) +for op in (:ceil, :floor, :trunc, :round, + :sin, :tan, :asin, :atan, + :sinh, :tanh, :asinh, :atanh, + :sinpi, :cosc, + :sind, :tand, :asind, :atand) @eval begin - function ($op){Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}) nfilledA = nnz(A) colptrB = Array(Ti, A.n+1) rowvalB = Array(Ti, nfilledA) - nzvalB = Array($(restype==Void ? (:Tv) : restype), nfilledA) + nzvalB = Array(Tv, nfilledA) + + k = 0 # number of additional zeros introduced by op(A) + @inbounds for i = 1 : A.n + colptrB[i] = A.colptr[i] - k + for j = A.colptr[i] : A.colptr[i+1]-1 + opAj = $(op)(A.nzval[j]) + if opAj == 0 + k += 1 + else + rowvalB[j - k] = A.rowval[j] + nzvalB[j - k] = opAj + end + end + end + colptrB[end] = A.colptr[end] - k + deleteat!(rowvalB, colptrB[end]:nfilledA) + deleteat!(nzvalB, colptrB[end]:nfilledA) + return SparseMatrixCSC(A.m, A.n, colptrB, rowvalB, nzvalB) + end + end # quote +end # macro + +for op in (:ceil, :floor, :trunc, :round) + @eval begin + function ($op){T,Tv,Ti}(::Type{T},A::SparseMatrixCSC{Tv,Ti}) + nfilledA = nnz(A) + colptrB = Array(Ti, A.n+1) + rowvalB = Array(Ti, nfilledA) + nzvalB = Array(T, nfilledA) k = 0 # number of additional zeros introduced by op(A) @inbounds for i = 1 : A.n @@ -474,6 +496,8 @@ for (op, restype) in ((:iceil, Int), (:ceil, Void), end # quote end # macro + + # Operations that map nonzeros to nonzeros, and zeros to zeros # Result is sparse for op in (:-, :abs, :abs2, :log1p, :expm1) diff --git a/base/special/gamma.jl b/base/special/gamma.jl index 09f40f62f06a8..a7438e76afbfb 100644 --- a/base/special/gamma.jl +++ b/base/special/gamma.jl @@ -80,7 +80,7 @@ function digamma(z::Union(Float64,Complex{Float64})) end if x < 7 # shift using recurrence formula - n = 7 - ifloor(x) + n = 7 - floor(Int,x) for ν = 1:n-1 ψ -= inv(z + ν) end @@ -103,7 +103,7 @@ function trigamma(z::Union(Float64,Complex{Float64})) ψ = zero(z) if x < 8 # shift using recurrence formula - n = 8 - ifloor(x) + n = 8 - floor(Int,x) ψ += inv(z)^2 for ν = 1:n-1 ψ += inv(z + ν)^2 @@ -285,7 +285,7 @@ function zeta(s::Union(Int,Float64,Complex{Float64}), throw(DomainError()) # or return NaN? end nx = int(xf) - n = iceil(cutoff - nx) + n = ceil(Int,cutoff - nx) ζ += inv_oftype(ζ, z)^s for ν = -nx:-1:1 ζₒ= ζ @@ -437,7 +437,7 @@ function zeta(s::Union(Float64,Complex{Float64})) # shift using recurrence formula: # n is a semi-empirical cutoff for the Stirling series, based # on the error term ~ (|m|/n)^18 / n^real(m) - n = iceil(6 + 0.7*abs(imag(s-1))^inv(1 + real(m)*0.05)) + n = ceil(Int,6 + 0.7*abs(imag(s-1))^inv(1 + real(m)*0.05)) ζ = one(s) for ν = 2:n ζₒ= ζ diff --git a/base/statistics.jl b/base/statistics.jl index 05f2763f18350..b7400429d7f34 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -490,8 +490,8 @@ function quantile!(v::AbstractVector, q::AbstractVector) lq = length(q) index = 1 .+ (lv-1)*q - lo = ifloor(index) - hi = iceil(index) + lo = floor(Int,index) + hi = ceil(Int,index) sort!(v) isnan(v[end]) && error("quantiles are undefined in presence of NaNs") i = find(index .> lo) @@ -537,7 +537,7 @@ function histrange{T<:FloatingPoint,N}(v::AbstractArray{T,N}, n::Integer) end end start = step*(ceil(lo/step)-1) - nm1 = iceil((hi - start)/step) + nm1 = ceil(Int,(hi - start)/step) start:step:(start + nm1*step) end @@ -550,7 +550,7 @@ function histrange{T<:Integer,N}(v::AbstractArray{T,N}, n::Integer) step = 1 else bw = (hi - lo) / n - e = 10^max(0,ifloor(log10(bw))) + e = 10^max(0,floor(Int,log10(bw))) r = bw / e if r <= 1 step = e @@ -563,7 +563,7 @@ function histrange{T<:Integer,N}(v::AbstractArray{T,N}, n::Integer) end end start = step*(ceil(lo/step)-1) - nm1 = iceil((hi - start)/step) + nm1 = ceil(Int,(hi - start)/step) start:step:(start + nm1*step) end @@ -574,7 +574,7 @@ midpoints(v::AbstractVector) = [0.5*(v[i] + v[i+1]) for i in 1:length(v)-1] ## hist ## function sturges(n) # Sturges' formula n==0 && return one(n) - iceil(log2(n))+1 + ceil(Int,log2(n))+1 end function hist!{HT}(h::AbstractArray{HT}, v::AbstractVector, edg::AbstractVector; init::Bool=true) diff --git a/doc/helpdb.jl b/doc/helpdb.jl index 79e5792457ea5..38e1ad01c9e52 100644 --- a/doc/helpdb.jl +++ b/doc/helpdb.jl @@ -4508,63 +4508,63 @@ popdisplay(d::Display) "), -("Base","round","round(x[, digits[, base]]) +("Base","round","round([T], x[, digits[, base]]) \"round(x)\" returns the nearest integral value of the same type as - \"x\" to \"x\". \"round(x, digits)\" rounds to the specified number - of digits after the decimal place, or before if negative, e.g., - \"round(pi,2)\" is \"3.14\". \"round(x, digits, base)\" rounds - using a different base, defaulting to 10, e.g., \"round(pi, 1, 8)\" - is \"3.125\". + \"x\" to \"x\", breaking ties by rounding away from zero. -"), - -("Base","ceil","ceil(x[, digits[, base]]) + \"round(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. - Returns the nearest integral value of the same type as \"x\" not - less than \"x\". \"digits\" and \"base\" work as above. + \"round(x, digits)\" rounds to the specified number of digits after + the decimal place, or before if negative, e.g., \"round(pi,2)\" is + \"3.14\". \"round(x, digits, base)\" rounds using a different base, + defaulting to 10, e.g., \"round(pi, 1, 8)\" is \"3.125\". "), -("Base","floor","floor(x[, digits[, base]]) +("Base","ceil","ceil([T], x[, digits[, base]]) - Returns the nearest integral value of the same type as \"x\" not - greater than \"x\". \"digits\" and \"base\" work as above. + \"ceil(x)\" returns the nearest integral value of the same type as + \"x\" that is greater than or equal to \"x\". -"), + \"ceil(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. -("Base","trunc","trunc(x[, digits[, base]]) - - Returns the nearest integral value of the same type as \"x\" not - greater in magnitude than \"x\". \"digits\" and \"base\" work as - above. + \"digits\" and \"base\" work as for \"round\". "), -("Base","iround","iround([T], x) -> Integer - - Returns the nearest integer to \"x\", converted to an integer type, - optionally passed as the first argument. +("Base","floor","floor([T], x[, digits[, base]]) -"), + \"floor(x)\" returns the nearest integral value of the same type as + \"x\" that is less than or equal to \"x\". -("Base","iceil","iceil(x) -> Integer + \"floor(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. - Returns the nearest integer not less than \"x\". + \"digits\" and \"base\" work as above. "), -("Base","ifloor","ifloor(x) -> Integer +("Base","trunc","trunc([T], x[, digits[, base]]) + + \"trunc(x)\" returns the nearest integral value of the same type as + \"x\" whose absolute value is less than or equal to \"x\". + + \"trunc(T, x)\" converts the result to type \"T\", throwing an + \"InexactError\" if the value is not representable. - Returns the nearest integer not greater than \"x\". + \"digits\" and \"base\" work as above. "), -("Base","itrunc","itrunc([T], x) -> Integer +("Base","unsafe_trunc","unsafe_trunc(T, x) - Returns the nearest integer not greater in magnitude than \"x\", - converted to an integer type, optionally passed as the first - argument. + \"unsafe_trunc(T, x)\" returns the nearest integral value of type + \"T\" whose absolute value is less than or equal to \"x\". If the + value is not representable by \"T\", an arbitrary value will be + returned. "), @@ -5854,16 +5854,13 @@ popdisplay(d::Display) "), -("Base","rand!","rand!([rng], A) - - Populate the array A with random values. - -"), - -("Base","rand!","rand!([rng], r, A) +("Base","rand!","rand!([rng], A[, coll]) - Populate the array A with random values drawn uniformly from the - range \"r\". + Populate the array A with random values. If the indexable + collection \"coll\" is specified, the values are picked randomly + from \"coll\". This is equivalent to \"copy!(A, rand(rng, coll, + size(A)))\" or \"copy!(A, rand(rng, eltype(A), size(A)))\" but + without allocating a new array. "), @@ -5913,6 +5910,35 @@ popdisplay(d::Display) "), +("Base","eachindex","eachindex(A) + + Creates an iterable object for visiting each multi-dimensional + index of the AbstractArray \"A\". Example for a 2-d array: + + julia> A = rand(2,3) + 2x3 Array{Float64,2}: + 0.960084 0.629326 0.625155 + 0.432588 0.955903 0.991614 + + julia> for iter in eachindex(A) + @show iter.I_1, iter.I_2 + @show A[iter] + end + (iter.I_1,iter.I_2) = (1,1) + A[iter] = 0.9600836263003063 + (iter.I_1,iter.I_2) = (2,1) + A[iter] = 0.4325878255452178 + (iter.I_1,iter.I_2) = (1,2) + A[iter] = 0.6293256402775211 + (iter.I_1,iter.I_2) = (2,2) + A[iter] = 0.9559027084099654 + (iter.I_1,iter.I_2) = (1,3) + A[iter] = 0.6251548453735303 + (iter.I_1,iter.I_2) = (2,3) + A[iter] = 0.9916142534546522 + +"), + ("Base","countnz","countnz(A) Counts the number of nonzero values in array A (dense or sparse). diff --git a/doc/manual/arrays.rst b/doc/manual/arrays.rst index ba3290c95297f..e318e86e1db5c 100644 --- a/doc/manual/arrays.rst +++ b/doc/manual/arrays.rst @@ -352,7 +352,6 @@ elementwise:: exponent eta zeta gamma hankelh1 hankelh2 ceil floor round trunc - iceil ifloor iround itrunc isfinite isinf isnan lbeta lfact lgamma log log10 log1p log2 diff --git a/doc/manual/mathematical-operations.rst b/doc/manual/mathematical-operations.rst index f1f89956d5796..160d4396fc159 100644 --- a/doc/manual/mathematical-operations.rst +++ b/doc/manual/mathematical-operations.rst @@ -362,14 +362,14 @@ Rounding functions ============= ================================== ================= Function Description Return type ============= ================================== ================= -``round(x)`` round ``x`` to the nearest integer ``FloatingPoint`` -``iround(x)`` round ``x`` to the nearest integer ``Integer`` -``floor(x)`` round ``x`` towards ``-Inf`` ``FloatingPoint`` -``ifloor(x)`` round ``x`` towards ``-Inf`` ``Integer`` -``ceil(x)`` round ``x`` towards ``+Inf`` ``FloatingPoint`` -``iceil(x)`` round ``x`` towards ``+Inf`` ``Integer`` -``trunc(x)`` round ``x`` towards zero ``FloatingPoint`` -``itrunc(x)`` round ``x`` towards zero ``Integer`` +``round(x)`` round ``x`` to the nearest integer ``typeof(x)`` +``round(T, x)`` round ``x`` to the nearest integer ``T`` +``floor(x)`` round ``x`` towards ``-Inf`` ``typeof(x)`` +``floor(T, x)`` round ``x`` towards ``-Inf`` ``T`` +``ceil(x)`` round ``x`` towards ``+Inf`` ``typeof(x)`` +``ceil(T, x)`` round ``x`` towards ``+Inf`` ``T`` +``trunc(x)`` round ``x`` towards zero ``typeof(x)`` +``trunc(T, x)`` round ``x`` towards zero ``T`` ============= ================================== ================= Division functions diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 0b7f7a77aa979..6a130fea21b44 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -3174,38 +3174,52 @@ Mathematical Functions Accurately compute :math:`e^x-1` -.. function:: round(x, [digits, [base]]) +.. function:: round([T,] x, [digits, [base]]) - ``round(x)`` returns the nearest integral value of the same type as ``x`` to ``x``. ``round(x, digits)`` rounds to the specified number of digits after the decimal place, or before if negative, e.g., ``round(pi,2)`` is ``3.14``. ``round(x, digits, base)`` rounds using a different base, defaulting to 10, e.g., ``round(pi, 1, 8)`` is ``3.125``. + ``round(x)`` returns the nearest integral value of the same type as ``x`` + to ``x``, breaking ties by rounding away from zero. -.. function:: ceil(x, [digits, [base]]) + ``round(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integral value of the same type as ``x`` not less than ``x``. ``digits`` and ``base`` work as above. + ``round(x, digits)`` rounds to the specified number of digits after the decimal place, or before if negative, e.g., ``round(pi,2)`` is ``3.14``. ``round(x, digits, base)`` rounds using a different base, defaulting to 10, e.g., ``round(pi, 1, 8)`` is ``3.125``. -.. function:: floor(x, [digits, [base]]) +.. function:: ceil([T,] x, [digits, [base]]) - Returns the nearest integral value of the same type as ``x`` not greater than ``x``. ``digits`` and ``base`` work as above. + ``ceil(x)`` returns the nearest integral value of the same type as ``x`` + that is greater than or equal to ``x``. -.. function:: trunc(x, [digits, [base]]) + ``ceil(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integral value of the same type as ``x`` not greater in magnitude than ``x``. ``digits`` and ``base`` work as above. + ``digits`` and ``base`` work as for ``round``. -.. function:: iround([T,]x) -> Integer +.. function:: floor([T,] x, [digits, [base]]) - Returns the nearest integer to ``x``, converted to an integer type, optionally passed as the first argument. + ``floor(x)`` returns the nearest integral value of the same type as ``x`` + that is less than or equal to ``x``. -.. function:: iceil(x) -> Integer + ``floor(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integer not less than ``x``. + ``digits`` and ``base`` work as above. -.. function:: ifloor(x) -> Integer +.. function:: trunc([T,] x, [digits, [base]]) - Returns the nearest integer not greater than ``x``. + ``trunc(x)`` returns the nearest integral value of the same type as ``x`` whose absolute + value is less than or equal to ``x``. -.. function:: itrunc([T,]x) -> Integer + ``trunc(T, x)`` converts the result to type ``T``, throwing an + ``InexactError`` if the value is not representable. - Returns the nearest integer not greater in magnitude than ``x``, converted to an integer type, optionally passed as the first argument. + ``digits`` and ``base`` work as above. +.. function:: unsafe_trunc(T, x) + + ``unsafe_trunc(T, x)`` returns the nearest integral value of type ``T`` whose absolute + value is less than or equal to ``x``. If the value is not representable by + ``T``, an arbitrary value will be returned. + .. function:: signif(x, digits, [base]) Rounds (in the sense of ``round``) ``x`` so that there are ``digits`` significant digits, under a base ``base`` representation, default 10. E.g., ``signif(123.456, 2)`` is ``120.0``, and ``signif(357.913, 4, 2)`` is ``352.0``. diff --git a/src/intrinsics.cpp b/src/intrinsics.cpp index 4e6a478722e15..af69094d14b0b 100644 --- a/src/intrinsics.cpp +++ b/src/intrinsics.cpp @@ -21,7 +21,7 @@ namespace JL_I { fptoui, fptosi, uitofp, sitofp, fptrunc, fpext, // checked conversion - fpsiround, fpuiround, checked_fptosi, checked_fptoui, + checked_fptosi, checked_fptoui, checked_trunc_sint, checked_trunc_uint, check_top_bit, // checked arithmetic checked_sadd, checked_uadd, checked_ssub, checked_usub, @@ -577,115 +577,6 @@ static Value *emit_checked_fptoui(jl_value_t *targ, Value *x, jl_codectx_t *ctx) return emit_checked_fptoui(staticeval_bitstype(targ, "checked_fptoui", ctx), x, ctx); } -static Value *emit_iround(Type *to, Value *x, bool issigned, jl_codectx_t *ctx) -{ - int nmantissa, expoffs, expbits; - int64_t topbit; - Type *intt, *floatt; - Value *bits = JL_INT(x); - Value *max, *min; - int tobits = to->getPrimitiveSizeInBits(); - - if (bits->getType()->getPrimitiveSizeInBits() == 32) { - nmantissa = 23; - expoffs = 127; - expbits = 0xff; - topbit = BIT31; - intt = T_int32; floatt = T_float32; - if (issigned) { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 127.99999); - min = ConstantFP::get(floatt, -128.99998); break; - case 16: max = ConstantFP::get(floatt, 32767.998); - min = ConstantFP::get(floatt, -32768.996); break; - case 32: max = ConstantFP::get(floatt, 2.1474835e9); - min = ConstantFP::get(floatt, -2.1474836e9); break; - case 64: max = ConstantFP::get(floatt, 9.2233715e18); - min = ConstantFP::get(floatt, -9.223372e18); break; - default: - jl_error("unsupported type in fpsiround"); - } - } - else { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 255.99998); break; - case 16: max = ConstantFP::get(floatt, 65535.996); break; - case 32: max = ConstantFP::get(floatt, 4.294967e9); break; - case 64: max = ConstantFP::get(floatt, 1.8446743e19); break; - default: - jl_error("unsupported type in fpuiround"); - } - // most negative number that truncates to zero - min = ConstantFP::get(floatt, -0.99999994); - } - } - else { - nmantissa = 52; - expoffs = 1023; - expbits = 0x7ff; - topbit = BIT63; - intt = T_int64; floatt = T_float64; - if (issigned) { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 127.99999999999999); - min = ConstantFP::get(floatt, -128.99999999999997); break; - case 16: max = ConstantFP::get(floatt, 32767.999999999996); - min = ConstantFP::get(floatt, -32768.99999999999); break; - case 32: max = ConstantFP::get(floatt, 2.1474836479999998e9); - min = ConstantFP::get(floatt, -2.1474836489999995e9); break; - case 64: max = ConstantFP::get(floatt, 9.223372036854775e18); - min = ConstantFP::get(floatt, -9.223372036854776e18); break; - default: - jl_error("unsupported type in fpsiround"); - } - } - else { - switch (tobits) { - case 8: max = ConstantFP::get(floatt, 255.99999999999997); break; - case 16: max = ConstantFP::get(floatt, 65535.99999999999); break; - case 32: max = ConstantFP::get(floatt, 4.2949672959999995e9); break; - case 64: max = ConstantFP::get(floatt, 1.844674407370955e19); break; - default: - jl_error("unsupported type in fpuiround"); - } - min = ConstantFP::get(floatt, -0.9999999999999999); - } - } - - // itrunc(x + copysign(0.5,x)) - // values with exponent >= nbits are already integers, and this - // rounding method doesn't always give the right answer there. - x = FP(x); - Value *expo = builder.CreateAShr(bits, ConstantInt::get(intt,nmantissa)); - expo = builder.CreateAnd(expo, ConstantInt::get(intt,expbits)); - Value *isint = builder.CreateICmpSGE(expo, - ConstantInt::get(intt,expoffs+nmantissa)); - Value *half = builder.CreateBitCast(ConstantFP::get(floatt, 0.5), intt); - Value *signedhalf = - builder.CreateOr(half, - builder.CreateAnd(bits, - ConstantInt::get(intt,topbit))); - Value *sum = builder.CreateFAdd(x, - builder.CreateBitCast(signedhalf, floatt)); - - Value *src = builder. - CreateSelect(builder. - CreateOr(isint, builder. - // need to give 0 for -0.5 < x < 0.5 (exponent < -1) - // otherwise iround(prevfloat(0.5)) == 1 - CreateICmpSLT(expo, - ConstantInt::get(intt,expoffs-1))), - x, sum); - - raise_exception_unless(builder.CreateAnd(builder.CreateFCmpOLE(src, max), - builder.CreateFCmpOGE(src, min)), - prepare_global(jlinexacterr_var), ctx); - if (issigned) - return builder.CreateFPToSI(src, to); - else - return builder.CreateFPToUI(src, to); -} - static Value *emit_runtime_pointerref(jl_value_t *e, jl_value_t *i, jl_codectx_t *ctx) { Value *preffunc = @@ -924,20 +815,6 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs, jl_error("fptosi: wrong number of arguments"); } - case fpsiround: - case fpuiround: - if (nargs == 1) { - Value *x = FP(auto_unbox(args[1], ctx)); - return emit_iround(JL_INTT(x->getType()), x, f == fpsiround, ctx); - } - else if (nargs == 2) { - return emit_iround(Type::getIntNTy(jl_LLVMContext, try_to_determine_bitstype_nbits(args[1],ctx)), - FP(auto_unbox(args[2],ctx)), f == fpsiround, ctx); - } - else { - jl_errorf("%s: wrong number of arguments", f == fpsiround ? "fpsiround" : "fpuiround"); - } - HANDLE(fptrunc,2) return builder.CreateFPTrunc(FP(auto_unbox(args[2],ctx)), FTnbits(try_to_determine_bitstype_nbits(args[1],ctx))); HANDLE(fpext,2) { Value *x = auto_unbox(args[2],ctx); @@ -1365,7 +1242,6 @@ extern "C" void jl_init_intrinsic_functions(void) ADD_I(ctpop_int); ADD_I(ctlz_int); ADD_I(cttz_int); ADD_I(sext_int); ADD_I(zext_int); ADD_I(trunc_int); ADD_I(fptoui); ADD_I(fptosi); - ADD_I(fpsiround); ADD_I(fpuiround); ADD_I(uitofp); ADD_I(sitofp); ADD_I(fptrunc); ADD_I(fpext); ADD_I(abs_float); ADD_I(copysign_float); diff --git a/test/arrayperf.jl b/test/arrayperf.jl index 10d1a849a5f7d..f2931a16747cd 100644 --- a/test/arrayperf.jl +++ b/test/arrayperf.jl @@ -11,7 +11,7 @@ if run_ref sz = ntuple(n_dims,i->lensmall) A = randn(sz) n_el = prod(sz) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r @@ -24,7 +24,7 @@ if run_ref sz = ntuple(n_dims,i->lenbig[n_dims]) A = randn(sz) n_el = prod(sz) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r @@ -40,7 +40,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -53,7 +53,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -69,7 +69,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -82,7 +82,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -96,7 +96,7 @@ if run_ref println("Small arrays:") function randind(len) i = rand(1:6) - indchoices = {1:len,1:iceil(len/2),1:iceil(3*len/4),2:2:len,1:iceil(len/2):len,len:-1:1} + indchoices = {1:len,1:ceil(Int,len/2),1:ceil(Int,3*len/4),2:2:len,1:ceil(Int,len/2):len,len:-1:1} return indchoices[i] end #indsmall = {1:4,1:2,1:3,2:2:4,1:3:4,4:-1:1} @@ -105,7 +105,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i->randind(sz[i])) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) @@ -118,7 +118,7 @@ if run_ref A = randn(sz) ind = ntuple(n_dims,i->randind(sz[i])) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) @@ -135,7 +135,7 @@ if run_assign sz = ntuple(n_dims,i->lensmall) B = zeros(sz) A = randn(sz) - n_r = iceil(n_evals/prod(sz)) + n_r = ceil(Int,n_evals/prod(sz)) print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r B[:] = A @@ -146,7 +146,7 @@ if run_assign sz = ntuple(n_dims,i->lenbig[n_dims]) B = zeros(sz) A = randn(sz) - n_r = iceil(n_evals/prod(sz)) + n_r = ceil(Int,n_evals/prod(sz)) print(n_dims, " dimensions (", n_r, " repeats): ") @time for i = 1:n_r B[:] = A @@ -162,7 +162,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -177,7 +177,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -195,7 +195,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -210,7 +210,7 @@ if run_assign ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) @@ -225,7 +225,7 @@ if run_assign println("Small arrays:") function randind(len) i = rand(1:6) - indchoices = {1:len,1:iceil(len/2),1:iceil(3*len/4),2:2:len,1:iceil(len/2):len,len:-1:1} + indchoices = {1:len,1:ceil(Int,len/2),1:ceil(Int,3*len/4),2:2:len,1:ceil(Int,len/2):len,len:-1:1} return indchoices[i] end #indsmall = {1:4,1:2,1:3,2:2:4,1:3:4,4:-1:1} @@ -235,7 +235,7 @@ if run_assign ind = ntuple(n_dims,i->randind(sz[i])) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) @@ -250,7 +250,7 @@ if run_assign ind = ntuple(n_dims,i->randind(sz[i])) A = randn(map(length,ind)) n_el = prod(map(length,ind)) - n_r = iceil(n_evals/n_el) + n_r = ceil(Int,n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r # ind = ntuple(n_dims,i->randind(sz[i])) diff --git a/test/complex.jl b/test/complex.jl index 9c03aef856d51..1fcd6e0d2433c 100644 --- a/test/complex.jl +++ b/test/complex.jl @@ -704,7 +704,7 @@ function logacc(x::Float64,expected::Float64) (x == Inf || x == -Inf) && (return 0) isnan(x) && (return 0) ra = relacc(BigFloat(x),BigFloat(expected)) - max(ifloor(-log2(ra)),0) + max(floor(Int,-log2(ra)),0) end # the robust division algorithm should have 53 or 52 # bits accuracy for each of the hard divisions diff --git a/test/euler.jl b/test/euler.jl index 04fd4f3b29c9b..525cf7db6332b 100644 --- a/test/euler.jl +++ b/test/euler.jl @@ -46,7 +46,7 @@ end @test sum(1:100)^2 - sum((1:100).^2) == 25164150 #7: 104743 -euler7(n) = primes(ifloor(n*log(n*log(n))))[n] +euler7(n) = primes(floor(Int,n*log(n*log(n))))[n] @test euler7(10001) == 104743 #8: 40824 diff --git a/test/file.jl b/test/file.jl index 7d5df16beb5f6..bb373297f3f1e 100644 --- a/test/file.jl +++ b/test/file.jl @@ -96,7 +96,7 @@ rm(c_tmpdir, recursive=true) # This section tests file watchers. # ####################################################################### function test_file_poll(channel,timeout_s) - rc = poll_file(file, iround(timeout_s/10), timeout_s) + rc = poll_file(file, round(Int,timeout_s/10), timeout_s) put!(channel,rc) end diff --git a/test/linalg/triangular.jl b/test/linalg/triangular.jl index 6bbafe1740082..bbb8be43c1734 100644 --- a/test/linalg/triangular.jl +++ b/test/linalg/triangular.jl @@ -210,7 +210,7 @@ for eltya in (Float32, Float64, Complex64, Complex128, BigFloat, Int) end debug && println("Test forward error [JIN 5705] if this is not a BigFloat") - b = eltyb == Int ? itrunc(Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2)) + b = eltyb == Int ? trunc(Int,Atri*ones(n, 2)) : convert(Matrix{eltyb}, Atri*ones(eltya, n, 2)) x = Atri \ b γ = n*ε/(1 - n*ε) if eltya != BigFloat diff --git a/test/mpfr.jl b/test/mpfr.jl index 7a05b2528d929..6d7a9f0d14eb8 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -345,21 +345,21 @@ y = BigFloat(42) @test convert(UInt32, y) == 42 @test convert(UInt32, y) == 42 -# iround +# round x = BigFloat(42.42) y = with_bigfloat_precision(256) do BigFloat("9223372036854775809.2324") end z = BigInt("9223372036854775809") -@test iround(x) == 42 -@test iround(y) == z -@test typeof(iround(UInt8, x)) == UInt8 && iround(UInt8, x) == 0x2a -@test typeof(iround(UInt16, x)) == UInt16 && iround(UInt16, x) == 0x2a -@test typeof(iround(UInt32, x)) == UInt32 && iround(UInt32, x) == 0x2a -@test typeof(iround(UInt64, x)) == UInt64 && iround(UInt64, x) == 0x2a -@test typeof(iround(Int64, x)) == Int64 && iround(Int64, x) == 42 -@test typeof(iround(Int, x)) == Int && iround(Int, x) == 42 -@test typeof(iround(UInt, x)) == UInt && iround(UInt, x) == 0x2a +@test round(Integer,x) == 42 +@test round(Integer,y) == z +@test typeof(round(UInt8, x)) == UInt8 && round(UInt8, x) == 0x2a +@test typeof(round(UInt16, x)) == UInt16 && round(UInt16, x) == 0x2a +@test typeof(round(UInt32, x)) == UInt32 && round(UInt32, x) == 0x2a +@test typeof(round(UInt64, x)) == UInt64 && round(UInt64, x) == 0x2a +@test typeof(round(Int64, x)) == Int64 && round(Int64, x) == 42 +@test typeof(round(Int, x)) == Int && round(Int, x) == 42 +@test typeof(round(UInt, x)) == UInt && round(UInt, x) == 0x2a # string representation str = "1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012e+00" @@ -445,7 +445,7 @@ with_bigfloat_precision(53) do @test ldexp(BigFloat(24.5), 0x48) == ldexp(24.5, 72) end -# ceil / iceil / floor / ifloor / trunc / itrunc +# ceil / floor / trunc x = BigFloat("28273.7312487489135135135") y = BigInt(28273) z = BigInt(28274) @@ -459,75 +459,90 @@ c = BigInt("123456789012345678901234567891") @test trunc(x) == y @test typeof(trunc(x)) == BigFloat -@test iceil(x) == z -@test typeof(iceil(x)) == BigInt -@test ifloor(x) == y -@test typeof(ifloor(x)) == BigInt -@test itrunc(x) == y -@test typeof(itrunc(x)) == BigInt - -@test iceil(Int64, x) == int64(z) -@test typeof(iceil(Int64, x)) == Int64 -@test ifloor(Int64, x) == int64(y) -@test typeof(ifloor(Int64, x)) == Int64 -@test itrunc(Int64, x) == int64(y) -@test typeof(itrunc(Int64, x)) == Int64 - -@test iceil(Int32, x) == int32(z) -@test typeof(iceil(Int32, x)) == Int32 -@test ifloor(Int32, x) == int32(y) -@test typeof(ifloor(Int32, x)) == Int32 -@test itrunc(Int32, x) == int32(y) -@test typeof(itrunc(Int32, x)) == Int32 - -@test iceil(Int16, x) == int16(z) -@test typeof(iceil(Int16, x)) == Int16 -@test ifloor(Int16, x) == int16(y) -@test typeof(ifloor(Int16, x)) == Int16 -@test itrunc(Int16, x) == int16(y) -@test typeof(itrunc(Int16, x)) == Int16 - -#@test iceil(Int8, x) == int8(z) -#@test typeof(iceil(Int8, x)) == Int8 -#@test ifloor(Int8, x) == int8(y) -#@test typeof(ifloor(Int8, x)) == Int8 -#@test itrunc(Int8, x) == int8(y) -#@test typeof(itrunc(Int8, x)) == Int8 - -@test iceil(UInt64, x) == uint64(z) -@test typeof(iceil(UInt64, x)) == UInt64 -@test ifloor(UInt64, x) == uint64(y) -@test typeof(ifloor(UInt64, x)) == UInt64 -@test itrunc(UInt64, x) == uint64(y) -@test typeof(itrunc(UInt64, x)) == UInt64 - -@test iceil(UInt32, x) == uint32(z) -@test typeof(iceil(UInt32, x)) == UInt32 -@test ifloor(UInt32, x) == uint32(y) -@test typeof(ifloor(UInt32, x)) == UInt32 -@test itrunc(UInt32, x) == uint32(y) -@test typeof(itrunc(UInt32, x)) == UInt32 - -@test iceil(UInt16, x) == uint16(z) -@test typeof(iceil(UInt16, x)) == UInt16 -@test ifloor(UInt16, x) == uint16(y) -@test typeof(ifloor(UInt16, x)) == UInt16 -@test itrunc(UInt16, x) == uint16(y) -@test typeof(itrunc(UInt16, x)) == UInt16 - -#@test iceil(UInt8, x) == uint8(z) -#@test typeof(iceil(UInt8, x)) == UInt8 -#@test ifloor(UInt8, x) == uint8(y) -#@test typeof(ifloor(UInt8, x)) == UInt8 -#@test itrunc(UInt8, x) == uint8(y) -#@test typeof(itrunc(UInt8, x)) == UInt8 - -@test iceil(a) == c -@test typeof(iceil(a)) == BigInt -@test ifloor(a) == b -@test typeof(ifloor(a)) == BigInt -@test itrunc(a) == b -@test typeof(itrunc(a)) == BigInt +@test ceil(Integer,x) == z +@test typeof(ceil(Integer,x)) == BigInt +@test floor(Integer,x) == y +@test typeof(floor(Integer,x)) == BigInt +@test trunc(Integer,x) == y +@test typeof(trunc(Integer,x)) == BigInt + +@test ceil(Int64, x) == int64(z) +@test typeof(ceil(Int64, x)) == Int64 +@test floor(Int64, x) == int64(y) +@test typeof(floor(Int64, x)) == Int64 +@test trunc(Int64, x) == int64(y) +@test typeof(trunc(Int64, x)) == Int64 + +@test ceil(Int32, x) == int32(z) +@test typeof(ceil(Int32, x)) == Int32 +@test floor(Int32, x) == int32(y) +@test typeof(floor(Int32, x)) == Int32 +@test trunc(Int32, x) == int32(y) +@test typeof(trunc(Int32, x)) == Int32 + +@test ceil(Int16, x) == int16(z) +@test typeof(ceil(Int16, x)) == Int16 +@test floor(Int16, x) == int16(y) +@test typeof(floor(Int16, x)) == Int16 +@test trunc(Int16, x) == int16(y) +@test typeof(trunc(Int16, x)) == Int16 + +#@test ceil(Int8, x) == int8(z) +#@test typeof(ceil(Int8, x)) == Int8 +#@test floor(Int8, x) == int8(y) +#@test typeof(floor(Int8, x)) == Int8 +#@test trunc(Int8, x) == int8(y) +#@test typeof(trunc(Int8, x)) == Int8 + +@test ceil(UInt64, x) == uint64(z) +@test typeof(ceil(UInt64, x)) == UInt64 +@test floor(UInt64, x) == uint64(y) +@test typeof(floor(UInt64, x)) == UInt64 +@test trunc(UInt64, x) == uint64(y) +@test typeof(trunc(UInt64, x)) == UInt64 + +@test ceil(UInt32, x) == uint32(z) +@test typeof(ceil(UInt32, x)) == UInt32 +@test floor(UInt32, x) == uint32(y) +@test typeof(floor(UInt32, x)) == UInt32 +@test trunc(UInt32, x) == uint32(y) +@test typeof(trunc(UInt32, x)) == UInt32 + +@test ceil(UInt16, x) == uint16(z) +@test typeof(ceil(UInt16, x)) == UInt16 +@test floor(UInt16, x) == uint16(y) +@test typeof(floor(UInt16, x)) == UInt16 +@test trunc(UInt16, x) == uint16(y) +@test typeof(trunc(UInt16, x)) == UInt16 + +#@test ceil(UInt8, x) == uint8(z) +#@test typeof(ceil(UInt8, x)) == UInt8 +#@test floor(UInt8, x) == uint8(y) +#@test typeof(floor(UInt8, x)) == UInt8 +#@test trunc(UInt8, x) == uint8(y) +#@test typeof(trunc(UInt8, x)) == UInt8 + +@test ceil(Integer,a) == c +@test typeof(ceil(Integer,a)) == BigInt +@test floor(Integer,a) == b +@test typeof(floor(Integer,a)) == BigInt +@test trunc(Integer,a) == b +@test typeof(trunc(Integer,a)) == BigInt + +@test ceil(Int128,a) == c +@test typeof(ceil(Int128,a)) == Int128 +@test floor(Int128,a) == b +@test typeof(floor(Int128,a)) == Int128 +@test trunc(Int128,a) == b +@test typeof(trunc(Int128,a)) == Int128 + +@test ceil(UInt128,a) == c +@test typeof(ceil(UInt128,a)) == UInt128 +@test floor(UInt128,a) == b +@test typeof(floor(UInt128,a)) == UInt128 +@test trunc(UInt128,a) == b +@test typeof(trunc(UInt128,a)) == UInt128 + # basic arithmetic # Signed addition @@ -789,7 +804,7 @@ f = BigFloat(10)^int32(1000) @test f > i1 @test f > i2 -i3 = itrunc(f) +i3 = trunc(Integer,f) @test i3 == f @test i3+1 > f @test i3+1 >= f diff --git a/test/numbers.jl b/test/numbers.jl index b026429c91f9d..9415304630fdc 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -563,11 +563,7 @@ end for x=int64(2)^53-2:int64(2)^53+5, y=[2.0^53-2 2.0^53-1 2.0^53 2.0^53+2 2.0^53+4] u = uint64(x) - if WORD_SIZE == 64 - @test y == float64(itrunc(y)) - else - @test y == float64(int64(trunc(y))) - end + @test y == float64(trunc(Int64,y)) @test (x==y)==(y==x) @test (x!=y)==!(x==y) @@ -707,6 +703,16 @@ end @test float64(typemax(UInt128)) == 2.0^128 @test float32(typemax(UInt128)) == 2.0f0^128 +# check for double rounding in conversion +@test float64(10633823966279328163822077199654060032) == 1.0633823966279327e37 #0x1p123 +@test float64(10633823966279328163822077199654060033) == 1.063382396627933e37 #nextfloat(0x1p123) +@test float64(-10633823966279328163822077199654060032) == -1.0633823966279327e37 +@test float64(-10633823966279328163822077199654060033) == -1.063382396627933e37 + +# check Float vs Int128 comparisons +@test int128(1e30) == 1e30 +@test int128(1e30)+1 > 1e30 + @test int128(-2.0^127) == typemin(Int128) @test float64(uint128(3.7e19)) == 3.7e19 @test float64(uint128(3.7e30)) == 3.7e30 @@ -1301,46 +1307,45 @@ end for x = 2^53-10:2^53+10 y = float64(x) - i = WORD_SIZE == 64 ? itrunc(y) : int64(trunc(y)) + i = trunc(Int64,y) @test int64(trunc(y)) == i @test int64(round(y)) == i @test int64(floor(y)) == i @test int64(ceil(y)) == i - if WORD_SIZE == 64 - @test iround(y) == i - @test ifloor(y) == i - @test iceil(y) == i - end + + @test round(Int64,y) == i + @test floor(Int64,y) == i + @test ceil(Int64,y) == i end for x = 2^24-10:2^24+10 y = float32(x) - i = itrunc(y) + i = trunc(Int,y) @test int(trunc(y)) == i @test int(round(y)) == i @test int(floor(y)) == i @test int(ceil(y)) == i - @test iround(y) == i - @test ifloor(y) == i - @test iceil(y) == i + @test round(Int,y) == i + @test floor(Int,y) == i + @test ceil(Int,y) == i end -@test_throws InexactError iround(Inf) -@test_throws InexactError iround(NaN) -@test iround(2.5) == 3 -@test iround(-1.9) == -2 -@test_throws InexactError iround(Int64, 9.223372036854776e18) -@test iround(Int64, 9.223372036854775e18) == 9223372036854774784 -@test_throws InexactError iround(Int64, -9.223372036854778e18) -@test iround(Int64, -9.223372036854776e18) == typemin(Int64) -@test_throws InexactError iround(UInt64, 1.8446744073709552e19) -@test iround(UInt64, 1.844674407370955e19) == 0xfffffffffffff800 -@test_throws InexactError iround(Int32, 2.1474836f9) -@test iround(Int32, 2.1474835f9) == 2147483520 -@test_throws InexactError iround(Int32, -2.147484f9) -@test iround(Int32, -2.1474836f9) == typemin(Int32) -@test_throws InexactError iround(UInt32, 4.2949673f9) -@test iround(UInt32, 4.294967f9) == 0xffffff00 +@test_throws InexactError round(Int,Inf) +@test_throws InexactError round(Int,NaN) +@test round(Int,2.5) == 3 +@test round(Int,-1.9) == -2 +@test_throws InexactError round(Int64, 9.223372036854776e18) +@test round(Int64, 9.223372036854775e18) == 9223372036854774784 +@test_throws InexactError round(Int64, -9.223372036854778e18) +@test round(Int64, -9.223372036854776e18) == typemin(Int64) +@test_throws InexactError round(UInt64, 1.8446744073709552e19) +@test round(UInt64, 1.844674407370955e19) == 0xfffffffffffff800 +@test_throws InexactError round(Int32, 2.1474836f9) +@test round(Int32, 2.1474835f9) == 2147483520 +@test_throws InexactError round(Int32, -2.147484f9) +@test round(Int32, -2.1474836f9) == typemin(Int32) +@test_throws InexactError round(UInt32, 4.2949673f9) +@test round(UInt32, 4.294967f9) == 0xffffff00 for n = 1:100 m = 1 @@ -1350,32 +1355,32 @@ for n = 1:100 @test n == m end -@test iround(UInt,-0.0) == 0 -@test iround(Int,-0.0) == 0 +@test round(UInt,-0.0) == 0 +@test round(Int,-0.0) == 0 -@test iround(Int, 0.5) == 1 -@test iround(Int, prevfloat(0.5)) == 0 -@test iround(Int, -0.5) == -1 -@test iround(Int, nextfloat(-0.5)) == 0 +@test round(Int, 0.5) == 1 +@test round(Int, prevfloat(0.5)) == 0 +@test round(Int, -0.5) == -1 +@test round(Int, nextfloat(-0.5)) == 0 -@test iround(UInt, 0.5) == 1 -@test iround(UInt, prevfloat(0.5)) == 0 -@test_throws InexactError iround(UInt, -0.5) -@test iround(UInt, nextfloat(-0.5)) == 0 +@test round(UInt, 0.5) == 1 +@test round(UInt, prevfloat(0.5)) == 0 +@test_throws InexactError round(UInt, -0.5) +@test round(UInt, nextfloat(-0.5)) == 0 -@test iround(Int, 0.5f0) == 1 -@test iround(Int, prevfloat(0.5f0)) == 0 -@test iround(Int, -0.5f0) == -1 -@test iround(Int, nextfloat(-0.5f0)) == 0 +@test round(Int, 0.5f0) == 1 +@test round(Int, prevfloat(0.5f0)) == 0 +@test round(Int, -0.5f0) == -1 +@test round(Int, nextfloat(-0.5f0)) == 0 -@test iround(UInt, 0.5f0) == 1 -@test iround(UInt, prevfloat(0.5f0)) == 0 -@test_throws InexactError iround(UInt, -0.5f0) -@test iround(UInt, nextfloat(-0.5f0)) == 0 +@test round(UInt, 0.5f0) == 1 +@test round(UInt, prevfloat(0.5f0)) == 0 +@test_throws InexactError round(UInt, -0.5f0) +@test round(UInt, nextfloat(-0.5f0)) == 0 # numbers that can't be rounded by trunc(x+0.5) -@test iround(Int64, 2.0^52 + 1) == 4503599627370497 -@test iround(Int32, 2.0f0^23 + 1) == 8388609 +@test round(Int64, 2.0^52 + 1) == 4503599627370497 +@test round(Int32, 2.0f0^23 + 1) == 8388609 # binary literals @@ -1982,11 +1987,11 @@ end # issue #7441 @test_throws InexactError int32(2.0^50) -@test_throws InexactError iround(UInt8, 255.5) -@test iround(UInt8, 255.4) === 0xff +@test_throws InexactError round(UInt8, 255.5) +@test round(UInt8, 255.4) === 0xff -@test_throws InexactError iround(Int16, -32768.7) -@test iround(Int16, -32768.1) === int16(-32768) +@test_throws InexactError round(Int16, -32768.7) +@test round(Int16, -32768.1) === int16(-32768) # issue #7508 @test_throws ErrorException reinterpret(Int, 0x01) diff --git a/test/random.jl b/test/random.jl index 0e5b7105f7501..3bf0d87fec087 100644 --- a/test/random.jl +++ b/test/random.jl @@ -114,7 +114,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # defines this as # k_0 = 2^31 * r * f(r) / v, w_0 = 0.5^31 * v / f(r), f_0 = 1, # where v is the area of each strip of the ziggurat. - ki[1] = uint64(itrunc(x1*fib[256]/nor_section_area*nmantissa)) + ki[1] = trunc(UInt64,x1*fib[256]/nor_section_area*nmantissa) wib[1] = nor_section_area/fib[256]/nmantissa fib[1] = one(BigFloat) @@ -122,7 +122,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus # need inverse operator of y = exp(-0.5*x*x) -> x = sqrt(-2*ln(y)) x = sqrt(-2.0*log(nor_section_area/x1 + fib[i+1])) - ki[i+1] = uint64(itrunc(x/x1*nmantissa)) + ki[i+1] = trunc(UInt64,x/x1*nmantissa) wib[i] = x/nmantissa fib[i] = exp(-0.5*x*x) x1 = x @@ -139,7 +139,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # defines this as # k_0 = 2^32 * r * f(r) / v, w_0 = 0.5^32 * v / f(r), f_0 = 1, # where v is the area of each strip of the ziggurat. - ke[1] = uint64(itrunc(x1*feb[256]/exp_section_area*emantissa)) + ke[1] = trunc(UInt64,x1*feb[256]/exp_section_area*emantissa) web[1] = exp_section_area/feb[256]/emantissa feb[1] = one(BigFloat) @@ -147,7 +147,7 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays # New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus # need inverse operator of y = exp(-x) -> x = -ln(y) x = -log(exp_section_area/x1 + feb[i+1]) - ke[i+1] = uint64(itrunc(x/x1*emantissa)) + ke[i+1] = trunc(UInt64,x/x1*emantissa) web[i] = x/emantissa feb[i] = exp(-x) x1 = x diff --git a/test/rounding.jl b/test/rounding.jl index 75d2672286f18..398b0e08941e6 100644 --- a/test/rounding.jl +++ b/test/rounding.jl @@ -105,24 +105,22 @@ for v = [sqrt(2),-1/3,nextfloat(1.0),prevfloat(1.0),nextfloat(-1.0), @test pu - pd == eps(pz) end -# TODO: add Uint128 check -# Float vs Uint128 comparisons are currently broken, e.g. Inf32 == typemax(Uint128) for T in [Float32,Float64] for v in [sqrt(big(2.0)),-big(1.0)/big(3.0),nextfloat(big(1.0)), prevfloat(big(1.0)),nextfloat(big(0.0)),prevfloat(big(0.0)), pi,e,eulergamma,catalan,golden, - typemax(Int64),typemax(Uint64),typemax(Int128),0xa2f30f6001bb2ec6] + typemax(Int64),typemax(Uint64),typemax(Int128),typemax(UInt128),0xa2f30f6001bb2ec6] pn = T(v,RoundNearest) - @test pn == convert(T,v) + @test pn == convert(T,BigFloat(v)) pz = T(v,RoundToZero) - @test pz == with_rounding(()->convert(T,v), BigFloat, RoundToZero) + @test pz == with_rounding(()->convert(T,BigFloat(v)), BigFloat, RoundToZero) pd = T(v,RoundDown) - @test pd == with_rounding(()->convert(T,v), BigFloat, RoundDown) + @test pd == with_rounding(()->convert(T,BigFloat(v)), BigFloat, RoundDown) pu = T(v,RoundUp) - @test pu == with_rounding(()->convert(T,v), BigFloat, RoundUp) + @test pu == with_rounding(()->convert(T,BigFloat(v)), BigFloat, RoundUp) @test pn == pd || pn == pu @test v > 0 ? pz == pd : pz == pu - @test pu - pd == eps(pz) + @test isinf(pu) || pu - pd == eps(pz) end end diff --git a/test/sparse.jl b/test/sparse.jl index 5d3ae411fd539..f03efcf4a4944 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -243,12 +243,19 @@ end # Unary functions a = sprand(5,15, 0.5) afull = full(a) -for op in (:sin, :cos, :tan, :iceil, :ifloor, :ceil, :floor, :abs, :abs2) +for op in (:sin, :cos, :tan, :ceil, :floor, :abs, :abs2) @eval begin @test ($op)(afull) == full($(op)(a)) end end +for op in (:ceil, :floor) + @eval begin + @test ($op)(Int,afull) == full($(op)(Int,a)) + end +end + + # getindex tests ni = 23 nj = 32 diff --git a/test/strings.jl b/test/strings.jl index d5dc665611888..5716ce1148139 100644 --- a/test/strings.jl +++ b/test/strings.jl @@ -771,7 +771,7 @@ n = 3 a = [3,1,2] @test """$(a[2])""" == "1" @test """$(a[3]+7)""" == "9" -@test """$(ifloor(4.5))""" == "4" +@test """$(floor(Int,4.5))""" == "4" nl = " " @test """ From cf661e306bb5555cdaf583605d9132e77a01eab6 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 24 Nov 2014 23:27:30 +0000 Subject: [PATCH 4/4] implement float <-> 128-bit int conversions in Julia --- base/float.jl | 99 ++++++++++++++++++++++++++++++++++++++++++++++++--- base/int.jl | 13 +++++-- 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/base/float.jl b/base/float.jl index 32107f407aac4..03df5d472125d 100644 --- a/base/float.jl +++ b/base/float.jl @@ -5,19 +5,81 @@ for t in (Bool,Char,Int8,Int16,Int32,Int64,UInt8,UInt16,UInt32,UInt64) end for t1 in (Float32,Float64) - for st in (Int8,Int16,Int32,Int64,Int128) + for st in (Int8,Int16,Int32,Int64) @eval begin convert(::Type{$t1},x::($st)) = box($t1,sitofp($t1,unbox($st,x))) promote_rule(::Type{$t1}, ::Type{$st} ) = $t1 end end - for ut in (Bool,Char,UInt8,UInt16,UInt32,UInt64,UInt128) + for ut in (Bool,Char,UInt8,UInt16,UInt32,UInt64) @eval begin convert(::Type{$t1},x::($ut)) = box($t1,uitofp($t1,unbox($ut,x))) promote_rule(::Type{$t1}, ::Type{$ut} ) = $t1 end end end + +promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64 +promote_rule(::Type{Float64}, ::Type{Int128}) = Float64 +promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32 +promote_rule(::Type{Float32}, ::Type{Int128}) = Float32 + +function convert(::Type{Float64}, x::UInt128) + x == 0 && return 0.0 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 53 + y = ((x % UInt64) << (53-n)) & 0x000f_ffff_ffff_ffff + else + y = ((x >> (n-54)) % UInt64) & 0x001f_ffff_ffff_ffff # keep 1 extra bit + y = (y+1)>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt64(trailing_zeros(x) == (n-54)) # fix last bit to round to even + end + reinterpret(Float64,((n+1022)<<52) % UInt64 + y) +end + +function convert(::Type{Float64}, x::Int128) + x == 0 && return 0.0 + s = ((x >>> 64) % UInt64) & 0x8000_0000_0000_0000 # sign bit + x = abs(x) % UInt128 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 53 + y = ((x % UInt64) << (53-n)) & 0x000f_ffff_ffff_ffff + else + y = ((x >> (n-54)) % UInt64) & 0x001f_ffff_ffff_ffff # keep 1 extra bit + y = (y+1)>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt64(trailing_zeros(x) == (n-54)) # fix last bit to round to even + end + reinterpret(Float64, (s | ((n+1022)<<52) % UInt64) + y) +end + +function convert(::Type{Float32}, x::UInt128) + x == 0 && return 0f0 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 24 + y = ((x % UInt32) << (24-n)) & 0x007f_ffff + else + y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit + y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even + end + reinterpret(Float32,((n+126)<<23) % UInt32 + y) +end + +function convert(::Type{Float32}, x::Int128) + x == 0 && return 0f0 + s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit + x = abs(x) % UInt128 + n = 128-leading_zeros(x) # ndigits0z(x,2) + if n <= 24 + y = ((x % UInt32) << (24-n)) & 0x007f_ffff + else + y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit + y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent) + y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even + end + reinterpret(Float32, (s | ((n+126)<<23) % UInt32) + y) +end + #convert(::Type{Float16}, x::Float32) = box(Float16,fptrunc(Float16,x)) convert(::Type{Float16}, x::Float64) = convert(Float16, convert(Float32,x)) convert(::Type{Float32}, x::Float64) = box(Float32,fptrunc(Float32,x)) @@ -44,19 +106,48 @@ float32(x) = convert(Float32, x) float64(x) = convert(Float64, x) float(x) = convert(FloatingPoint, x) -for Ti in (Int8, Int16, Int32, Int64, Int128) +for Ti in (Int8, Int16, Int32, Int64) @eval begin unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptosi($Ti,unbox(Float32,x))) unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptosi($Ti,unbox(Float64,x))) end end -for Ti in (UInt8, UInt16, UInt32, UInt64, UInt128) +for Ti in (UInt8, UInt16, UInt32, UInt64) @eval begin unsafe_trunc(::Type{$Ti}, x::Float32) = box($Ti,fptoui($Ti,unbox(Float32,x))) unsafe_trunc(::Type{$Ti}, x::Float64) = box($Ti,fptoui($Ti,unbox(Float64,x))) end end +function unsafe_trunc(::Type{UInt128}, x::Float64) + xu = reinterpret(UInt64,x) + k = int(xu >> 52) & 0x07ff - 1075 + xu = (xu & 0x000f_ffff_ffff_ffff) | 0x0010_0000_0000_0000 + if k <= 0 + UInt128(xu >> -k) + else + UInt128(xu) << k + end +end +function unsafe_trunc(::Type{Int128}, x::Float64) + copysign(unsafe_trunc(UInt128,x) % Int128, x) +end + +function unsafe_trunc(::Type{UInt128}, x::Float32) + xu = reinterpret(UInt32,x) + k = int(xu >> 23) & 0x00ff - 150 + xu = (xu & 0x007f_ffff) | 0x0080_0000 + if k <= 0 + UInt128(xu >> -k) + else + UInt128(xu) << k + end +end +function unsafe_trunc(::Type{Int128}, x::Float32) + copysign(unsafe_trunc(UInt128,x) % Int128, x) +end + + # matches convert methods # also determines floor, ceil, round trunc(::Type{Signed}, x::Float32) = trunc(Int,x) diff --git a/base/int.jl b/base/int.jl index fb55bd8f46232..ef0143f57059c 100644 --- a/base/int.jl +++ b/base/int.jl @@ -179,20 +179,29 @@ rem{T<:Integer}(x::T, ::Type{T}) = x rem(x::Integer, ::Type{Bool}) = ((x&1)!=0) mod{T<:Integer}(x::Integer, ::Type{T}) = rem(x, T) -for to in (Int8, Int16, Int32, Int64, Int128) +for to in (Int8, Int16, Int32, Int64) @eval begin convert(::Type{$to}, x::Float32) = box($to,checked_fptosi($to,unbox(Float32,x))) convert(::Type{$to}, x::Float64) = box($to,checked_fptosi($to,unbox(Float64,x))) end end -for to in (UInt8, UInt16, UInt32, UInt64, UInt128) +for to in (UInt8, UInt16, UInt32, UInt64) @eval begin convert(::Type{$to}, x::Float32) = box($to,checked_fptoui($to,unbox(Float32,x))) convert(::Type{$to}, x::Float64) = box($to,checked_fptoui($to,unbox(Float64,x))) end end +for Ti in (Int128,UInt128) + for Tf in (Float32,Float64) + @eval function convert(::Type{$Ti},x::$Tf) + isinteger(x) || throw(InexactError()) + trunc($Ti,x) + end + end +end + convert(::Type{Signed}, x::UInt8 ) = convert(Int8,x) convert(::Type{Signed}, x::UInt16 ) = convert(Int16,x) convert(::Type{Signed}, x::UInt32 ) = convert(Int32,x)