God made the integers, all the rest is the work of man.
~ Leopold Kronecker
Part 1 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.
However, modern computers run on floating-point arithmetic, not integer operations in a finite field.
Now, we must invent our own math to accommodate floating point operations in our algebraic number theory library.
We’re building a complex number unit. It’s a CPU designed for working with imaginary numbers. Useful for neural net quantization, factorization, pell curves and discrete logs.
1.1 Quick Primer #
We covered all this in detail in Part 1. Our library works with a subset of complex prime numbers of the form:
Where r is drawn from the Heegner numbers:
When r = 2, this complex quadratic primes are the Gaussian integers. r = 3 is called an Eisenstein integer.
Arithmetic happens in a ring or a field depending on r. Here’s a quick cheatsheet:
<sup>*</sup>Jump to Section 2.4 for the runnable code block you can copy and paste
This section is a step-by-step guide to accommodating floating point ops using complex numbers. Here are some super salient identities (OpenStax, 2026)1:
We 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.
C code for our structs:
2.1 Finding Number Field Roots and Generators #
In Part 1, 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:
Now, we extend root evalution to floating-point via the identity below:
√-r = i√r (Square root of a negative number identity)
For instance, the number field root for the eisentein integers becomes:
√-3 = i√3
Then, the number field generator, ω, for the eisenstein integers becomes:
Generally, we define ω as:
where
and
C code for this initialization:
2.2 Changing Coordinate Systems #
Regular 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):
The 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:
If you’re here for a good time, then coordinate conversion is done as:
If you’re here for a long time then here’s the derivation:
Change of Coordinate Derivation
C code for conversion is:
2.3 Complex Arithmetic #
High school math taught us addition and multiplication with complex numbers in rectangular coordinates resembles (Roberts, 2026)4:
C code looks like:
Arithmetic in complex quadratic domains depends on the Heegner number (Dummit, 2021)5 and here are the rules:
Division is handwavy somewhat. So we exclude it. Here’s the C code:
Our C code doesn’t look like the formula due to numerical instablity, but the formula works in Python:
def multiply_heegner_float(z1, z2, u, R):
a1, b1 = z1
a2, b2 = z2
return (
a1*a2 - (u*u + R*R)*b1*b2,
a1*b2 + a2*b1 + 2*u*b1*b2
)
2.4 Test Conversion and Arithmetic #
We 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.
Here’s the code block you can copy and paste into your IDE:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
//clear && gcc main.c -lm -o m.o && ./m.o
const int heegnerNumberList_internal[] = {1, 2, 3, 7, 11, 19, 43, 67, 163};
const int heegnerNumberCount_internal = 9;
typedef struct heegner_number_struct HeegnerFloat;
typedef struct heegner_constant_struct HeegnerConstant;
typedef struct complex_number_struct Complex;
struct heegner_number_struct
{
double a;
double b;
};
struct heegner_constant_struct
{
double u;
double R;
int absHeegner;
};
struct complex_number_struct
{
double x;
double y;
};
double Random_Float(void)
{
return ((double)(rand() % 20001) - 10000.0f) / 100.0f;
}
bool Complex_Equal(Complex a, Complex b, double epsilon)
{
return fabs(a.x - b.x) < epsilon && fabs(a.y - b.y) < epsilon;
}
Complex HeegnerFloat_To_Complex(HeegnerFloat num0, HeegnerConstant heegnerConstant)
{
/*Convert Heegner coordinates to rectangular coordinates*/
Complex result;
result.x = num0.a + num0.b * heegnerConstant.u;
result.y = num0.b * heegnerConstant.R;
return result;
}
Complex Complex_Add(Complex num0, Complex num1)
{
Complex result;
result.x = num0.x + num1.x;
result.y = num0.y + num1.y;
return result;
}
Complex Complex_Mul(Complex num0, Complex num1)
{
Complex result;
result.x = num0.x * num1.x - num0.y * num1.y;
result.y = num0.x * num1.y + num0.y * num1.x;
return result;
}
HeegnerConstant HeegnerConstant_Get(int index)
{
HeegnerConstant result;
int r = heegnerNumberList_internal[index];
result.absHeegner = r;
if(r % 4 == 3)
{
result.u = 0.5;
result.R = sqrt((double)r) / 2.0;
}
else
{
result.u = 0.0;
result.R = sqrt((double)r);
}
return result;
}
HeegnerFloat Complex_To_HeegnerFloat(Complex num0, HeegnerConstant heegnerConstant)
{
/*Convert rectangular coordinates to Heegner coordinates*/
HeegnerFloat result;
result.a = num0.x - heegnerConstant.u * num0.y / heegnerConstant.R;
result.b = num0.y / heegnerConstant.R;
return result;
}
HeegnerFloat HeegnerFloat_Add(HeegnerFloat num0, HeegnerFloat num1)
{
HeegnerFloat result;
result.a = num0.a + num1.a;
result.b = num0.b + num1.b;
return result;
}
HeegnerFloat HeegnerFloat_Mul(HeegnerFloat num0,HeegnerFloat num1,HeegnerConstant heegnerConstant)
{
HeegnerFloat result;
double c;
if(heegnerConstant.absHeegner % 4 == 3){c = (heegnerConstant.absHeegner + 1) / 4.0;}
else{c = (double)heegnerConstant.absHeegner;}
result.a = num0.a * num1.a - c * num0.b * num1.b;
result.b = num0.a * num1.b+ num0.b * num1.a+ 2.0 * heegnerConstant.u * num0.b * num1.b;
return result;
}
bool Test_Heegner_Conversion(void)
{
/*Floating point error precision*/
const double epsilon = 1e-10f;
/*Random Heegner constant*/
int index = rand() % heegnerNumberCount_internal;
HeegnerConstant heegnerConstant = HeegnerConstant_Get(index);
/*Random HeegnerFloat*/
HeegnerFloat h0 = {Random_Float(), Random_Float()};
HeegnerFloat h1 = {Random_Float(), Random_Float()};
/*Convert HeegnerFloats to Complex*/
Complex c0 = HeegnerFloat_To_Complex(h0, heegnerConstant);
Complex c1 = HeegnerFloat_To_Complex(h1, heegnerConstant);
/*Heegner domain addition*/
HeegnerFloat hAdd = HeegnerFloat_Add(h0, h1);
/*Complex domain addition*/
Complex cAdd = Complex_Add(c0, c1);
/*Convert Heegner addition result*/
Complex hAddToComplex = HeegnerFloat_To_Complex(hAdd, heegnerConstant);
/*Heegner domain product*/
HeegnerFloat hMul = HeegnerFloat_Mul(h0, h1, heegnerConstant);
/*Complex domain product*/
Complex cMul = Complex_Mul(c0, c1);
/*Convert Heegner prod result */
Complex hMulToComplex = HeegnerFloat_To_Complex(hMul, heegnerConstant);
/* Compare results */
bool additionPass = Complex_Equal(hAddToComplex, cAdd, epsilon);
bool multiplicationPass = Complex_Equal(hMulToComplex, cMul, epsilon);
printf("Heegner number: %d\n", heegnerConstant.absHeegner);
printf("Addition: %s\n", additionPass ? "PASS" : "FAIL");
printf("Multiplication: %s\n", multiplicationPass ? "PASS" : "FAIL");
return additionPass && multiplicationPass;
}
int main()
{
srand(4516);
int tests = 10000;
int passed = 0;
for(int i = 0; i < tests; i++)
{
if(Test_Heegner_Conversion())
{
passed += 1;
}
}
printf("\nResults: %d/%d passed\n", passed, tests);
return 0;
}
Floating point error prevents complete test passes. Running this code on my computer yields:
We derived our core ops correctly. Now we construct our own standard library from first principles!
3.1 Regular Complex Number Library #
The ability to change base from regular complex numbers to quadratic complex primes is our silver bullet. So we need a comprehensive complex number library:
We further include complex exponentiation, logarithms, using the principal exponential formula (Mathwords, 2026)6.
In C, we have:
3.2 Quadratic Complex Library #
3.2.1 Basic Arithmetic
We write add, sub, mul and div. In division, we convert to complex numbers, divide, then get back to Heegner floats:
Conjugate
3.2.2 Norm Function
Part 1 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:
In C:
NOTE: The general norm function is unstable sometimes because of R. Use this one when integer high-precision is needed.
3.2.3 Exponentiation and Logarithm Function
We 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).
The generalized binomial theorem permits n be a float, a complex number, or whatever you want really. It tells us (Morris, 2026)7:
The downside to this approach is that summation fails to converge when abs(b/a) > 1.
A better alternative is converting to rectangular coordinates, exponentiating, then reverse conversion.
We derive the approach below, where u and R are our HeegnerFloat constants:
So our power and log functions resemble:
For the curious, we can raise and find a log to complex exponents like this:
3.2.4 Complex Continued Fractions
This is a number theory library, so lol I can show you how to invent your own quadratic complex prime continued fractions.
First we write a function that takes a complex input and finds the nearest HeegnerFloat with integer coefficients:
Next, we write a function to find continued fraction coefficients:
Finally, we write a function that converts our coefficients into HeegnerFloat ‘rationals’:
The test function is available in the complete code section 4.0.
3.2.5 Prime Factorization
HeegnerFloats with integer coefficients (not all complex numbers) factorize into smaller HeegnerFloats uniquely. This is related to the fundamental theorem of arithmetic (Khandelwal, 2026)8.
The norm is multiplicative, so one factorizes the norm and finds the Jacobi symbol of each individual prime:
- 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.
- 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.
- Jacobi (0). Prime ramifies. We replace with anything whose norm is p.
Code for 2D gauss reduction to solve for the norm is:
int FindGenerator(fmpz_t generator, fmpz_t prime, HeegnerConstant heegnerConstant)
{
fmpz_t rootOfUnity,twoInverse;
fmpz_init(rootOfUnity);fmpz_init(twoInverse);
fmpz_set_si(rootOfUnity, heegnerConstant.absHeegner * -1);
int foundSquareRoot = fmpz_sqrtmod(rootOfUnity, rootOfUnity, prime);
fmpz_set_ui(twoInverse, 2);
fmpz_invmod(twoInverse, twoInverse, prime);
if(foundSquareRoot == 0)
{
fmpz_clear(rootOfUnity);fmpz_clear(twoInverse);
return foundSquareRoot;
}
else
{
if(heegnerConstant.absHeegner % 4 == 3)
{
fmpz_add_ui(rootOfUnity, rootOfUnity, 1);
fmpz_mul(rootOfUnity, rootOfUnity, twoInverse);
fmpz_mod(rootOfUnity, rootOfUnity, prime);
}
}
fmpz_set(generator, rootOfUnity);
fmpz_clear(rootOfUnity);fmpz_clear(twoInverse);
return foundSquareRoot;
}
void LLL2D(fmpz_t x1, fmpz_t y1, fmpz_t x2, fmpz_t y2)
{
fmpz_t mu, dot12, dot11,temp0, temp02, temp03;
fmpz_init(mu);fmpz_init(dot12);fmpz_init(dot11);fmpz_init(temp0);fmpz_init(temp02);fmpz_init(temp03);
int infiniteLoopCheck = 0;
while(1)
{
/*dot12 = v1·v2 */
fmpz_mul(dot12, x1, x2);fmpz_mul(temp0, y1, y2);fmpz_add(dot12, dot12, temp0);
/*dot11 = v1·v1 */
fmpz_mul(dot11, x1, x1);fmpz_mul(temp0, y1, y1);fmpz_add(dot11, dot11, temp0);
/*avoid division by zero */
if(fmpz_is_zero(dot11)){break;}
/*mu = floor((v1·v2)/(v1·v1))*/
fmpz_fdiv_q(mu, dot12, dot11);
/*v2 = v2 - mu*v1 */
fmpz_mul(temp0, mu, x1);fmpz_sub(x2, x2, temp0);fmpz_mul(temp0, mu, y1);fmpz_sub(y2, y2, temp0);
/*||v1||^2 */
fmpz_mul(temp0, x1, x1);fmpz_mul(temp02, y1, y1);fmpz_add(temp0, temp0, temp02);
/*||v2||^2 */
fmpz_mul(temp02, x2, x2);fmpz_mul(temp03, y2, y2);fmpz_add(temp02, temp02, temp03);
/*If ||v2|| < ||v1|| then swap*/
if(fmpz_cmp(temp02, temp0) < 0){fmpz_swap(x1, x2);fmpz_swap(y1, y2);}
else{break;}
infiniteLoopCheck++;
if(infiniteLoopCheck > INFINITE_LOOP_CHECK_SUM){printf("Gauss reduction may be in infinite loop\n");abort();}
}
fmpz_clear(mu);fmpz_clear(dot12);fmpz_clear(dot11);fmpz_clear(temp0);fmpz_clear(temp02);fmpz_clear(temp03);
}
int Heegner_FindShortestComplexPrime(fmpz_t a, fmpz_t b,fmpz_t numberFieldGenerator, fmpz_t prime, HeegnerConstant heegnerConstant, bool useSecondTV)
{
int solutionSearch = 0;
int foundShortestComplexPrime = 0;
fmpz_t a0,a1,b0,b1;
fmpz_init(a0);fmpz_init(a1);fmpz_init(b0);fmpz_init(b1);
if(heegnerConstant.absHeegner % 4 != 3)
{
//Solving a² + abs{negativeUnderRoot}b² = {p} using LLL
fmpz_set(a0, prime);
fmpz_set_ui(b0, 0);
fmpz_set(a1, numberFieldGenerator);
fmpz_set_ui(b1, 1);
LLL2D(a0, b0, a1, b1);
if(useSecondTV)
{
fmpz_set(a0, a1);
fmpz_set(b0, b1);
}
foundShortestComplexPrime = 1;
}
else
{
//Solve T+V(numberFieldGenerator) = 0 mod p
fmpz_set(a0, prime);
fmpz_set_ui(b0, 0);
fmpz_mul_si(a1, numberFieldGenerator, -1);
fmpz_set_ui(b1, 1);
LLL2D(a0, b0, a1, b1);
if(useSecondTV)
{
fmpz_set(a0, a1);
fmpz_set(b0, b1);
}
foundShortestComplexPrime = 1;
}
fmpz_set(a, a0);fmpz_set(b, b0);
fmpz_clear(a0);fmpz_clear(a1);fmpz_clear(b0);fmpz_clear(b1);
return foundShortestComplexPrime;
}
Then we use this for our prime factorizer:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <assert.h>
//clear && gcc cf.c -lm -o cf && ./cf
const int heegnerNumberList_internal[] ={1, 2, 3, 7, 11, 19, 43, 67, 163};
const int heegnerNumberCount_internal = 9;
typedef struct heegner_number_struct HeegnerFloat;
typedef struct heegner_constant_struct HeegnerConstant;
typedef struct complex_number_struct Complex;
struct heegner_number_struct
{
double a;
double b;
};
struct heegner_constant_struct
{
double u;
double R;
int absHeegner;
};
struct complex_number_struct
{
double x;
double y;
};
double Random_Float(void){return ((double)(rand() % 20001) - 10000.0) / 100.0;}
bool Complex_Equal(Complex a, Complex b, double epsilon)
{
double dx = fabs(a.x - b.x);
double dy = fabs(a.y - b.y);
double scale = fmax(1.0,fmax(hypot(a.x, a.y), hypot(b.x, b.y)));
return dx <= epsilon * scale &&dy <= epsilon * scale;
}
Complex Complex_Add(Complex a, Complex b)
{
Complex result;
result.x = a.x + b.x;result.y = a.y + b.y;
return result;
}
Complex Complex_Sub(Complex a, Complex b)
{
Complex result;
result.x = a.x - b.x;result.y = a.y - b.y;
return result;
}
Complex Complex_Mul(Complex a, Complex b)
{
Complex result;
result.x = a.x * b.x - a.y * b.y;result.y = a.x * b.y + a.y * b.x;
return result;
}
Complex Complex_Div(Complex a, Complex b)
{
Complex result;
double denominator = b.x*b.x + b.y*b.y;
if(denominator == 0.0){result.x = NAN;result.y = NAN;return result;}
result.x = (a.x*b.x + a.y*b.y) / denominator;result.y = (a.y*b.x - a.x*b.y) / denominator;
return result;
}
double Complex_Abs(Complex z){return hypot(z.x, z.y);}
double Complex_Arg(Complex z){return atan2(z.y, z.x);}
Complex Complex_Exp(Complex z)
{
Complex result;
double magnitude = exp(z.x);
result.x = magnitude * cos(z.y);
result.y = magnitude * sin(z.y);
return result;
}
Complex Complex_Log(Complex z)
{
Complex result;
result.x = log(Complex_Abs(z));
result.y = Complex_Arg(z);
return result;
}
Complex Complex_Power(Complex base, double exponent)
{
Complex logarithm = Complex_Log(base);
Complex scaled;
scaled.x = exponent * logarithm.x;
scaled.y = exponent * logarithm.y;
return Complex_Exp(scaled);
}
Complex HeegnerFloat_To_Complex(HeegnerFloat num0,HeegnerConstant hc)
{
Complex result;
result.x = num0.a + num0.b * hc.u;
result.y = num0.b * hc.R;
return result;
}
HeegnerFloat Complex_To_HeegnerFloat(Complex num0,HeegnerConstant hc)
{
HeegnerFloat result;
result.a = num0.x - hc.u * num0.y / hc.R;
result.b = num0.y / hc.R;
return result;
}
HeegnerConstant HeegnerConstant_Get(int index)
{
HeegnerConstant result = {0};
if(index < 0 || index >= heegnerNumberCount_internal)return result;
int r = heegnerNumberList_internal[index];
result.absHeegner = r;
if(r % 4 == 3)
{
result.u = 0.5;
result.R = sqrt((double)r) / 2.0;
}
else
{
result.u = 0.0;
result.R = sqrt((double)r);
}
return result;
}
HeegnerFloat HeegnerFloat_Add(HeegnerFloat num0,HeegnerFloat num1)
{
HeegnerFloat result;result.a = num0.a + num1.a;result.b = num0.b + num1.b;
return result;
}
HeegnerFloat HeegnerFloat_Sub(HeegnerFloat num0,HeegnerFloat num1)
{
HeegnerFloat result;result.a = num0.a - num1.a;result.b = num0.b - num1.b;
return result;
}
HeegnerFloat HeegnerFloat_Mul(HeegnerFloat num0,HeegnerFloat num1,HeegnerConstant hc)
{
HeegnerFloat result;double c;
if(hc.absHeegner % 4 == 3){c = (hc.absHeegner + 1) / 4.0;}
else{c = (double)hc.absHeegner;}
result.a = num0.a * num1.a- c * num0.b * num1.b;
result.b = num0.a * num1.b+ num0.b * num1.a+ 2.0 * hc.u * num0.b * num1.b;
return result;
}
bool HeegnerFloat_Div(HeegnerFloat num0,HeegnerFloat num1,HeegnerConstant hc,HeegnerFloat *result)
{
if(result == NULL || hc.R == 0.0)return false;
Complex a = HeegnerFloat_To_Complex(num0, hc);Complex b = HeegnerFloat_To_Complex(num1, hc);
if(Complex_Abs(b) == 0.0)return false;
Complex answer = Complex_Div(a, b);
*result = Complex_To_HeegnerFloat(answer, hc);
return true;
}
double HeegnerFloat_Norm(HeegnerFloat num0, HeegnerConstant hc)
{
double c = hc.u * hc.u + hc.R * hc.R;
return num0.a * num0.a+ 2.0 * hc.u * num0.a * num0.b + c * num0.b * num0.b;
}
bool HeegnerFloat_Power(HeegnerFloat base,double exponent,HeegnerConstant hc,HeegnerFloat *result)
{
if(result == NULL || hc.R == 0.0)return false;
Complex z = HeegnerFloat_To_Complex(base, hc);
if(Complex_Abs(z) == 0.0)
{
if(exponent > 0.0){result->a = 0.0;result->b = 0.0;return true;}
return false;
}
Complex answer = Complex_Power(z, exponent);
*result = Complex_To_HeegnerFloat(answer, hc);
return true;
}
bool HeegnerFloat_FindRealLog(HeegnerFloat base,HeegnerFloat target,HeegnerConstant hc,double *exponent,double tolerance)
{
if(exponent == NULL || hc.R == 0.0)return false;
Complex z = HeegnerFloat_To_Complex(base, hc);Complex t = HeegnerFloat_To_Complex(target, hc);
double r = Complex_Abs(z);
double s = Complex_Abs(t);
if(r == 0.0 || s == 0.0)return false;
if(fabs(r - 1.0) < tolerance)return false;
double n = log(s) / log(r);
Complex check = Complex_Power(z, n);
double error = hypot(check.x - t.x,check.y - t.y);
if(error > tolerance * fmax(1.0, s))return false;
*exponent = n;
return true;
}
bool HeegnerFloat_LogGeneral(HeegnerFloat value,HeegnerFloat base,HeegnerConstant hc,HeegnerFloat *result)
{
if(result == NULL || hc.R == 0.0)return false;
Complex z = HeegnerFloat_To_Complex(value, hc);
Complex b = HeegnerFloat_To_Complex(base, hc);
if(Complex_Abs(z) == 0.0 ||Complex_Abs(b) == 0.0)return false;
Complex logValue = Complex_Log(z);
Complex logBase = Complex_Log(b);
if(Complex_Abs(logBase) < 1e-14)return false;
Complex answer = Complex_Div(logValue, logBase);
if(!isfinite(answer.x) || !isfinite(answer.y))return false;
*result = Complex_To_HeegnerFloat(answer, hc);
return true;
}
bool HeegnerFloat_PowerGeneral(HeegnerFloat base,HeegnerFloat exponent,HeegnerConstant hc,HeegnerFloat *result)
{
if(result == NULL || hc.R == 0.0)return false;
Complex z = HeegnerFloat_To_Complex(base, hc);
Complex e = HeegnerFloat_To_Complex(exponent, hc);
if(Complex_Abs(z) == 0.0)return false;
Complex logarithm = Complex_Log(z);
Complex product = Complex_Mul(e, logarithm);
Complex answer = Complex_Exp(product);
if(!isfinite(answer.x) || !isfinite(answer.y))return false;
*result = Complex_To_HeegnerFloat(answer, hc);
return true;
}
HeegnerFloat HeegnerFloat_NearestInteger(Complex input, HeegnerConstant hc)
{
HeegnerFloat bestEstimate = Complex_To_HeegnerFloat(input, hc);
int a0 = (int)round(bestEstimate.a);
int b0 = (int)round(bestEstimate.b);
double bestNorm = INFINITY;
//Find close lattice points
for(int a = a0 - 1; a <= a0 + 1; a++)
{
for(int b = b0 - 1; b <= b0 + 1; b++)
{
HeegnerFloat candidate = {(double)a, (double)b};
Complex complexCandidate = HeegnerFloat_To_Complex(candidate, hc);
Complex remainder = Complex_Sub(input, complexCandidate);
double errorNorm = Complex_Abs(remainder);
errorNorm *= errorNorm;
if(errorNorm < bestNorm)
{
bestNorm = errorNorm;
bestEstimate.a = candidate.a;
bestEstimate.b = candidate.b;
}
}
}
return bestEstimate;
}
size_t HeegnerFloat_ContinuedFraction(size_t maxTerms, HeegnerFloat coefficients[maxTerms], Complex z,HeegnerConstant hc)
{
Complex current = z;
Complex one = {1.0, 0.0};
size_t count = 0;
for(size_t i = 0; i < maxTerms; i++)
{
coefficients[i] = HeegnerFloat_NearestInteger(current,hc);
count += 1;
Complex complexCandidate = HeegnerFloat_To_Complex(coefficients[i], hc);
Complex remainder = Complex_Sub(current, complexCandidate);
double errorNorm = Complex_Abs(remainder);
errorNorm *= errorNorm;
if(errorNorm < 1e-20)break;
current = Complex_Div(one, remainder);
}
return count;
}
size_t HeegnerFloat_Convergents(size_t count,HeegnerFloat coefficients[count],HeegnerFloat numerators[count],HeegnerFloat denominators[count],HeegnerConstant hc)
{
if(count == 0) return 0;
HeegnerFloat pPrevious = {1.0, 0.0};
HeegnerFloat pCurrent = coefficients[0];
HeegnerFloat qPrevious = {0.0, 0.0};
HeegnerFloat qCurrent = {1.0, 0.0};
numerators[0] = pCurrent;
denominators[0] = qCurrent;
for(size_t i = 1; i < count; i++)
{
HeegnerFloat pNext = HeegnerFloat_Add(HeegnerFloat_Mul(coefficients[i], pCurrent, hc),pPrevious);
HeegnerFloat qNext = HeegnerFloat_Add(HeegnerFloat_Mul(coefficients[i], qCurrent, hc),qPrevious);
pPrevious = pCurrent;
pCurrent = pNext;
qPrevious = qCurrent;
qCurrent = qNext;
numerators[i] = pCurrent;
denominators[i] = qCurrent;
}
return count;
}
int TestLog()
{
srand(4516);
int index = 2;
HeegnerConstant hc = HeegnerConstant_Get(index);
HeegnerFloat original = {2.35437451, 1.78687861};
double n = 3;
HeegnerFloat powered;HeegnerFloat logarithm;HeegnerFloat generalResult;
double recoveredExponent;
printf("\nHeegner number: %d\n", hc.absHeegner);
printf("Omega = %.10f + %.10fi\n", hc.u, hc.R);
printf("Original pair:<%.10f, %.10f>\n", original.a, original.b);
printf("\nOriginal exponent:%.10f\n",n);
assert(HeegnerFloat_Power(original, n, hc, &powered));
printf("Result = <%.10f, %.10f>\n",powered.a, powered.b);
assert(HeegnerFloat_FindRealLog(original,powered,hc,&recoveredExponent,1e-8));
printf("Original exponent = %.10f\n", n);
printf("Recovered exponent = %.10f\n\n",recoveredExponent);
return 0;
}
void TestContinuedFraction()
{
int index = 2;
size_t maxTerms = 30;
HeegnerConstant hc = HeegnerConstant_Get(index);
HeegnerFloat original = {2.35437451, 1.78687861};
Complex z = HeegnerFloat_To_Complex(original, hc);
HeegnerFloat coefficients[maxTerms];
HeegnerFloat numerators[maxTerms];
HeegnerFloat denominators[maxTerms];
size_t coefficientCount = HeegnerFloat_ContinuedFraction(maxTerms, coefficients, z, hc);
HeegnerFloat_Convergents(coefficientCount, coefficients, numerators, denominators, hc);
printf("\nHeegner number: %d\n", hc.absHeegner);
printf("Omega = %.10f + %.10fi\n", hc.u, hc.R);
printf("Original Heegner:<%.10f, %.10f>\n", original.a, original.b);
printf("Original Complex:<%.10f, %.10f>\n", z.x, z.y);
printf("\nCoefficient count: %ld\n", coefficientCount);
for(size_t i = 0; i < coefficientCount; i++)
{
HeegnerFloat convergent;
HeegnerFloat_Div(numerators[i], denominators[i], hc, &convergent);
Complex approximation = HeegnerFloat_To_Complex(convergent, hc);
Complex error = Complex_Sub(z, approximation);
double errorAbs = Complex_Abs(error);
double errorNorm = errorAbs * errorAbs;
printf("%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);
}
}
int main()
{
TestContinuedFraction();
return 0;
}
1
OpenStax Authors. (2026). 2.4 Complex Numbers. In College Algebra with Corequisite Support 2e. Link.
4
Roberts, D. (2026). Arithmetic of Complex Numbers (Add, Subtract, Multiply). In MathBitsNotebook Algebra 2. Link.
5
Dummit, D. S. (2021). Number Theory 2: Lecture #21 of 38: Arithmetic in Domains + Quadratic Integer Rings. Northeastern University lecture notes. PDF Link.
6
Morris, J. (2026). The Generalized Binomial Theorem. In Combinatorics (Morris), Libretexts Mathematics. University of Lethbridge. Link.