Swift 5 MIT LiCENSE CI via GitHub Actions

swift-int2x

轻松创建双倍宽度整数

概要

import Int2X

typealias U128 = UInt2X<UInt64> // Yes.  That's it!
typealias I128 = Int2X<UInt64>  // ditto for signed integers

描述

感谢 SE-0104,创建您自己的整数类型比以往任何时候都更容易。 该模块利用了这一点 -- 从任何给定的 FixedWidthInteger 创建双倍宽度整数。

U?Int{128,256,512,1024} 预定义如下

public typealias UInt128    = UInt2X<UInt64>
public typealias UInt256    = UInt2X<UInt128>
public typealias UInt512    = UInt2X<UInt256>
public typealias UInt1024   = UInt2X<UInt512>
public typealias Int128    = Int2X<UInt64>
public typealias Int256    = Int2X<UInt128>
public typealias Int512    = Int2X<UInt256>
public typealias Int1024   = Int2X<UInt512>

正如您所看到的,UInt2XInt2X 本身也是 FixedWidthInteger,因此您可以将它们堆叠起来。

用法

构建

$ git clone https://github.com/dankogai/swift-int2x.git
$ cd swift-int2x # the following assumes your $PWD is here
$ swift build

REPL

$ scripts/run-repl.sh

或者

$ swift run --repl

在您的 REPL 中,

Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
  1> import Int2X 
  2> Int1024.max.description
$R0: String = "89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068607"

从您的 SwiftPM 管理的项目

将以下内容添加到 dependencies 部分

.package(
  url: "https://github.com/dankogai/swift-int2x.git", .branch("main")
)

将以下内容添加到 .target 参数

.target(
  name: "YourSwiftyPackage",
  dependencies: ["Int2X"])

现在您要做的就是

import Int2X

在你的代码中。 享受吧!

先决条件

Swift 5 或更高版本,OS X 或 Linux 用于构建。