diff --git a/vlib/math/fact_tables.v b/vlib/math/fact_tables.v new file mode 100644 index 0000000000..80472072bc --- /dev/null +++ b/vlib/math/fact_tables.v @@ -0,0 +1,177 @@ +module math + +const( + factorials = [ + f64(1.000000000000000000000e+0), /* 0! */ + 1.000000000000000000000e+0, /* 1! */ + 2.000000000000000000000e+0, /* 2! */ + 6.000000000000000000000e+0, /* 3! */ + 2.400000000000000000000e+1, /* 4! */ + 1.200000000000000000000e+2, /* 5! */ + 7.200000000000000000000e+2, /* 6! */ + 5.040000000000000000000e+3, /* 7! */ + 4.032000000000000000000e+4, /* 8! */ + 3.628800000000000000000e+5, /* 9! */ + 3.628800000000000000000e+6, /* 10! */ + 3.991680000000000000000e+7, /* 11! */ + 4.790016000000000000000e+8, /* 12! */ + 6.227020800000000000000e+9, /* 13! */ + 8.717829120000000000000e+10, /* 14! */ + 1.307674368000000000000e+12, /* 15! */ + 2.092278988800000000000e+13, /* 16! */ + 3.556874280960000000000e+14, /* 17! */ + 6.402373705728000000000e+15, /* 18! */ + 1.216451004088320000000e+17, /* 19! */ + 2.432902008176640000000e+18, /* 20! */ + 5.109094217170944000000e+19, /* 21! */ + 1.124000727777607680000e+21, /* 22! */ + 2.585201673888497664000e+22, /* 23! */ + 6.204484017332394393600e+23, /* 24! */ + 1.551121004333098598400e+25, /* 25! */ + 4.032914611266056355840e+26, /* 26! */ + 1.088886945041835216077e+28, /* 27! */ + 3.048883446117138605015e+29, /* 28! */ + 8.841761993739701954544e+30, /* 29! */ + 2.652528598121910586363e+32, /* 30! */ + 8.222838654177922817726e+33, /* 31! */ + 2.631308369336935301672e+35, /* 32! */ + 8.683317618811886495518e+36, /* 33! */ + 2.952327990396041408476e+38, /* 34! */ + 1.033314796638614492967e+40, /* 35! */ + 3.719933267899012174680e+41, /* 36! */ + 1.376375309122634504632e+43, /* 37! */ + 5.230226174666011117600e+44, /* 38! */ + 2.039788208119744335864e+46, /* 39! */ + 8.159152832478977343456e+47, /* 40! */ + 3.345252661316380710817e+49, /* 41! */ + 1.405006117752879898543e+51, /* 42! */ + 6.041526306337383563736e+52, /* 43! */ + 2.658271574788448768044e+54, /* 44! */ + 1.196222208654801945620e+56, /* 45! */ + 5.502622159812088949850e+57, /* 46! */ + 2.586232415111681806430e+59, /* 47! */ + 1.241391559253607267086e+61, /* 48! */ + 6.082818640342675608723e+62, /* 49! */ + 3.041409320171337804361e+64, /* 50! */ + 1.551118753287382280224e+66, /* 51! */ + 8.065817517094387857166e+67, /* 52! */ + 4.274883284060025564298e+69, /* 53! */ + 2.308436973392413804721e+71, /* 54! */ + 1.269640335365827592597e+73, /* 55! */ + 7.109985878048634518540e+74, /* 56! */ + 4.052691950487721675568e+76, /* 57! */ + 2.350561331282878571829e+78, /* 58! */ + 1.386831185456898357379e+80, /* 59! */ + 8.320987112741390144276e+81, /* 60! */ + 5.075802138772247988009e+83, /* 61! */ + 3.146997326038793752565e+85, /* 62! */ + 1.982608315404440064116e+87, /* 63! */ + 1.268869321858841641034e+89, /* 64! */ + 8.247650592082470666723e+90, /* 65! */ + 5.443449390774430640037e+92, /* 66! */ + 3.647111091818868528825e+94, /* 67! */ + 2.480035542436830599601e+96, /* 68! */ + 1.711224524281413113725e+98, /* 69! */ + 1.197857166996989179607e+100, /* 70! */ + 8.504785885678623175212e+101, /* 71! */ + 6.123445837688608686152e+103, /* 72! */ + 4.470115461512684340891e+105, /* 73! */ + 3.307885441519386412260e+107, /* 74! */ + 2.480914081139539809195e+109, /* 75! */ + 1.885494701666050254988e+111, /* 76! */ + 1.451830920282858696341e+113, /* 77! */ + 1.132428117820629783146e+115, /* 78! */ + 8.946182130782975286851e+116, /* 79! */ + 7.156945704626380229481e+118, /* 80! */ + 5.797126020747367985880e+120, /* 81! */ + 4.753643337012841748421e+122, /* 82! */ + 3.945523969720658651190e+124, /* 83! */ + 3.314240134565353266999e+126, /* 84! */ + 2.817104114380550276949e+128, /* 85! */ + 2.422709538367273238177e+130, /* 86! */ + 2.107757298379527717214e+132, /* 87! */ + 1.854826422573984391148e+134, /* 88! */ + 1.650795516090846108122e+136, /* 89! */ + 1.485715964481761497310e+138, /* 90! */ + 1.352001527678402962552e+140, /* 91! */ + 1.243841405464130725548e+142, /* 92! */ + 1.156772507081641574759e+144, /* 93! */ + 1.087366156656743080274e+146, /* 94! */ + 1.032997848823905926260e+148, /* 95! */ + 9.916779348709496892096e+149, /* 96! */ + 9.619275968248211985333e+151, /* 97! */ + 9.426890448883247745626e+153, /* 98! */ + 9.332621544394415268170e+155, /* 99! */ + 9.332621544394415268170e+157, /* 100! */ + 9.425947759838359420852e+159, /* 101! */ + 9.614466715035126609269e+161, /* 102! */ + 9.902900716486180407547e+163, /* 103! */ + 1.029901674514562762385e+166, /* 104! */ + 1.081396758240290900504e+168, /* 105! */ + 1.146280563734708354534e+170, /* 106! */ + 1.226520203196137939352e+172, /* 107! */ + 1.324641819451828974500e+174, /* 108! */ + 1.443859583202493582205e+176, /* 109! */ + 1.588245541522742940425e+178, /* 110! */ + 1.762952551090244663872e+180, /* 111! */ + 1.974506857221074023537e+182, /* 112! */ + 2.231192748659813646597e+184, /* 113! */ + 2.543559733472187557120e+186, /* 114! */ + 2.925093693493015690688e+188, /* 115! */ + 3.393108684451898201198e+190, /* 116! */ + 3.969937160808720895402e+192, /* 117! */ + 4.684525849754290656574e+194, /* 118! */ + 5.574585761207605881323e+196, /* 119! */ + 6.689502913449127057588e+198, /* 120! */ + 8.094298525273443739682e+200, /* 121! */ + 9.875044200833601362412e+202, /* 122! */ + 1.214630436702532967577e+205, /* 123! */ + 1.506141741511140879795e+207, /* 124! */ + 1.882677176888926099744e+209, /* 125! */ + 2.372173242880046885677e+211, /* 126! */ + 3.012660018457659544810e+213, /* 127! */ + 3.856204823625804217357e+215, /* 128! */ + 4.974504222477287440390e+217, /* 129! */ + 6.466855489220473672507e+219, /* 130! */ + 8.471580690878820510985e+221, /* 131! */ + 1.118248651196004307450e+224, /* 132! */ + 1.487270706090685728908e+226, /* 133! */ + 1.992942746161518876737e+228, /* 134! */ + 2.690472707318050483595e+230, /* 135! */ + 3.659042881952548657690e+232, /* 136! */ + 5.012888748274991661035e+234, /* 137! */ + 6.917786472619488492228e+236, /* 138! */ + 9.615723196941089004197e+238, /* 139! */ + 1.346201247571752460588e+241, /* 140! */ + 1.898143759076170969429e+243, /* 141! */ + 2.695364137888162776589e+245, /* 142! */ + 3.854370717180072770522e+247, /* 143! */ + 5.550293832739304789551e+249, /* 144! */ + 8.047926057471991944849e+251, /* 145! */ + 1.174997204390910823948e+254, /* 146! */ + 1.727245890454638911203e+256, /* 147! */ + 2.556323917872865588581e+258, /* 148! */ + 3.808922637630569726986e+260, /* 149! */ + 5.713383956445854590479e+262, /* 150! */ + 8.627209774233240431623e+264, /* 151! */ + 1.311335885683452545607e+267, /* 152! */ + 2.006343905095682394778e+269, /* 153! */ + 3.089769613847350887959e+271, /* 154! */ + 4.789142901463393876336e+273, /* 155! */ + 7.471062926282894447084e+275, /* 156! */ + 1.172956879426414428192e+278, /* 157! */ + 1.853271869493734796544e+280, /* 158! */ + 2.946702272495038326504e+282, /* 159! */ + 4.714723635992061322407e+284, /* 160! */ + 7.590705053947218729075e+286, /* 161! */ + 1.229694218739449434110e+289, /* 162! */ + 2.004401576545302577600e+291, /* 163! */ + 3.287218585534296227263e+293, /* 164! */ + 5.423910666131588774984e+295, /* 165! */ + 9.003691705778437366474e+297, /* 166! */ + 1.503616514864999040201e+300, /* 167! */ + 2.526075744973198387538e+302, /* 168! */ + 4.269068009004705274939e+304, /* 169! */ + 7.257415615307998967397e+306 /* 170! */ + ] +) diff --git a/vlib/math/math.v b/vlib/math/math.v index 1a908ec7fb..19a18a2f20 100644 --- a/vlib/math/math.v +++ b/vlib/math/math.v @@ -133,49 +133,20 @@ pub fn exp2(a f64) f64 { } // factorial calculates the factorial of the provided value. -// TODO bring back once multiple value functions are implemented -/* -fn recursive_product( n int, current_number_ptr &int) int{ - mut m := n / 2 - if (m == 0){ - return *current_number_ptr += 2 - } - if (n == 2){ - return (*current_number_ptr += 2) * (*current_number_ptr += 2) - } - return recursive_product((n - m), *current_number_ptr) * recursive_product(m, *current_number_ptr) -} +pub fn factorial(n f64) f64 { + // For a large postive argument (n >= factorials.len) return max_f64 -pub fn factorial(n int) i64 { - if n < 0 { - panic('factorial: Cannot find factorial of negative number') - } - if n < 2 { - return i64(1) - } - mut r := 1 - mut p := 1 - mut current_number := 1 - mut h := 0 - mut shift := 0 - mut high := 1 - mut len := high - mut log2n := int(floor(log2(n))) - for ;h != n; { - shift += h - h = n >> log2n - log2n -= 1 - len = high - high = (h - 1) | 1 - len = (high - len)/2 - if (len > 0){ - p *= recursive_product(len, ¤t_number) - r *= p - } - } - return i64((r << shift)) + if n >= factorials.len { + return max_f64 + } + + /* Otherwise return n!. */ + if n == f64(i64(n)) && n >= 0.0 { + return f64(factorials[i64(n)]) + } + + return gamma(n + 1.0) } -*/ // floor returns the nearest f64 lower or equal of the provided value. pub fn floor(a f64) f64 { diff --git a/vlib/math/math_test.v b/vlib/math/math_test.v index 3f0cf1f2a7..cf50fdd7cb 100644 --- a/vlib/math/math_test.v +++ b/vlib/math/math_test.v @@ -27,13 +27,11 @@ fn test_digits() { assert negative_digits[2] == -1 } -/* fn test_factorial() { assert math.factorial(12) == 479001600 assert math.factorial(5) == 120 assert math.factorial(0) == 1 } -*/ fn test_erf() { assert math.erf(0) == 0