问题
I want to to perform computations with large integers in SML, through functions like pow in this link:
http://www.standardml.org/Basis/int-inf.html#IntInf:STR:SPEC
But how do I get to use this "library"?
UPDATE:
Thanks for the answer. I got it. I also had to change the limit for printing with
Control.Print.intinfDepth := 10000;
I made my own pow function for IntInfs (and it works) like this:
fun power 0 = IntInf.toLarge 1
| power n = IntInf.toLarge 2 * power(n-1);
回答1:
It depends on which implementation you use, but generally you need to convert your Int's to LageInt/InfInf types with the Int.toLarge
:
(* will be types as an IntInf *)
val aa = 10983298432984329843298432984329843298432987987987432987987987432987
val a = IntInf.pow(aa,10);
(* explicit type as if some other constraint had enforced this*)
val b = 10 : int
val c = Int.toLarge b;
val d = IntInf.pow(c, b);
The variable aa
may not be parsed in your interpreter. It depends on what you use. I have tested it in poly and mlton.
where the above gets the types (given by mlton with -show-basis flag):
val a: intInf
val aa: intInf
val b: int32
val c: intInf
val d: intInf
回答2:
-fun prod (a: IntInf.int, b:IntInf.int) : IntInf.int = a+b;
val prod = fn : IntInf.int * IntInf.int -> IntInf.int
for example
-> on how to represent a big integer : IntInf.int as a parameter inside a function
来源:https://stackoverflow.com/questions/7807441/how-to-use-intinf-or-largeint-in-sml