English 中文(简体)
在Ocaml挽救一大笔愤怒
原标题:Saving a large integer in Ocaml
  • 时间:2024-05-06 02:08:56
  •  标签:
  • ocaml

我试图将一大批基数——10人改为16人,然后改为海尔。 我所看到的错误是Ocaml说,愤怒太大。

utop # let large_int = 11515195063862318899931685488813747395775516287289682636499965282714637259206269 ;;
Error: Integer literal exceeds the range of representable integers of type int

我也尝试利用Int64和类似的问题。

utop # let large_int = Int64.of_string "11515195063862318899931685488813747395775516287289682636499965282714637259206269";;
    Exception: Failure "Int64.of_string".

这是我努力工作的更大法典。 它与下文中的例子一样运作。

(* convert from base10_to_base16 *)
let rec split_digits n =
  if n = 0 then [] else
    let (d, rest) = n mod 256, n / 256 in
    d :: split_digits rest


let decode_base10 message =
  let bytes_arr = List.rev (split_digits message) in
  let decoded_message = String.concat "" (List.map (fun x -> String.make 1 (Char.chr x)) bytes_arr) in
  decoded_message


let base10_message = 310400273487 (* base-10 number *)
let decoded_message = decode_base10 base10_message
问题回答

他表示再次接触是因为你提供的ger字字面值超过了OCaml的<代码>int类型的最大可代表价值。 OCaml s int 通常类型为<代码>31或63-bit,根据您的系统结构签字。

为了处理像你重新工作的那种大型分类器,你可以使用OCaml的<代码>Int64模块。 然而,由于代表人数太大,因此,的功能似乎已经失败。

您可以尝试使用OCaml的Z模块,该单元提供任意割礼。

open Z

let large_int = Z.of_string "11515195063862318899931685488813747395775516287289682636499965282714637259206269"

问题在于您的<代码>decode_base10功能。 页: 1 你们需要首先把星号转换成数字清单,然后才能将其分数。

let rec split_digits n =
  if n = 0 then [] else
    let (d, rest) = n mod 256, n / 256 in
    d :: split_digits rest

let decode_base10 message =
  let bytes_arr = List.rev message in
  let decoded_message = String.concat "" (List.map (fun x -> String.make 1 (Char.chr x)) bytes_arr) in
  decoded_message

let base10_message = split_digits 310400273487 (* base-10 number *)
let decoded_message = decode_base10 base10_message

您希望安装Zarith Library,以便以任意精确数字开展工作,因为本地的内乱类型无法处理你再次要求他们的大小。

Z.div<>>>>,

# #require "zarith";;

# let n = Z.of_string "11515195063862318899931685488813747395775516287289682636499965282714637259206269";;
val n : Z.t = <abstr>

# # let rec split_digits n =
    if n = Z.zero then []
    else
      let z256 = Z.of_int 256 in
      Z.to_int (Z.rem n z256) :: split_digits (Z.div n z256);;
val split_digits : Z.t -> int list = <fun>

# split_digits n;;
- : int list =
[125; 110; 119; 48; 100; 95; 121; 52; 119; 95; 51; 104; 55; 95;
 108; 108; 52; 95; 54; 110; 49; 100; 48; 99; 110; 51; 123; 111;
 116; 112; 121; 114; 99]

当然,你可能希望使<条码>分数/代码>的功能具有适时性,以避免出现大量人口过多的情况。 https://v2.ocaml.org/manual/tail_mod_cons.html”

let[@tail_mod_cons] rec split_digits n =
  if n = Z.zero then []
  else
    let z256 = Z.of_int 256 in
    Z.to_int (Z.rem n z256) :: split_digits (Z.div n z256)

当然,通过修改<条码>分数/代码>以生成。 我还使256个参数成为增加灵活性的参数。

# let rec split_digits sz n () =
    if n = Z.zero then Seq.Nil
    else
      let z_sz = Z.of_int sz in
      Seq.Cons (
        Z.to_int @@ Z.rem n z_sz, 
        split_digits sz @@ Z.div n z_sz);;
val split_digits : Z.t -> int -> int Seq.t = <fun>

# n
  |> split_digits 256 
  |> List.of_seq;;
- : int list =
[125; 110; 119; 48; 100; 95; 121; 52; 119; 95; 51; 104; 55; 95;
 108; 108; 52; 95; 54; 110; 49; 100; 48; 99; 110; 51; 123; 111;
 116; 112; 121; 114; 99]




相关问题
ocamlc, module compilation

I wrote an app in ocaml. It consist of several modules: Util (util.ml) Work1 (work1.ml) -- open Util Work2 (work2.ml) -- open Util, too Main (main.ml) -- open all of them. When i compile its, using ...

How can I simplify this ocaml pattern-matching code?

I m writing a simple little ocaml program that reads an algebraic statement in from a file, parses it into an AST using ocamllex/ocamlyacc, reduces it, and then prints it. The part where I m reducing ...

How can I create a type with multiple parameters in OCaml?

I m trying to create a type that has multiple type parameters. I know how to make a type with one parameter: type a foo = a * int But I need to have two parameters, so that I can parameterize the ...

Hashtable indexed on several fields

I m currently programming an OCaml module defining a type corresponding to a CPU register. The interface of this module is the following : (* * Defines a type which represents a R3000 register. *) ...

Extending an existing type in OCaml

I ve been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I ve started to think that I d like to be able to extend an ...

Ocaml Syntax Error

What s wrong with this code? I can t figure it out: let parent (rules : grammar) (symbol1 : string) (symbol2 : string) : (SymbolSet.t) = try SymbolSet.singleton (getParent [symbol1; symbol2] ...