commit 28ae2a9b293ccad59fe54ad72a16865564df70e6
parent 0fc9652907d06f18b6243cdfff7e9534dafb39cd
Author: Andrew Laack <andrew@laack.co>
Date: Sun, 12 Oct 2025 19:05:29 -0500
Created (near) arbitrary precision integer struct
Diffstat:
2 files changed, 154 insertions(+), 0 deletions(-)
diff --git a/fib-in-c.c b/dsa/fib-in-c.c
diff --git a/dsa/jumbo-int.c b/dsa/jumbo-int.c
@@ -0,0 +1,154 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+struct jumboInt{
+ short* revValue;
+ unsigned long long digits; // technically not arbitrary length, but like, really...
+ short sign;
+};
+
+struct jumboInt* makeJumboInt(int input){
+ struct jumboInt* retVal = malloc(sizeof(struct jumboInt));
+
+ unsigned long long digits = 0;
+ unsigned long long check = input;
+
+ if(input < 0){
+ retVal->sign = -1;
+ input *= -1;
+ check *= -1;
+ }
+ else{
+ retVal->sign = 1;
+ }
+
+ for(int i = 0; check > 0; ++i){
+ check /= 10;
+ digits += 1;
+ }
+ if(input == 0){
+ digits = 1;
+ }
+
+ short* inp = malloc(sizeof(short) * digits);
+
+ int itr = 0;
+ while(itr < digits){
+ short current = input % 10;
+ input /= 10;
+ inp[itr] = current;
+ itr += 1;
+ }
+
+ short* res = malloc(sizeof(short) * digits);
+ for(int i = 0; i < digits; ++i){
+ res[i] = inp[(digits-1) - i];
+ }
+ free(inp);
+
+ retVal->revValue = res;
+ retVal->digits = digits;
+
+
+ return retVal;
+}
+
+void printJumbo(struct jumboInt* result){
+ if (result->digits != 0){
+ if (result->sign == -1){
+ printf("-");
+ }
+ for(unsigned long long i = 0; i < result->digits ; ++i){
+ printf("%hd" , (result->revValue[i]));
+ }
+ }
+}
+
+struct jumboInt* addJumbo(struct jumboInt* in1, struct jumboInt* in2){
+
+ struct jumboInt* shorter;
+ struct jumboInt* longer;
+
+ if(in1->digits < in2->digits){
+ shorter = in1;
+ longer = in2;
+
+ }
+ else{
+ shorter = in2;
+ longer = in1;
+ }
+
+ short* shorterReverse = malloc(sizeof(short) * shorter->digits);
+ short* longerReverse = malloc(sizeof(short) * longer->digits);
+
+ short* resultReverse = malloc(sizeof(short) * (longer->digits + 1));
+
+ for(int i = 0; i < shorter->digits; ++i){
+ shorterReverse[i] = shorter->revValue[(shorter->digits-1) - i];
+ }
+
+ for(int i = 0; i < longer->digits; ++i){
+ longerReverse[i] = longer->revValue[(longer->digits-1) - i];
+ }
+
+ short carry = 0;
+ for(int i = 0; i < longer->digits; ++i){
+ short sum = longerReverse[i] + carry;
+ if (i < shorter->digits) {
+ sum += shorterReverse[i];
+ }
+ resultReverse[i] = sum % 10;
+ carry = sum / 10;
+ }
+
+ short* finalResult;
+ unsigned long long finalDigits;
+ short sign = longer->sign;
+
+ if(carry == 1){
+ resultReverse[longer->digits] = 1;
+ finalResult = malloc(sizeof(short) * (longer->digits + 1));
+ finalDigits = longer->digits + 1;
+ }
+ else{
+ finalResult = malloc(sizeof(short) * (longer->digits));
+ finalDigits = longer->digits;
+ }
+
+ for(int i = 0 ; i < finalDigits; ++i){
+ finalResult[i] = resultReverse[(finalDigits - 1) - i];
+ }
+
+
+ free(shorterReverse);
+ free(longerReverse);
+ free(resultReverse);
+
+ struct jumboInt* result = malloc(sizeof(struct jumboInt));
+ result->revValue = finalResult;
+ result->sign = sign;
+ result->digits = finalDigits;
+
+ return result;
+}
+
+
+// NOTE: This is not really tested for negative numbers. Addition of two negatives should work, but addition of a negative and positive
+// does not.
+
+int main(){
+ struct jumboInt* sum = makeJumboInt(10);
+
+ for(int i = 0 ; i < 1000000; ++i){
+ struct jumboInt* newSum = addJumbo(sum, sum);
+ free(sum->revValue);
+ free(sum);
+ sum = newSum;
+ }
+
+ printJumbo(sum);
+ printf("\n");
+
+ return 0;
+}