pif_LongInt

by Popisfizzy
Double, triple, and quadruple-precision integers, both signed and unsigned.
ID:2066921
 
BETA VERSION. FEATURES ARE INCOMPLETE.

pif_LongInt is a library that implements both signed and unsigned double, triple, and quadruple precision (32-bit, 48-bit, and 64-bit) integers. In this beta version, only 32-bit signed and unsigned double precision integers are available.

As an example of how one could use this library, here is how you could write a function to output binomial coefficients with your desired precision.
proc/Choose(n, k, type = /pif_LongInt/Unsigned32)
/*
* This method was found at:
* http://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
*/


var/pif_LongInt/Int = new type(1)

if(k > (n-k))
// Choose(n, k) = Choose(n, n-k) so by doing this we reduce the
// number of steps needed.
k = n-k

for(var/i = 0, i <= k-1, i ++)
Int *= n-i
Int /= i+1

return Int

Then to test it, we could do
mob/Login()
..()

for(var/i = 0, i <= 25, i ++)
world << "<tt>Choose(25, [i])\t=>\t[Choose(25, i, /pif_LongInt/Unsigned32).Print()]</tt>"

And this produces the following output.
Choose(25, 0)    =>      1
Choose(25, 1)    =>      25
Choose(25, 2)    =>      300
Choose(25, 3)    =>      2300
Choose(25, 4)    =>      12650
Choose(25, 5)    =>      53130
Choose(25, 6)    =>      177100
Choose(25, 7)    =>      480700
Choose(25, 8)    =>      1081575
Choose(25, 9)    =>      2042975
Choose(25, 10)   =>      3268760
Choose(25, 11)   =>      4457400
Choose(25, 12)   =>      5200300
Choose(25, 13)   =>      5200300
Choose(25, 14)   =>      4457400
Choose(25, 15)   =>      3268760
Choose(25, 16)   =>      2042975
Choose(25, 17)   =>      1081575
Choose(25, 18)   =>      480700
Choose(25, 19)   =>      177100
Choose(25, 20)   =>      53130
Choose(25, 21)   =>      12650
Choose(25, 22)   =>      2300
Choose(25, 23)   =>      300
Choose(25, 24)   =>      25
Choose(25, 25)   =>      1

Upon completion of this library, when a Unsigned64 object is available, one could compute binomial coefficients Choose(n,k) for all 0 ≤ n,k ≤ 67 to perfect accuracy (where the largest in this domain is given by Choose(67, 33) = Choose(67, 34) = 14,226,520,737,620,288,370).

Other Links
The MIT License
Copyright (c) 2016-2024 Timothy "popisfizzy" Reilly

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.