{"slug": "inventing-your-own-math-as-a-computer-programmer", "title": "Inventing Your Own Math as a Computer Programmer", "summary": "A developer extended a C library for algebraic number theory, previously limited to integer arithmetic in finite fields, to support floating-point operations by representing complex quadratic primes as a + bω and regular complex numbers as x + yi. The work adds number-field root and generator evaluation via the identity √-r = i√r, coordinate conversion between rectangular and lattice geometries such as the Eisenstein hexagonal plane, and complex arithmetic rules keyed to Heegner numbers, with division excluded due to numerical instability. The author frames the effort as groundwork for a 'complex number unit' CPU aimed at neural net quantization, factorization, Pell curves and discrete logs.", "body_md": "God made the integers, all the rest is the work of man.\n\n~ Leopold Kronecker\n\n# 1.0 Introduction \n\n[Part 1](https://leetarxiv.substack.com/p/special-q-descent-and-lattice-sieving-931) explored the *discovered* side of algebraic number theory, and we built a C library for working with complex quadratic primes of the form x² + ny² modulo a prime number p. \n\nHowever, modern computers run on floating-point arithmetic, not integer operations in a finite field.\n\nNow, we must *invent* our own math to accommodate floating point operations in our algebraic number theory library.\n\nWe’re building a *complex number unit*. It’s a CPU designed for working with imaginary numbers. Useful for [neural net quantization](https://leetarxiv.substack.com/p/sherry-tequila-and-fairy-in-python), [factorization](https://leetarxiv.substack.com/p/continued-fractions-and-lattice-sieving), [pell curves](https://leetarxiv.substack.com/p/pell-equation-over-a-finite-field) and [discrete logs](https://leetarxiv.substack.com/p/computation-of-discrete-logarithms-gaussian-eisenstein).\n\n## 1.1 Quick Primer\n\nWe covered all this in detail in [Part 1](https://leetarxiv.substack.com/p/special-q-descent-and-lattice-sieving-931). Our library works with a subset of complex prime numbers of the form:\n\nWhere `r` is drawn from the Heegner numbers:\n\nWhen `r = 2`, this complex quadratic primes are the *[Gaussian integers](https://leetarxiv.substack.com/i/179533821/22-gaussian-integer-sieve)*. `r = 3` is called an [Eisenstein integer](https://leetarxiv.substack.com/i/179533821/31-ring-of-eisenstein-integers).\n\nArithmetic happens in a ring or a field depending on `r`. Here’s a quick cheatsheet:\n\n# 2.0 Floating Point Extensions\n\n###### <sup>*</sup>Jump to Section 2.4 for the runnable code block you can copy and paste\n\nThis section is a step-by-step guide to accommodating floating point ops using complex numbers. Here are some super salient identities (OpenStax, 2026)[1](#footnote-1):\n\nWe represent our complex quadratic primes as `< a, b > = a + bω` and our regular complex numbers as `[ x, y ] = x + yi` as done in (Barber, 2022)[2](#footnote-2).\n\nC code for our structs:\n\n## 2.1 Finding Number Field Roots and Generators\n\nIn [Part 1](https://leetarxiv.substack.com/p/special-q-descent-and-lattice-sieving-931), evaluating the number field root modulo a prime `p` (like `√-3` for eisenstein integers) was a fundamental operation. Here’s a sample generator if you don’t know what that means:\n\nNow, we extend root evalution to floating-point via the identity below:\n\n```\n√-r = i√r (Square root of a negative number identity)\n```\n\nFor instance, the number field root for the eisentein integers becomes:\n\n```\n√-3 = i√3\n```\n\nThen, the number field generator, `ω`, for the eisenstein integers becomes:\n\nGenerally, we define `ω` as:\n\nwhere\n\nand\n\nC code for this initialization:\n\n## 2.2 Changing Coordinate Systems\n\nRegular complex numbers exist over a rectangular plane where the x-axis is the real part and the y-axis is the imaginary part, `[ x, y ] = x + yi` (OpenStax, 2026):\n\nThe complex quadratic primes however, occupy different lattice geometries. These are represented as `< a, b > = a + bω`. For instance, the Eisenstein integers occupy a hexagonal plane. This and other planes are illustrated below (Graycuber, 2026)[3](#footnote-3):\n\nIf you’re here for a good time, then coordinate conversion is done as:\n\nIf you’re here for a long time then here’s the derivation:\n\n### Change of Coordinate Derivation\n\nC code for conversion is:\n\n## **2.3 Complex Arithmetic**\n\nHigh school math taught us addition and multiplication with complex numbers in rectangular coordinates resembles (Roberts, 2026)[4](#footnote-4):\n\nC code looks like:\n\nArithmetic in complex quadratic domains depends on the Heegner number (Dummit, 2021)[5](#footnote-5) and here are the rules:\n\nDivision is handwavy somewhat. So we exclude it. Here’s the C code:\n\nOur C code doesn’t look like the formula due to numerical instablity, but the formula works in Python:\n\n``` python\ndef multiply_heegner_float(z1, z2, u, R):\n    a1, b1 = z1\n    a2, b2 = z2\n\n    return (\n        a1*a2 - (u*u + R*R)*b1*b2,\n        a1*b2 + a2*b1 + 2*u*b1*b2\n    )\n```\n\n## 2.4 Test Conversion and Arithmetic\n\nWe can test both our conversion and arithmetic by selecting random Heegner fields and two random HeegnerFloats. Then, comparing sums and products over both rectangular and non-rectangular domains.\n\nHere’s the code block you can copy and paste into your IDE:\n\n```\n#include <stdio.h>\n#include <stdbool.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <string.h>\n#include <math.h>\n\n//clear && gcc main.c -lm -o m.o && ./m.o\nconst int heegnerNumberList_internal[] = {1, 2, 3, 7, 11, 19, 43, 67, 163};\nconst int heegnerNumberCount_internal = 9;\ntypedef struct heegner_number_struct   HeegnerFloat;\ntypedef struct heegner_constant_struct HeegnerConstant;\ntypedef struct complex_number_struct Complex;\nstruct heegner_number_struct\n{\n\tdouble a;\n\tdouble b;\n};\nstruct heegner_constant_struct\n{\n\tdouble u;\n\tdouble R;\n\tint absHeegner;\n};\nstruct complex_number_struct\n{\n\tdouble x;\n\tdouble y;\n};\n\ndouble Random_Float(void)\n{\n\treturn ((double)(rand() % 20001) - 10000.0f) / 100.0f;\n}\n\nbool Complex_Equal(Complex a, Complex b, double epsilon)\n{\n\treturn fabs(a.x - b.x) < epsilon && fabs(a.y - b.y) < epsilon;\n}\n\nComplex HeegnerFloat_To_Complex(HeegnerFloat num0, HeegnerConstant heegnerConstant)\n{\n\t/*Convert Heegner coordinates to rectangular coordinates*/\n\tComplex result;\n\tresult.x = num0.a + num0.b * heegnerConstant.u;\n\tresult.y = num0.b * heegnerConstant.R;\n\treturn result;\n}\n\nComplex Complex_Add(Complex num0, Complex num1)\n{\n\tComplex result;\n\tresult.x = num0.x + num1.x;\n\tresult.y = num0.y + num1.y;\n\treturn result;\n}\n\nComplex Complex_Mul(Complex num0, Complex num1)\n{\n\tComplex result;\n\tresult.x = num0.x * num1.x - num0.y * num1.y;\n\tresult.y = num0.x * num1.y + num0.y * num1.x;\n\treturn result;\n}\n\nHeegnerConstant HeegnerConstant_Get(int index)\n{\n\tHeegnerConstant result;\n\tint r = heegnerNumberList_internal[index];\n\tresult.absHeegner = r;\n\tif(r % 4 == 3)              \n\t{\n\t\tresult.u = 0.5;\n\t\tresult.R = sqrt((double)r) / 2.0;\n\t}\n\telse\n\t{\n\t\tresult.u = 0.0;\n\t\tresult.R = sqrt((double)r);\n\t}\n\treturn result;\n}\n\nHeegnerFloat Complex_To_HeegnerFloat(Complex num0, HeegnerConstant heegnerConstant)\n{\n\t/*Convert rectangular coordinates to Heegner coordinates*/\n\tHeegnerFloat result;\n\tresult.a = num0.x - heegnerConstant.u * num0.y / heegnerConstant.R;\n\tresult.b = num0.y / heegnerConstant.R;\n\treturn result;\n}\n\nHeegnerFloat HeegnerFloat_Add(HeegnerFloat num0, HeegnerFloat num1)\n{\n\tHeegnerFloat result;\n\tresult.a = num0.a + num1.a;\n\tresult.b = num0.b + num1.b;\n\treturn result;\n}\n\nHeegnerFloat HeegnerFloat_Mul(HeegnerFloat num0,HeegnerFloat num1,HeegnerConstant heegnerConstant)\n{\n\tHeegnerFloat result;\n\tdouble c;\n\tif(heegnerConstant.absHeegner % 4 == 3){c = (heegnerConstant.absHeegner + 1) / 4.0;}\n\telse{c = (double)heegnerConstant.absHeegner;}\n\tresult.a = num0.a * num1.a - c * num0.b * num1.b;\n\tresult.b = num0.a * num1.b+ num0.b * num1.a+ 2.0 * heegnerConstant.u * num0.b * num1.b;\n\treturn result;\n}\n\nbool Test_Heegner_Conversion(void)\n{\n\t/*Floating point error precision*/\n\tconst double epsilon =  1e-10f;\n\t/*Random Heegner constant*/\n\tint index = rand() % heegnerNumberCount_internal;\n\tHeegnerConstant heegnerConstant = HeegnerConstant_Get(index);\n\t/*Random HeegnerFloat*/\n\tHeegnerFloat h0 = {Random_Float(), Random_Float()};\n\tHeegnerFloat h1 = {Random_Float(), Random_Float()};\n\t/*Convert HeegnerFloats to Complex*/\n\tComplex c0 = HeegnerFloat_To_Complex(h0, heegnerConstant);\n\tComplex c1 = HeegnerFloat_To_Complex(h1, heegnerConstant);\n\t/*Heegner domain addition*/\n\tHeegnerFloat hAdd = HeegnerFloat_Add(h0, h1);\n\t/*Complex domain addition*/\n\tComplex cAdd = Complex_Add(c0, c1);\n\t/*Convert Heegner addition result*/\n\tComplex hAddToComplex = HeegnerFloat_To_Complex(hAdd, heegnerConstant);\n\n\t/*Heegner domain product*/\n\tHeegnerFloat hMul = HeegnerFloat_Mul(h0, h1, heegnerConstant);\n\t/*Complex domain product*/\n\tComplex cMul = Complex_Mul(c0, c1);\n\t/*Convert Heegner prod result */\n\tComplex hMulToComplex = HeegnerFloat_To_Complex(hMul, heegnerConstant);\n\t/* Compare results */\n\tbool additionPass = Complex_Equal(hAddToComplex, cAdd, epsilon);\n\tbool multiplicationPass = Complex_Equal(hMulToComplex, cMul, epsilon);\n\n\tprintf(\"Heegner number: %d\\n\", heegnerConstant.absHeegner);\n\tprintf(\"Addition:       %s\\n\", additionPass ? \"PASS\" : \"FAIL\");\n\tprintf(\"Multiplication: %s\\n\", multiplicationPass ? \"PASS\" : \"FAIL\");\n\treturn additionPass && multiplicationPass;\n}\n\nint main()\n{\n\tsrand(4516);\n\tint tests = 10000;\n\tint passed = 0;\n\n\tfor(int i = 0; i < tests; i++)\n\t{\n\t\tif(Test_Heegner_Conversion())\n\t\t{\n\t\t\tpassed += 1;\n\t\t}\n\n\t}\n\n\tprintf(\"\\nResults: %d/%d passed\\n\", passed, tests);\n\n\treturn 0;\n}\n```\n\nFloating point error prevents complete test passes. Running this code on my computer yields:\n\n# 3.0 Standard Library Functions\n\nWe derived our core ops correctly. Now we construct our own standard library from first principles!\n\n## 3.1 Regular Complex Number Library\n\nThe ability to change base from regular complex numbers to quadratic complex primes is our silver bullet. So we need a comprehensive complex number library:\n\nWe further include complex exponentiation, logarithms, using the *principal exponential formula* (Mathwords, 2026)[6](#footnote-6). \n\nIn C, we have:\n\n## 3.2 Quadratic Complex Library\n\n### 3.2.1 Basic Arithmetic\n\nWe write add, sub, mul and div. In division, we convert to complex numbers, divide, then get back to Heegner floats:\n\nConjugate\n\n### 3.2.2 Norm Function\n\n[Part 1](https://leetarxiv.substack.com/p/special-q-descent-and-lattice-sieving-931) informed us the norm is multiplicative. This super useful as seen in the Pell Index Calculus paper and Discrete Log paper. We derive a norm function below:\n\nIn C:\n\nNOTE: The general norm function is unstable sometimes because of R. Use this one when integer high-precision is needed.\n\n### 3.2.3 Exponentiation and Logarithm Function\n\nWe aim to perform (`< a, b > = a + bω`)<sup>n</sup> where `n` is a float, for now. We use the notation `<A`<sub>n</sub>`,B`<sub>n</sub>`>` to represent `<a,b>`<sup>n</sup> as introduced in (Barber, 2022).\n\nThe generalized binomial theorem permits `n` be a float, a complex number, or whatever you want really. It tells us (Morris, 2026)[7](#footnote-7):\n\nThe downside to this approach is that summation fails to converge when abs(b/a) > 1.\n\nA better alternative is converting to rectangular coordinates, exponentiating, then reverse conversion.\n\nWe derive the approach below, where u and R are our HeegnerFloat constants:\n\nSo our power and log functions resemble:\n\nFor the curious, we can raise and find a log to complex exponents like this:\n\n### 3.2.4 Complex Continued Fractions\n\nThis is a number theory library, so lol I can show you how to invent your own quadratic complex prime continued fractions.\n\nFirst we write a function that takes a complex input and finds the nearest HeegnerFloat with integer coefficients:\n\nNext, we write a function to find continued fraction coefficients:\n\nFinally, we write a function that converts our coefficients into HeegnerFloat ‘rationals’:\n\nThe test function is available in the complete code section 4.0.\n\n### 3.2.5 Prime Factorization\n\nHeegnerFloats with integer coefficients (not all complex numbers) factorize into smaller HeegnerFloats uniquely. This is related to the fundamental theorem of arithmetic (Khandelwal, 2026)[8](#footnote-8). \n\nThe norm is multiplicative, so one factorizes the norm and finds the Jacobi symbol of each individual prime:\n\n1. Jacobi (-1). Prime is inert, ie p remains prime in the quadratic integer ring. The exponent must be even and we extract p and half its exponent.\n2. Jacobi (1). Prime splits. We use the lll trick to find one of the possible HeegnerFloats whose norm equals p. Then we test divisibility in the integer ring. If it fails then we take the conjugate.\n3. Jacobi (0). Prime ramifies. We replace with anything whose norm is p.\n\nCode for [2D gauss reduction](https://leetarxiv.substack.com/p/2-dimensional-lattice-basis-reduction) to solve for the norm is:\n\n```\nint FindGenerator(fmpz_t generator, fmpz_t prime, HeegnerConstant heegnerConstant)\n{\n\tfmpz_t rootOfUnity,twoInverse;\n\tfmpz_init(rootOfUnity);fmpz_init(twoInverse);\n\t\n\tfmpz_set_si(rootOfUnity, heegnerConstant.absHeegner * -1);\n\tint foundSquareRoot = fmpz_sqrtmod(rootOfUnity, rootOfUnity, prime);\n\tfmpz_set_ui(twoInverse, 2);\n\tfmpz_invmod(twoInverse, twoInverse, prime);\n\tif(foundSquareRoot == 0)\n\t{\n\t\tfmpz_clear(rootOfUnity);fmpz_clear(twoInverse);\n\t\treturn foundSquareRoot;\n\t}\n\telse\n\t{\n\t\tif(heegnerConstant.absHeegner % 4 == 3)\n\t\t{\n\t\t\tfmpz_add_ui(rootOfUnity, rootOfUnity, 1);\n\t\t\tfmpz_mul(rootOfUnity, rootOfUnity, twoInverse);\n\t\t\tfmpz_mod(rootOfUnity, rootOfUnity, prime);\n\t\t}\n\t}\n\tfmpz_set(generator, rootOfUnity);\n\tfmpz_clear(rootOfUnity);fmpz_clear(twoInverse);\n\treturn foundSquareRoot;\n}\n\nvoid LLL2D(fmpz_t x1, fmpz_t y1, fmpz_t x2, fmpz_t y2)\n{\n\tfmpz_t mu, dot12, dot11,temp0, temp02, temp03;\n\tfmpz_init(mu);fmpz_init(dot12);fmpz_init(dot11);fmpz_init(temp0);fmpz_init(temp02);fmpz_init(temp03);\n\tint infiniteLoopCheck = 0;\n\twhile(1)\n\t{\n\t\t/*dot12 = v1·v2 */\n\t\tfmpz_mul(dot12, x1, x2);fmpz_mul(temp0, y1, y2);fmpz_add(dot12, dot12, temp0);\n\t\t/*dot11 = v1·v1 */\n\t\tfmpz_mul(dot11, x1, x1);fmpz_mul(temp0, y1, y1);fmpz_add(dot11, dot11, temp0);\n\t\t/*avoid division by zero */\n\t\tif(fmpz_is_zero(dot11)){break;}\n\t\t/*mu = floor((v1·v2)/(v1·v1))*/\n\t\tfmpz_fdiv_q(mu, dot12, dot11);\n\t\t/*v2 = v2 - mu*v1 */\n\t\tfmpz_mul(temp0, mu, x1);fmpz_sub(x2, x2, temp0);fmpz_mul(temp0, mu, y1);fmpz_sub(y2, y2, temp0);\n\t\t/*||v1||^2 */\n\t\tfmpz_mul(temp0, x1, x1);fmpz_mul(temp02, y1, y1);fmpz_add(temp0, temp0, temp02);\n\t\t/*||v2||^2 */\n\t\tfmpz_mul(temp02, x2, x2);fmpz_mul(temp03, y2, y2);fmpz_add(temp02, temp02, temp03);\n\t\t/*If ||v2|| < ||v1|| then swap*/\n\t\tif(fmpz_cmp(temp02, temp0) < 0){fmpz_swap(x1, x2);fmpz_swap(y1, y2);}\n\t\telse{break;}\n\t\tinfiniteLoopCheck++;\n\t\tif(infiniteLoopCheck > INFINITE_LOOP_CHECK_SUM){printf(\"Gauss reduction may be in infinite loop\\n\");abort();}\n\t}\n\tfmpz_clear(mu);fmpz_clear(dot12);fmpz_clear(dot11);fmpz_clear(temp0);fmpz_clear(temp02);fmpz_clear(temp03);\n}\nint Heegner_FindShortestComplexPrime(fmpz_t a, fmpz_t b,fmpz_t numberFieldGenerator, fmpz_t prime, HeegnerConstant heegnerConstant, bool useSecondTV)\n{\n\tint solutionSearch = 0;\n\tint foundShortestComplexPrime = 0;\n\tfmpz_t a0,a1,b0,b1;\n\tfmpz_init(a0);fmpz_init(a1);fmpz_init(b0);fmpz_init(b1);\t\n\tif(heegnerConstant.absHeegner % 4 != 3)\n\t{\n\t\t//Solving a² + abs{negativeUnderRoot}b² = {p} using LLL\n\t\tfmpz_set(a0, prime);\n\t\tfmpz_set_ui(b0, 0);\n\t\tfmpz_set(a1, numberFieldGenerator);\n\t\tfmpz_set_ui(b1, 1);\n\t\tLLL2D(a0, b0, a1, b1);\n\t\t\n\t\tif(useSecondTV)\n\t\t{\n\t\t\tfmpz_set(a0, a1);\n\t\t\tfmpz_set(b0, b1);\n\t\t}\n\t\tfoundShortestComplexPrime = 1;\n\t}\n\telse\n\t{\n\t\t//Solve T+V(numberFieldGenerator) = 0 mod p\n\t\tfmpz_set(a0, prime);\n\t\tfmpz_set_ui(b0, 0);\n\t\tfmpz_mul_si(a1, numberFieldGenerator, -1);\n\t\tfmpz_set_ui(b1, 1);\n\t\tLLL2D(a0, b0, a1, b1);\n\t\tif(useSecondTV)\n\t\t{\n\t\t\tfmpz_set(a0, a1);\n\t\t\tfmpz_set(b0, b1);\n\t\t}\n\t\tfoundShortestComplexPrime = 1;\n\t}\n\tfmpz_set(a, a0);fmpz_set(b, b0);\n\tfmpz_clear(a0);fmpz_clear(a1);fmpz_clear(b0);fmpz_clear(b1);\t\n\treturn foundShortestComplexPrime;\n}\n```\n\nThen we use this for our prime factorizer:\n\n# 4.0 Complete code\n\n```\n#include <stdio.h>\n#include <stdbool.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <string.h>\n#include <math.h>\n#include <assert.h>\n\n//clear && gcc cf.c -lm -o cf && ./cf\nconst int heegnerNumberList_internal[] ={1, 2, 3, 7, 11, 19, 43, 67, 163};\nconst int heegnerNumberCount_internal = 9;\ntypedef struct heegner_number_struct HeegnerFloat;\ntypedef struct heegner_constant_struct HeegnerConstant;\ntypedef struct complex_number_struct Complex;\nstruct heegner_number_struct\n{\n\tdouble a;\n\tdouble b;\n};\nstruct heegner_constant_struct\n{\n\tdouble u;\n\tdouble R;\n\tint absHeegner;\n};\nstruct complex_number_struct\n{\n\tdouble x;\n\tdouble y;\n};\ndouble Random_Float(void){return ((double)(rand() % 20001) - 10000.0) / 100.0;}\nbool Complex_Equal(Complex a, Complex b, double epsilon)\n{\n\tdouble dx = fabs(a.x - b.x);\n\tdouble dy = fabs(a.y - b.y);\n\tdouble scale = fmax(1.0,fmax(hypot(a.x, a.y), hypot(b.x, b.y)));\n\treturn dx <= epsilon * scale &&dy <= epsilon * scale;\n}\nComplex Complex_Add(Complex a, Complex b)\n{\n\tComplex result;\n\tresult.x = a.x + b.x;result.y = a.y + b.y;\n\treturn result;\n}\nComplex Complex_Sub(Complex a, Complex b)\n{\n\tComplex result;\n\tresult.x = a.x - b.x;result.y = a.y - b.y;\n\treturn result;\n}\nComplex Complex_Mul(Complex a, Complex b)\n{\n\tComplex result;\n\tresult.x = a.x * b.x - a.y * b.y;result.y = a.x * b.y + a.y * b.x;\n\treturn result;\n}\nComplex Complex_Div(Complex a, Complex b)\n{\n\tComplex result;\n\tdouble denominator = b.x*b.x + b.y*b.y;\n\tif(denominator == 0.0){result.x = NAN;result.y = NAN;return result;}\n\tresult.x = (a.x*b.x + a.y*b.y) / denominator;result.y = (a.y*b.x - a.x*b.y) / denominator;\n\treturn result;\n}\ndouble Complex_Abs(Complex z){return hypot(z.x, z.y);}\ndouble Complex_Arg(Complex z){return atan2(z.y, z.x);}\nComplex Complex_Exp(Complex z)\n{\n\tComplex result;\n\tdouble magnitude = exp(z.x);\n\tresult.x = magnitude * cos(z.y);\n\tresult.y = magnitude * sin(z.y);\n\treturn result;\n}\nComplex Complex_Log(Complex z)\n{\n\tComplex result;\n\tresult.x = log(Complex_Abs(z));\n\tresult.y = Complex_Arg(z);\n\treturn result;\n}\nComplex Complex_Power(Complex base, double exponent)\n{\n\tComplex logarithm = Complex_Log(base);\n\tComplex scaled;\n\tscaled.x = exponent * logarithm.x;\n\tscaled.y = exponent * logarithm.y;\n\treturn Complex_Exp(scaled);\n}\n\nComplex HeegnerFloat_To_Complex(HeegnerFloat num0,HeegnerConstant hc)\n{\n\tComplex result;\n\tresult.x = num0.a + num0.b * hc.u;\n\tresult.y = num0.b * hc.R;\n\treturn result;\n}\nHeegnerFloat Complex_To_HeegnerFloat(Complex num0,HeegnerConstant hc)\n{\n\tHeegnerFloat result;\n\tresult.a = num0.x - hc.u * num0.y / hc.R;\n\tresult.b = num0.y / hc.R;\n\treturn result;\n}\nHeegnerConstant HeegnerConstant_Get(int index)\n{\n\tHeegnerConstant result = {0};\n\tif(index < 0 || index >= heegnerNumberCount_internal)return result;\n\tint r = heegnerNumberList_internal[index];\n\tresult.absHeegner = r;\n\tif(r % 4 == 3)\n\t{\n\t\tresult.u = 0.5;\n\t\tresult.R = sqrt((double)r) / 2.0;\n\t}\n\telse\n\t{\n\t\tresult.u = 0.0;\n\t\tresult.R = sqrt((double)r);\n\t}\n\treturn result;\n}\n\nHeegnerFloat HeegnerFloat_Add(HeegnerFloat num0,HeegnerFloat num1)\n{\n\tHeegnerFloat result;result.a = num0.a + num1.a;result.b = num0.b + num1.b;\n\treturn result;\n}\nHeegnerFloat HeegnerFloat_Sub(HeegnerFloat num0,HeegnerFloat num1)\n{\n\tHeegnerFloat result;result.a = num0.a - num1.a;result.b = num0.b - num1.b;\n\treturn result;\n}\nHeegnerFloat HeegnerFloat_Mul(HeegnerFloat num0,HeegnerFloat num1,HeegnerConstant hc)\n{\n\tHeegnerFloat result;double c;\n\tif(hc.absHeegner % 4 == 3){c = (hc.absHeegner + 1) / 4.0;}\n\telse{c = (double)hc.absHeegner;}\n\tresult.a = num0.a * num1.a- c * num0.b * num1.b;\n\tresult.b = num0.a * num1.b+ num0.b * num1.a+ 2.0 * hc.u * num0.b * num1.b;\n\treturn result;\n}\nbool HeegnerFloat_Div(HeegnerFloat num0,HeegnerFloat num1,HeegnerConstant hc,HeegnerFloat *result)\n{\n\tif(result == NULL || hc.R == 0.0)return false;\n\tComplex a = HeegnerFloat_To_Complex(num0, hc);Complex b = HeegnerFloat_To_Complex(num1, hc);\n\tif(Complex_Abs(b) == 0.0)return false;\n\tComplex answer = Complex_Div(a, b);\n\t*result = Complex_To_HeegnerFloat(answer, hc);\n\treturn true;\n}\n\ndouble HeegnerFloat_Norm(HeegnerFloat num0, HeegnerConstant hc)\n{\n\tdouble c = hc.u * hc.u + hc.R * hc.R;\n\treturn num0.a * num0.a+ 2.0 * hc.u * num0.a * num0.b + c * num0.b * num0.b;\n}\n\nbool HeegnerFloat_Power(HeegnerFloat base,double exponent,HeegnerConstant hc,HeegnerFloat *result)\n{\n\tif(result == NULL || hc.R == 0.0)return false;\n\tComplex z = HeegnerFloat_To_Complex(base, hc);\n\tif(Complex_Abs(z) == 0.0)\n\t{\n\t\tif(exponent > 0.0){result->a = 0.0;result->b = 0.0;return true;}\n\t\treturn false;\n\t}\n\tComplex answer = Complex_Power(z, exponent);\n\t*result = Complex_To_HeegnerFloat(answer, hc);\n\treturn true;\n}\nbool HeegnerFloat_FindRealLog(HeegnerFloat base,HeegnerFloat target,HeegnerConstant hc,double *exponent,double tolerance)\n{\n\tif(exponent == NULL || hc.R == 0.0)return false;\n\tComplex z = HeegnerFloat_To_Complex(base, hc);Complex t = HeegnerFloat_To_Complex(target, hc);\n\tdouble r = Complex_Abs(z);\n\tdouble s = Complex_Abs(t);\n\tif(r == 0.0 || s == 0.0)return false;\n\tif(fabs(r - 1.0) < tolerance)return false;\n\tdouble n = log(s) / log(r);\n\tComplex check = Complex_Power(z, n);\n\tdouble error = hypot(check.x - t.x,check.y - t.y);\n\tif(error > tolerance * fmax(1.0, s))return false;\n\t*exponent = n;\n\treturn true;\n}\nbool HeegnerFloat_LogGeneral(HeegnerFloat value,HeegnerFloat base,HeegnerConstant hc,HeegnerFloat *result)\n{\n\tif(result == NULL || hc.R == 0.0)return false;\n\tComplex z = HeegnerFloat_To_Complex(value, hc);\n\tComplex b = HeegnerFloat_To_Complex(base, hc);\n\tif(Complex_Abs(z) == 0.0 ||Complex_Abs(b) == 0.0)return false;\n\tComplex logValue = Complex_Log(z);\n\tComplex logBase = Complex_Log(b);\n\tif(Complex_Abs(logBase) < 1e-14)return false;\n\tComplex answer = Complex_Div(logValue, logBase);\n\tif(!isfinite(answer.x) || !isfinite(answer.y))return false;\n\t*result = Complex_To_HeegnerFloat(answer, hc);\n\treturn true;\n}\nbool HeegnerFloat_PowerGeneral(HeegnerFloat base,HeegnerFloat exponent,HeegnerConstant hc,HeegnerFloat *result)\n{\n\tif(result == NULL || hc.R == 0.0)return false;\n\tComplex z = HeegnerFloat_To_Complex(base, hc);\n\tComplex e = HeegnerFloat_To_Complex(exponent, hc);\n\tif(Complex_Abs(z) == 0.0)return false;\n\tComplex logarithm = Complex_Log(z);\n\tComplex product = Complex_Mul(e, logarithm);\n\tComplex answer = Complex_Exp(product);\n\tif(!isfinite(answer.x) || !isfinite(answer.y))return false;\n\t*result = Complex_To_HeegnerFloat(answer, hc);\n\treturn true;\n}\nHeegnerFloat HeegnerFloat_NearestInteger(Complex input, HeegnerConstant hc)\n{\n\tHeegnerFloat bestEstimate = Complex_To_HeegnerFloat(input, hc);\n\tint a0 = (int)round(bestEstimate.a);\n\tint b0 = (int)round(bestEstimate.b);\n\tdouble bestNorm = INFINITY;\n\t//Find close lattice points\n\tfor(int a = a0 - 1; a <= a0 + 1; a++)\n\t{\n\t\tfor(int b = b0 - 1; b <= b0 + 1; b++)\n\t\t{\n\t\t\tHeegnerFloat candidate = {(double)a, (double)b};\n\t\t\tComplex complexCandidate = HeegnerFloat_To_Complex(candidate, hc);\n\t\t\tComplex remainder = Complex_Sub(input, complexCandidate);\n\t\t\tdouble errorNorm = Complex_Abs(remainder);\n\t\t\terrorNorm *= errorNorm;\n\t\t\tif(errorNorm < bestNorm)\n\t\t\t{\n\t\t\t\tbestNorm = errorNorm;\n\t\t\t\tbestEstimate.a = candidate.a;\n\t\t\t\tbestEstimate.b = candidate.b;\n\t\t\t}\n\t\t}\n\t}\n\treturn bestEstimate;\n}\nsize_t HeegnerFloat_ContinuedFraction(size_t maxTerms, HeegnerFloat coefficients[maxTerms], Complex z,HeegnerConstant hc)\n{\n\tComplex current = z;\n\tComplex one = {1.0, 0.0};\n\tsize_t count = 0;\n\tfor(size_t i = 0; i < maxTerms; i++)\n\t{\n\t\tcoefficients[i] = HeegnerFloat_NearestInteger(current,hc);\n\t\tcount += 1;\n\t\tComplex complexCandidate = HeegnerFloat_To_Complex(coefficients[i], hc);\n\t\tComplex remainder = Complex_Sub(current, complexCandidate);\n\t\tdouble errorNorm = Complex_Abs(remainder);\n\t\terrorNorm *= errorNorm;\n\t\tif(errorNorm < 1e-20)break;\n\t\tcurrent = Complex_Div(one, remainder);\n\t}\n\treturn count;\n}\n\nsize_t HeegnerFloat_Convergents(size_t count,HeegnerFloat coefficients[count],HeegnerFloat numerators[count],HeegnerFloat denominators[count],HeegnerConstant hc)\n{\n\tif(count == 0) return 0;\n\tHeegnerFloat pPrevious = {1.0, 0.0};\n\tHeegnerFloat pCurrent = coefficients[0];\n\tHeegnerFloat qPrevious = {0.0, 0.0};\n\tHeegnerFloat qCurrent = {1.0, 0.0};\n\tnumerators[0] = pCurrent;\n\tdenominators[0] = qCurrent;\n\n\tfor(size_t i = 1; i < count; i++)\n\t{\n\t\tHeegnerFloat pNext = HeegnerFloat_Add(HeegnerFloat_Mul(coefficients[i], pCurrent, hc),pPrevious);\n\t\tHeegnerFloat qNext = HeegnerFloat_Add(HeegnerFloat_Mul(coefficients[i], qCurrent, hc),qPrevious);\n\t\tpPrevious = pCurrent;\n\t\tpCurrent = pNext;\n\t\tqPrevious = qCurrent;\n\t\tqCurrent = qNext;\n\t\tnumerators[i] = pCurrent;\n\t\tdenominators[i] = qCurrent;\n\t}\n\treturn count;\n}\n\nint TestLog()\n{\n\tsrand(4516);\n\tint index = 2;\n\tHeegnerConstant hc = HeegnerConstant_Get(index);\n\tHeegnerFloat original = {2.35437451, 1.78687861};\n\tdouble n = 3;\n\n\tHeegnerFloat powered;HeegnerFloat logarithm;HeegnerFloat generalResult;\n\tdouble recoveredExponent;\n\tprintf(\"\\nHeegner number: %d\\n\", hc.absHeegner);\n\tprintf(\"Omega = %.10f + %.10fi\\n\", hc.u, hc.R);\n\tprintf(\"Original pair:<%.10f, %.10f>\\n\", original.a, original.b);\n\tprintf(\"\\nOriginal exponent:%.10f\\n\",n);\n\tassert(HeegnerFloat_Power(original, n, hc, &powered));\n\tprintf(\"Result = <%.10f, %.10f>\\n\",powered.a, powered.b);\n\tassert(HeegnerFloat_FindRealLog(original,powered,hc,&recoveredExponent,1e-8));\n\tprintf(\"Original exponent  = %.10f\\n\", n);\n\tprintf(\"Recovered exponent = %.10f\\n\\n\",recoveredExponent);\n\n\treturn 0;\n}\n\nvoid TestContinuedFraction()\n{\n\tint index = 2;\n\tsize_t maxTerms = 30;\n\tHeegnerConstant hc = HeegnerConstant_Get(index);\n\tHeegnerFloat original = {2.35437451, 1.78687861};\n\tComplex z = HeegnerFloat_To_Complex(original, hc);\n\tHeegnerFloat coefficients[maxTerms];\n\tHeegnerFloat numerators[maxTerms];\n\tHeegnerFloat denominators[maxTerms];\n\tsize_t coefficientCount = HeegnerFloat_ContinuedFraction(maxTerms, coefficients, z, hc);\n\tHeegnerFloat_Convergents(coefficientCount, coefficients, numerators, denominators, hc);\n\tprintf(\"\\nHeegner number: %d\\n\", hc.absHeegner);\n\tprintf(\"Omega = %.10f + %.10fi\\n\", hc.u, hc.R);\n\tprintf(\"Original Heegner:<%.10f, %.10f>\\n\", original.a, original.b);\n\tprintf(\"Original Complex:<%.10f, %.10f>\\n\", z.x, z.y);\n\tprintf(\"\\nCoefficient count: %ld\\n\", coefficientCount);\n\tfor(size_t i = 0; i < coefficientCount; i++)\n\t{\n\t\tHeegnerFloat convergent;\n\t\tHeegnerFloat_Div(numerators[i], denominators[i], hc, &convergent);\n\t\tComplex approximation = HeegnerFloat_To_Complex(convergent, hc);\n\t\tComplex error = Complex_Sub(z, approximation);\n\t\tdouble errorAbs = Complex_Abs(error);\n\t\tdouble errorNorm = errorAbs * errorAbs;\n\t\tprintf(\"%zu: P=<%8.1f, %8.1f>  Q=<%8.1f, %8.1f> Error=%.12e Norm=%.12e\\n\\n\",i,numerators[i].a,numerators[i].b,denominators[i].a,denominators[i].b,errorAbs,errorNorm);\n\t}\n}\n\nint main()\n{\n\tTestContinuedFraction();\n\treturn 0;\n}\n```\n\n# References\n\n[1](#footnote-anchor-1)\n\nOpenStax Authors. (2026). **2.4 Complex Numbers**. In *College Algebra with Corequisite Support 2e*. [Link](https://openstax.org/books/college-algebra-corequisite-support-2e/pages/2-4-complex-numbers).\n\n[4](#footnote-anchor-4)\n\nRoberts, D. (2026). **Arithmetic of Complex Numbers (Add, Subtract, Multiply)**. In *MathBitsNotebook Algebra 2*. [Link](https://mathbitsnotebook.com/Algebra2/ComplexNumbers/CPArithmeticASM.html).\n\n[5](#footnote-anchor-5)\n\nDummit, D. S. (2021). **Number Theory 2: Lecture #21 of 38: Arithmetic in Domains + Quadratic Integer Rings**. Northeastern University lecture notes. [PDF Link](https://dummit.cos.northeastern.edu/teaching_sp21_4527/4527_lecture_22_arithmetic_in_domains_quadratic_rings.pdf).\n\n[6](#footnote-anchor-6)\n\nMorris, J. (2026). **The Generalized Binomial Theorem**. In *Combinatorics (Morris)*, Libretexts Mathematics. University of Lethbridge. Link.", "url": "https://wpnews.pro/news/inventing-your-own-math-as-a-computer-programmer", "canonical_source": "https://leetarxiv.substack.com/p/inventing-your-own-math-as-a-computer-programmer", "published_at": "2026-11-21 19:45:31+00:00", "updated_at": "2026-11-21 20:14:59.971168+00:00", "lang": "en", "topics": ["artificial-intelligence", "neural-networks", "machine-learning"], "entities": ["Leopold Kronecker", "OpenStax", "Barber", "Graycuber", "Roberts", "Dummit"], "also_reported_by": [], "takeaway": "Extending integer-only algebraic number theory code to floating point requires re-deriving field roots via √-r = i√r and converting between rectangular and lattice coordinate systems, while division must be omitted for numerical instability.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/inventing-your-own-math-as-a-computer-programmer", "markdown": "https://wpnews.pro/news/inventing-your-own-math-as-a-computer-programmer.md", "text": "https://wpnews.pro/news/inventing-your-own-math-as-a-computer-programmer.txt", "jsonld": "https://wpnews.pro/news/inventing-your-own-math-as-a-computer-programmer.jsonld"}}