Просмотр исходного кода

Starting design of big integer class.
Operators defined: A+B, -A

Samuel Jaffe 8 лет назад
Родитель
Сommit
7d80393dce
4 измененных файлов с 710 добавлено и 0 удалено
  1. 455 0
      bigdecimal.xcodeproj/project.pbxproj
  2. 59 0
      include/biginteger.h
  3. 161 0
      src/biginteger.cpp
  4. 35 0
      test/biginteger.t.h

+ 455 - 0
bigdecimal.xcodeproj/project.pbxproj

@@ -0,0 +1,455 @@
+// !$*UTF8*$!
+{
+	archiveVersion = 1;
+	classes = {
+	};
+	objectVersion = 46;
+	objects = {
+
+/* Begin PBXBuildFile section */
+		CD5FB2911F06EFEF005A0D61 /* biginteger.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD5FB2801F06EF7D005A0D61 /* biginteger.cpp */; };
+		CD5FB2921F06EFF2005A0D61 /* biginteger.h in Headers */ = {isa = PBXBuildFile; fileRef = CD5FB27F1F06EF70005A0D61 /* biginteger.h */; settings = {ATTRIBUTES = (Public, ); }; };
+		CD5FB2A31F06F03D005A0D61 /* biginteger.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD5FB2801F06EF7D005A0D61 /* biginteger.cpp */; };
+		CD5FB2A41F06F048005A0D61 /* libbigdecimal_s.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CD5FB2981F06F030005A0D61 /* libbigdecimal_s.a */; };
+		CD5FB2A71F06F0D2005A0D61 /* bigdecimal_tc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD5FB2A61F06F0D2005A0D61 /* bigdecimal_tc.cpp */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXCopyFilesBuildPhase section */
+		CD5FB26F1F06EEAF005A0D61 /* CopyFiles */ = {
+			isa = PBXCopyFilesBuildPhase;
+			buildActionMask = 2147483647;
+			dstPath = /usr/share/man/man1/;
+			dstSubfolderSpec = 0;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 1;
+		};
+/* End PBXCopyFilesBuildPhase section */
+
+/* Begin PBXFileReference section */
+		CD5FB2711F06EEAF005A0D61 /* bigdecimal_tc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = bigdecimal_tc; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD5FB27E1F06EF64005A0D61 /* biginteger.t.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = biginteger.t.h; sourceTree = "<group>"; };
+		CD5FB27F1F06EF70005A0D61 /* biginteger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = biginteger.h; sourceTree = "<group>"; };
+		CD5FB2801F06EF7D005A0D61 /* biginteger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = biginteger.cpp; sourceTree = "<group>"; };
+		CD5FB2861F06EFEA005A0D61 /* libbigdecimal.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libbigdecimal.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD5FB2981F06F030005A0D61 /* libbigdecimal_s.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libbigdecimal_s.a; sourceTree = BUILT_PRODUCTS_DIR; };
+		CD5FB2A61F06F0D2005A0D61 /* bigdecimal_tc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bigdecimal_tc.cpp; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+		CD5FB26E1F06EEAF005A0D61 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD5FB2A41F06F048005A0D61 /* libbigdecimal_s.a in Frameworks */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+		CD5FB2831F06EFEA005A0D61 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+		CD5FB2951F06F030005A0D61 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+		CD5FB2681F06EEAF005A0D61 = {
+			isa = PBXGroup;
+			children = (
+				CD5FB27B1F06EED6005A0D61 /* include */,
+				CD5FB27C1F06EED6005A0D61 /* src */,
+				CD5FB27D1F06EED6005A0D61 /* test */,
+				CD5FB2721F06EEAF005A0D61 /* Products */,
+			);
+			sourceTree = "<group>";
+		};
+		CD5FB2721F06EEAF005A0D61 /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				CD5FB2711F06EEAF005A0D61 /* bigdecimal_tc */,
+				CD5FB2861F06EFEA005A0D61 /* libbigdecimal.dylib */,
+				CD5FB2981F06F030005A0D61 /* libbigdecimal_s.a */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
+		CD5FB27B1F06EED6005A0D61 /* include */ = {
+			isa = PBXGroup;
+			children = (
+				CD5FB27F1F06EF70005A0D61 /* biginteger.h */,
+			);
+			path = include;
+			sourceTree = "<group>";
+		};
+		CD5FB27C1F06EED6005A0D61 /* src */ = {
+			isa = PBXGroup;
+			children = (
+				CD5FB2801F06EF7D005A0D61 /* biginteger.cpp */,
+			);
+			path = src;
+			sourceTree = "<group>";
+		};
+		CD5FB27D1F06EED6005A0D61 /* test */ = {
+			isa = PBXGroup;
+			children = (
+				CD5FB27E1F06EF64005A0D61 /* biginteger.t.h */,
+				CD5FB2A61F06F0D2005A0D61 /* bigdecimal_tc.cpp */,
+			);
+			path = test;
+			sourceTree = "<group>";
+		};
+/* End PBXGroup section */
+
+/* Begin PBXHeadersBuildPhase section */
+		CD5FB2841F06EFEA005A0D61 /* Headers */ = {
+			isa = PBXHeadersBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD5FB2921F06EFF2005A0D61 /* biginteger.h in Headers */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+		CD5FB2961F06F030005A0D61 /* Headers */ = {
+			isa = PBXHeadersBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXHeadersBuildPhase section */
+
+/* Begin PBXNativeTarget section */
+		CD5FB2701F06EEAF005A0D61 /* bigdecimal_tc */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CD5FB2781F06EEAF005A0D61 /* Build configuration list for PBXNativeTarget "bigdecimal_tc" */;
+			buildPhases = (
+				CD5FB26D1F06EEAF005A0D61 /* Sources */,
+				CD5FB26E1F06EEAF005A0D61 /* Frameworks */,
+				CD5FB26F1F06EEAF005A0D61 /* CopyFiles */,
+				CD5FB2A51F06F04C005A0D61 /* ShellScript */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = bigdecimal_tc;
+			productName = bigdecimal;
+			productReference = CD5FB2711F06EEAF005A0D61 /* bigdecimal_tc */;
+			productType = "com.apple.product-type.tool";
+		};
+		CD5FB2851F06EFEA005A0D61 /* bigdecimal */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CD5FB28E1F06EFEA005A0D61 /* Build configuration list for PBXNativeTarget "bigdecimal" */;
+			buildPhases = (
+				CD5FB2821F06EFEA005A0D61 /* Sources */,
+				CD5FB2831F06EFEA005A0D61 /* Frameworks */,
+				CD5FB2841F06EFEA005A0D61 /* Headers */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = bigdecimal;
+			productName = bigdecimal;
+			productReference = CD5FB2861F06EFEA005A0D61 /* libbigdecimal.dylib */;
+			productType = "com.apple.product-type.library.dynamic";
+		};
+		CD5FB2971F06F030005A0D61 /* bigdecimal_s */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CD5FB2A01F06F030005A0D61 /* Build configuration list for PBXNativeTarget "bigdecimal_s" */;
+			buildPhases = (
+				CD5FB2941F06F030005A0D61 /* Sources */,
+				CD5FB2951F06F030005A0D61 /* Frameworks */,
+				CD5FB2961F06F030005A0D61 /* Headers */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = bigdecimal_s;
+			productName = bigdecimal_s;
+			productReference = CD5FB2981F06F030005A0D61 /* libbigdecimal_s.a */;
+			productType = "com.apple.product-type.library.static";
+		};
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+		CD5FB2691F06EEAF005A0D61 /* Project object */ = {
+			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0720;
+				ORGANIZATIONNAME = "Sam Jaffe";
+				TargetAttributes = {
+					CD5FB2701F06EEAF005A0D61 = {
+						CreatedOnToolsVersion = 7.2.1;
+					};
+					CD5FB2851F06EFEA005A0D61 = {
+						CreatedOnToolsVersion = 7.2.1;
+					};
+					CD5FB2971F06F030005A0D61 = {
+						CreatedOnToolsVersion = 7.2.1;
+					};
+				};
+			};
+			buildConfigurationList = CD5FB26C1F06EEAF005A0D61 /* Build configuration list for PBXProject "bigdecimal" */;
+			compatibilityVersion = "Xcode 3.2";
+			developmentRegion = English;
+			hasScannedForEncodings = 0;
+			knownRegions = (
+				en,
+			);
+			mainGroup = CD5FB2681F06EEAF005A0D61;
+			productRefGroup = CD5FB2721F06EEAF005A0D61 /* Products */;
+			projectDirPath = "";
+			projectRoot = "";
+			targets = (
+				CD5FB2701F06EEAF005A0D61 /* bigdecimal_tc */,
+				CD5FB2851F06EFEA005A0D61 /* bigdecimal */,
+				CD5FB2971F06F030005A0D61 /* bigdecimal_s */,
+			);
+		};
+/* End PBXProject section */
+
+/* Begin PBXShellScriptBuildPhase section */
+		CD5FB2A51F06F04C005A0D61 /* ShellScript */ = {
+			isa = PBXShellScriptBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			inputPaths = (
+				"$(SRCROOT)/test/biginteger.t.h",
+			);
+			outputPaths = (
+				"$(SRCROOT)/test/bigdecimal_tc.cpp",
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+			shellPath = /bin/sh;
+			shellScript = "cd test; cxxtestgen --error-printer -o bigdecimal_tc.cpp biginteger.t.h";
+		};
+/* End PBXShellScriptBuildPhase section */
+
+/* Begin PBXSourcesBuildPhase section */
+		CD5FB26D1F06EEAF005A0D61 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD5FB2A71F06F0D2005A0D61 /* bigdecimal_tc.cpp in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+		CD5FB2821F06EFEA005A0D61 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD5FB2911F06EFEF005A0D61 /* biginteger.cpp in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+		CD5FB2941F06F030005A0D61 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CD5FB2A31F06F03D005A0D61 /* biginteger.cpp in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+		CD5FB2761F06EEAF005A0D61 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = dwarf;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				ENABLE_TESTABILITY = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_DYNAMIC_NO_PIC = NO;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_OPTIMIZATION_LEVEL = 0;
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"DEBUG=1",
+					"$(inherited)",
+				);
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				HEADER_SEARCH_PATHS = "";
+				MACOSX_DEPLOYMENT_TARGET = 10.10;
+				MTL_ENABLE_DEBUG_INFO = YES;
+				ONLY_ACTIVE_ARCH = YES;
+				SDKROOT = macosx;
+				USER_HEADER_SEARCH_PATHS = include;
+			};
+			name = Debug;
+		};
+		CD5FB2771F06EEAF005A0D61 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				ENABLE_NS_ASSERTIONS = NO;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				HEADER_SEARCH_PATHS = "";
+				MACOSX_DEPLOYMENT_TARGET = 10.10;
+				MTL_ENABLE_DEBUG_INFO = NO;
+				SDKROOT = macosx;
+				USER_HEADER_SEARCH_PATHS = include;
+			};
+			name = Release;
+		};
+		CD5FB2791F06EEAF005A0D61 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				HEADER_SEARCH_PATHS = /usr/local/include;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Debug;
+		};
+		CD5FB27A1F06EEAF005A0D61 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				HEADER_SEARCH_PATHS = /usr/local/include;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Release;
+		};
+		CD5FB28F1F06EFEA005A0D61 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				GCC_ENABLE_CPP_EXCEPTIONS = YES;
+				GCC_ENABLE_CPP_RTTI = YES;
+				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Debug;
+		};
+		CD5FB2901F06EFEA005A0D61 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				GCC_ENABLE_CPP_EXCEPTIONS = YES;
+				GCC_ENABLE_CPP_RTTI = YES;
+				GCC_SYMBOLS_PRIVATE_EXTERN = YES;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Release;
+		};
+		CD5FB2A11F06F030005A0D61 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				EXECUTABLE_PREFIX = lib;
+				GCC_ENABLE_CPP_EXCEPTIONS = YES;
+				GCC_ENABLE_CPP_RTTI = YES;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Debug;
+		};
+		CD5FB2A21F06F030005A0D61 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				EXECUTABLE_PREFIX = lib;
+				GCC_ENABLE_CPP_EXCEPTIONS = YES;
+				GCC_ENABLE_CPP_RTTI = YES;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+			};
+			name = Release;
+		};
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+		CD5FB26C1F06EEAF005A0D61 /* Build configuration list for PBXProject "bigdecimal" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD5FB2761F06EEAF005A0D61 /* Debug */,
+				CD5FB2771F06EEAF005A0D61 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CD5FB2781F06EEAF005A0D61 /* Build configuration list for PBXNativeTarget "bigdecimal_tc" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD5FB2791F06EEAF005A0D61 /* Debug */,
+				CD5FB27A1F06EEAF005A0D61 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CD5FB28E1F06EFEA005A0D61 /* Build configuration list for PBXNativeTarget "bigdecimal" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD5FB28F1F06EFEA005A0D61 /* Debug */,
+				CD5FB2901F06EFEA005A0D61 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CD5FB2A01F06F030005A0D61 /* Build configuration list for PBXNativeTarget "bigdecimal_s" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CD5FB2A11F06F030005A0D61 /* Debug */,
+				CD5FB2A21F06F030005A0D61 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+/* End XCConfigurationList section */
+	};
+	rootObject = CD5FB2691F06EEAF005A0D61 /* Project object */;
+}

+ 59 - 0
include/biginteger.h

@@ -0,0 +1,59 @@
+//
+//  biginteger.h
+//  bigdecimal
+//
+//  Created by Sam Jaffe on 6/30/17.
+//
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+namespace math {
+  class biginteger {
+  public:
+    using data_type = std::vector<int32_t>;
+    static constexpr int32_t const MAX_SEG   { 999999999};
+    static constexpr int32_t const OVER_SEG  {1000000000};
+    static constexpr int32_t const SEG_DIGITS{         9};
+  public:
+    // Constructors
+    biginteger();
+    biginteger(int32_t);
+    biginteger(uint32_t);
+    biginteger(int64_t);
+    biginteger(uint64_t);
+    biginteger(char const *);
+    // Unary operators
+    biginteger operator-() const;
+    // Binary operators
+    friend biginteger operator+(biginteger const &, biginteger const &);
+    friend biginteger operator-(biginteger const &, biginteger const &);
+    friend biginteger operator*(biginteger const &, biginteger const &);
+    friend biginteger operator/(biginteger const &, biginteger const &);
+    friend biginteger & operator+=(biginteger &, biginteger const &);
+    friend biginteger & operator-=(biginteger &, biginteger const &);
+    friend biginteger & operator*=(biginteger &, biginteger const &);
+    friend biginteger & operator/=(biginteger &, biginteger const &);
+    // Output
+    std::string to_string() const;
+    // Comparison
+    friend bool operator==(biginteger const &, biginteger const &);
+    friend bool operator!=(biginteger const &, biginteger const &);
+    friend bool operator<=(biginteger const &, biginteger const &);
+    friend bool operator< (biginteger const &, biginteger const &);
+    friend bool operator>=(biginteger const &, biginteger const &);
+    friend bool operator> (biginteger const &, biginteger const &);
+  private:
+    biginteger(bool, uint64_t);
+    biginteger(bool, data_type &&);
+    friend void swap(biginteger & rhs, biginteger & lhs) {
+      using std::swap;
+      swap(rhs.is_negative, lhs.is_negative);
+      swap(rhs.data, lhs.data);
+    }
+    bool is_negative;
+    data_type data{};
+  };
+}

+ 161 - 0
src/biginteger.cpp

@@ -0,0 +1,161 @@
+//
+//  biginteger.cpp
+//  bigdecimal
+//
+//  Created by Sam Jaffe on 6/30/17.
+//
+
+#include "biginteger.h"
+
+using namespace math;
+
+namespace detail {
+  using data_type = biginteger::data_type;
+  // 1 => GREATER, 0 => EQUAL, -1 => LESS
+  int compare(data_type const & rhs, data_type const & lhs);
+  data_type add(data_type const & rhs, data_type const & lhs);
+  data_type subtract_nounderflow(data_type const & rhs, data_type const & lhs);
+}
+
+static void swap(biginteger & rhs, biginteger && lhs) {
+  swap(rhs, lhs);
+}
+
+biginteger::biginteger()
+: biginteger(false, 0) {}
+
+biginteger::biginteger(int32_t value)
+: biginteger(static_cast<int64_t>(value)) {}
+
+biginteger::biginteger(uint32_t value)
+: biginteger(static_cast<uint64_t>(value)) {}
+
+biginteger::biginteger(int64_t value) :
+biginteger(value < 0, static_cast<uint64_t>(value < 0 ? -value : value)) {}
+
+biginteger::biginteger(uint64_t value)
+: biginteger(false, value) {}
+
+biginteger::biginteger(bool neg, uint64_t value) :
+is_negative(neg) {
+  uint64_t next{0};
+  do {
+    next = value / OVER_SEG;
+    data.push_back(static_cast<int32_t>(value - (OVER_SEG * next)));
+  } while ((value = next) > 0);
+}
+
+biginteger::biginteger(bool neg, data_type && dt) :
+is_negative(neg), data(std::move(dt)) {}
+
+biginteger & operator+=(biginteger & rhs, biginteger const & lhs) {
+  swap(rhs, rhs + lhs);
+  return rhs;
+}
+
+//biginteger & operator-=(biginteger & rhs, biginteger const & lhs) {
+//  swap(rhs, rhs - lhs);
+//  return rhs;
+//}
+//
+//biginteger & operator*=(biginteger & rhs, biginteger const & lhs) {
+//  swap(rhs, rhs * lhs);
+//  return rhs;
+//}
+//
+//biginteger & operator/=(biginteger & rhs, biginteger const & lhs) {
+//  swap(rhs, rhs / lhs);
+//  return rhs;
+//}
+
+biginteger biginteger::operator-() const {
+  biginteger tmp{*this};
+  tmp.is_negative = !tmp.is_negative;
+  return tmp;
+}
+
+biginteger math::operator+(biginteger const & rhs, biginteger const & lhs) {
+  if (rhs.is_negative == lhs.is_negative) {
+    return {rhs.is_negative, detail::add(rhs.data, lhs.data)};
+  } else {
+    auto cmp = detail::compare(rhs.data, lhs.data);
+    if (cmp == 0) {
+      return 0;
+    } else if (cmp > 0) {
+      return {rhs.is_negative, detail::subtract_nounderflow(rhs.data, lhs.data)};
+    } else {
+      return {lhs.is_negative, detail::subtract_nounderflow(lhs.data, rhs.data)};
+    }
+  }
+}
+
+namespace detail {
+#define IMPL_COMPARE(expr) \
+  if (rhs expr < lhs expr) return -1; \
+  else if (lhs expr < rhs expr) return 1
+  
+  int compare(data_type const & rhs, data_type const & lhs) {
+    IMPL_COMPARE(.size());
+    for (data_type::size_type i = rhs.size(); i > 0; --i) {
+      IMPL_COMPARE([i-1]);
+    }
+    return 0;
+  }
+#undef IMPL_COMPARE
+  
+  data_type add(data_type const & rhs, data_type const & lhs) {
+    data_type::size_type const rbnd = rhs.size(), lbnd = lhs.size();
+    data_type::size_type const ubnd = std::max(rbnd, lbnd);
+    data_type rval(ubnd);
+    // Add
+    for (data_type::size_type i = 0; i < rbnd; ++i) {
+      rval[i] += rhs[i];
+    }
+    for (data_type::size_type i = 0; i < lbnd; ++i) {
+      rval[i] += lhs[i];
+    }
+    // Carry
+    for (data_type::size_type i = 0; i < ubnd-1; ++i) {
+      if (rval[i] > biginteger::MAX_SEG) {
+        rval[i] -= biginteger::OVER_SEG;
+        rval[i+1] += 1;
+      }
+    }
+    if (rval[ubnd-1] > biginteger::MAX_SEG) {
+      rval[ubnd-1] -= biginteger::OVER_SEG;
+      rval.push_back(1);
+    }
+    return rval;
+  }
+  
+  data_type subtract_nounderflow(data_type const & rhs, data_type const & lhs) {
+    data_type::size_type const rbnd = rhs.size(), lbnd = lhs.size();
+    data_type rval{rhs};
+    // Subtract
+    for (data_type::size_type i = 0; i < lbnd; ++i) {
+      rval[i] -= lhs[i];
+    }
+    // Borrow
+    for (data_type::size_type i = 0; i < rbnd; ++i) {
+      if (rval[i] < 0) {
+        rval[i] += biginteger::OVER_SEG;
+        rval[i+1] -= 1;
+      }
+    }
+    if (rval[rbnd-1] == 0 && rbnd > 1) { rval.pop_back(); }
+    return rval;
+  }
+}
+
+#include <iomanip>
+#include <sstream>
+
+std::string biginteger::to_string() const {
+  std::stringstream ss;
+  if (is_negative) ss << '-';
+  ss << data[data.size()-1];
+  for (data_type::size_type i = data.size()-1; i > 0; --i) {
+    ss << std::setw(SEG_DIGITS) << std::setfill('0') << data[i-1];
+  }
+  return ss.str();
+}

+ 35 - 0
test/biginteger.t.h

@@ -0,0 +1,35 @@
+//
+//  biginteger.t.h
+//  bigdecimal
+//
+//  Created by Sam Jaffe on 6/30/17.
+//
+
+#pragma once
+
+#include "biginteger.h"
+
+#include <cxxtest/TestSuite.h>
+
+class biginteger_TestSuite : public CxxTest::TestSuite {
+public:
+  void testAddPastBounds() {
+    math::biginteger bi{999999999ULL};
+    TS_ASSERT_EQUALS((bi+1).to_string(), "1000000000");
+  }
+  
+  void testAddReciprocalIsZero() {
+    math::biginteger bi{1000};
+    TS_ASSERT_EQUALS((bi+(-bi)).to_string(), "0");
+  }
+
+  void testAddNegativeLargerGivesNegative() {
+    math::biginteger bi{1000};
+    TS_ASSERT_EQUALS((bi+(-1001)).to_string(), "-1");
+  }
+
+  void testAddNegativeSmallerGivesPositive() {
+    math::biginteger bi{1000};
+    TS_ASSERT_EQUALS((bi+(-999)).to_string(), "1");
+  }
+};