This is a reworked fun experiment I had about how to store and load data
in complex numbers:
https://groups.google.com/g/comp.lang.c++/c/bB1wA4wvoFc/m/OTccTiXLAgAJ
// updated code
Can you run it and tell me what you get? Thanks!
:^)
_____________________________________
// Chris M. Thomasson
// complex storage for fun...
#include <complex>
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cstring>
typedef std::int64_t ct_int;
typedef std::uint64_t ct_uint;
typedef double ct_float;
typedef std::numeric_limits<ct_float> ct_float_nlim;
typedef std::complex<ct_float> ct_complex;
typedef std::vector<ct_complex> ct_complex_vec;
#define CT_PI 3.14159265358979323846
ct_float
ct_roots(
-a-a-a ct_complex const& z,
-a-a-a ct_int p,
-a-a-a ct_complex_vec& out
) {
-a-a-a assert(p != 0);
-a-a-a ct_float radius = std::pow(std::abs(z), 1.0 / p);
-a-a-a ct_float angle_base = std::arg(z) / p;
-a-a-a ct_float angle_step = (CT_PI * 2.0) / p;
-a-a-a ct_uint n = std::abs(p);
-a-a-a ct_float avg_err = 0.0;
-a-a-a for (ct_uint i = 0; i < n; ++i) {
-a-a-a-a-a-a-a ct_float angle = angle_step * i;
-a-a-a-a-a-a-a ct_complex c = {
-a-a-a-a-a-a-a-a-a-a-a std::cos(angle_base + angle) * radius,
-a-a-a-a-a-a-a-a-a-a-a std::sin(angle_base + angle) * radius
-a-a-a-a-a-a-a };
-a-a-a-a-a-a-a out.push_back(c);
-a-a-a-a-a-a-a ct_complex raised = std::pow(c, p);
-a-a-a-a-a-a-a avg_err = avg_err + std::abs(raised - z);
-a-a-a }
-a-a-a return avg_err / n;
}
// Direct angular calculation - O(1) instead of O(n)
ct_int
ct_try_find_direct(
-a-a-a ct_complex const& z,
-a-a-a ct_complex const& z_next,
-a-a-a ct_int power,
-a-a-a ct_float eps
) {
-a-a-a // Calculate what the angle_base was when z_next's roots were computed
-a-a-a ct_float angle_base = std::arg(z_next) / power;
-a-a-a // Get z's angle relative to origin
-a-a-a ct_float z_angle = std::arg(z);
-a-a-a // Find which root slot z falls into
-a-a-a // Subtract the base angle and normalize
-a-a-a ct_float relative_angle = z_angle - angle_base;
-a-a-a // Normalize to [0, 2*pi)
-a-a-a while (relative_angle < 0) relative_angle += CT_PI * 2.0;
-a-a-a while (relative_angle >= CT_PI * 2.0) relative_angle -= CT_PI * 2.0;
-a-a-a // Calculate step size between roots
-a-a-a ct_float angle_step = (CT_PI * 2.0) / power;
-a-a-a // Find nearest root index
-a-a-a ct_uint index = (ct_uint)std::round(relative_angle / angle_step);
-a-a-a // Handle wrap-around
-a-a-a if (index >= (ct_uint)std::abs(power)) {
-a-a-a-a-a-a-a index = 0;
-a-a-a }
-a-a-a return index;
}
// Original linear search version - more robust but O(n)
ct_int
ct_try_find(
-a-a-a ct_complex const& z,
-a-a-a ct_complex_vec const& roots,
-a-a-a ct_float eps
) {
-a-a-a std::size_t n = roots.size();
-a-a-a for (std::size_t i = 0; i < n; ++i) {
-a-a-a-a-a-a-a ct_complex const& root = roots[i];
-a-a-a-a-a-a-a ct_float adif = std::abs(root - z);
-a-a-a-a-a-a-a if (adif < eps) {
-a-a-a-a-a-a-a-a-a-a-a return i;
-a-a-a-a-a-a-a }
-a-a-a }
-a-a-a return -1;
}
static std::string const g_tokens_str =
-a-a-a "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ct_int
ct_gain_power(
-a-a-a std::string const& tokens
) {
-a-a-a ct_uint n = tokens.length();
-a-a-a std::size_t pmax = 0;
-a-a-a for (ct_uint i = 0; i < n; ++i) {
-a-a-a-a-a-a-a std::size_t fridx = g_tokens_str.find_first_of(tokens[i]);
-a-a-a-a-a-a-a assert(fridx != std::string::npos);
-a-a-a-a-a-a-a pmax = std::max(pmax, fridx);
-a-a-a }
-a-a-a return (ct_int)(pmax + 1);
}
ct_complex
ct_store(
-a-a-a ct_complex const& z_origin,
-a-a-a ct_int p,
-a-a-a std::string const& tokens
) {
-a-a-a ct_uint n = tokens.length();
-a-a-a ct_complex z = z_origin;
-a-a-a ct_float store_avg_err = 0.0;
-a-a-a std::cout << "Storing Data..." << "\n";
-a-a-a std::cout << "stored:z_origin:" << z_origin << "\n";
-a-a-a for (ct_uint i = 0; i < n; ++i) {
-a-a-a-a-a-a-a ct_complex_vec roots;
-a-a-a-a-a-a-a ct_float avg_err = ct_roots(z, p, roots);
-a-a-a-a-a-a-a store_avg_err = store_avg_err + avg_err;
-a-a-a-a-a-a-a std::size_t fridx = g_tokens_str.find_first_of(tokens[i]);
-a-a-a-a-a-a-a assert(fridx != std::string::npos);
-a-a-a-a-a-a-a z = roots[fridx];
-a-a-a-a-a-a-a std::cout << "stored[" << i << "]:" << z << "\n";
-a-a-a }
-a-a-a store_avg_err = store_avg_err / n;
-a-a-a std::cout << "store_avg_err:" << store_avg_err << "\n";
-a-a-a return z;
}
ct_float
ct_load(
-a-a-a ct_complex const& z_store,
-a-a-a ct_complex const& z_target,
-a-a-a ct_int p,
-a-a-a ct_float eps,
-a-a-a std::string& out_tokens,
-a-a-a ct_complex& out_z,
-a-a-a bool use_direct = false-a // Toggle between direct and linear search ) {
-a-a-a ct_complex z = z_store;
-a-a-a ct_uint n = 128;
-a-a-a ct_float load_err_sum = 0.0;
-a-a-a std::cout << "Loading Data... (using " << (use_direct ? "direct" : "linear search") << " method)\n";
-a-a-a for (ct_uint i = 0; i < n; ++i) {
-a-a-a-a-a-a-a // Raise to power to get parent point
-a-a-a-a-a-a-a ct_complex z_next = std::pow(z, p);
-a-a-a-a-a-a-a ct_int root_idx;
-a-a-a-a-a-a-a if (use_direct) {
-a-a-a-a-a-a-a-a-a-a-a // Direct O(1) calculation
-a-a-a-a-a-a-a-a-a-a-a root_idx = ct_try_find_direct(z, z_next, p, eps);
-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a else {
-a-a-a-a-a-a-a-a-a-a-a // Linear search O(n) - compute roots and search
-a-a-a-a-a-a-a-a-a-a-a ct_complex_vec roots;
-a-a-a-a-a-a-a-a-a-a-a ct_float avg_err = ct_roots(z_next, p, roots);
-a-a-a-a-a-a-a-a-a-a-a load_err_sum += avg_err;
-a-a-a-a-a-a-a-a-a-a-a root_idx = ct_try_find(z, roots, eps);
-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a if (root_idx < 0 || (ct_uint)root_idx >= g_tokens_str.length()) {
-a-a-a-a-a-a-a-a-a-a-a break;
-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a std::cout << "loaded[" << i << "]:" << z << " (index:" << root_idx << ")\n";
-a-a-a-a-a-a-a out_tokens += g_tokens_str[root_idx];
-a-a-a-a-a-a-a // Move to parent point
-a-a-a-a-a-a-a z = z_next;
-a-a-a-a-a-a-a // Check if we've reached the origin
-a-a-a-a-a-a-a if (std::abs(z - z_target) < eps) {
-a-a-a-a-a-a-a-a-a-a-a std::cout << "fin detected!:[" << i << "]:" << z << "\n";
-a-a-a-a-a-a-a-a-a-a-a break;
-a-a-a-a-a-a-a }
-a-a-a }
-a-a-a // Reverse to get original order
-a-a-a std::reverse(out_tokens.begin(), out_tokens.end());
-a-a-a out_z = z;
-a-a-a return load_err_sum;
}
int main() {
-a-a-a std::cout.precision(ct_float_nlim::max_digits10);
-a-a-a std::cout << "g_tokens_str:" << g_tokens_str << "\n\n";
-a-a-a {
-a-a-a-a-a-a-a ct_complex z_origin = { -.75, .06 };
-a-a-a-a-a-a-a std::string stored = "CHRIS";
-a-a-a-a-a-a-a ct_int power = ct_gain_power(stored);
-a-a-a-a-a-a-a std::cout << "stored:" << stored << "\n";
-a-a-a-a-a-a-a std::cout << "power:" << power << "\n\n";
-a-a-a-a-a-a-a std::cout << "________________________________________\n";
-a-a-a-a-a-a-a // STORE
-a-a-a-a-a-a-a ct_complex z_stored = ct_store(z_origin, power, stored);
-a-a-a-a-a-a-a std::cout << "________________________________________\n";
-a-a-a-a-a-a-a std::cout << "\nSTORED POINT:" << z_stored << "\n";
-a-a-a-a-a-a-a std::cout << "________________________________________\n";
-a-a-a-a-a-a-a // LOAD - try both methods
-a-a-a-a-a-a-a std::string loaded;
-a-a-a-a-a-a-a ct_complex z_loaded;
-a-a-a-a-a-a-a ct_float eps = .001;
-a-a-a-a-a-a-a std::cout << "\n=== Testing LINEAR SEARCH method ===\n";
-a-a-a-a-a-a-a ct_float load_err_sum =
-a-a-a-a-a-a-a-a-a-a-a ct_load(z_stored, z_origin, power, eps, loaded, z_loaded,
false);
-a-a-a-a-a-a-a std::cout << "________________________________________\n";
-a-a-a-a-a-a-a std::cout << "\nORIGIN POINT:" << z_origin << "\n";
-a-a-a-a-a-a-a std::cout << "LOADED POINT:" << z_loaded << "\n";
-a-a-a-a-a-a-a std::cout << "\nloaded:" << loaded << "\n";
-a-a-a-a-a-a-a std::cout << "load_err_sum:" << load_err_sum << "\n";
-a-a-a-a-a-a-a if (stored == loaded) {
-a-a-a-a-a-a-a-a-a-a-a std::cout << "\n\nDATA COHERENT! :^D" << "\n";
-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a else {
-a-a-a-a-a-a-a-a-a-a-a std::cout << "\n\n***** DATA CORRUPTED!!! Shi%! *****" <<
"\n";
-a-a-a-a-a-a-a-a-a-a-a std::cout << "Expected: " << stored << "\n";
-a-a-a-a-a-a-a-a-a-a-a std::cout << "Got:-a-a-a-a-a " << loaded << "\n";
-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a // Try direct method
-a-a-a-a-a-a-a std::cout << "\n\n=== Testing DIRECT ANGULAR method ===\n";
-a-a-a-a-a-a-a std::string loaded_direct;
-a-a-a-a-a-a-a ct_complex z_loaded_direct;
-a-a-a-a-a-a-a ct_float load_err_sum_direct =
-a-a-a-a-a-a-a-a-a-a-a ct_load(z_stored, z_origin, power, eps, loaded_direct,
z_loaded_direct, true);
-a-a-a-a-a-a-a std::cout << "________________________________________\n";
-a-a-a-a-a-a-a std::cout << "\nloaded:" << loaded_direct << "\n";
-a-a-a-a-a-a-a if (stored == loaded_direct) {
-a-a-a-a-a-a-a-a-a-a-a std::cout << "\n\nDATA COHERENT (DIRECT METHOD)! :^D" << "\n";
-a-a-a-a-a-a-a }
-a-a-a-a-a-a-a else {
-a-a-a-a-a-a-a-a-a-a-a std::cout << "\n\n***** DATA CORRUPTED (DIRECT METHOD)!!!
*****" << "\n";
-a-a-a-a-a-a-a-a-a-a-a std::cout << "Expected: " << stored << "\n";
-a-a-a-a-a-a-a-a-a-a-a std::cout << "Got:-a-a-a-a-a " << loaded_direct << "\n";
-a-a-a-a-a-a-a }
-a-a-a }
-a-a-a std::cout << "\n\nFin, hit <ENTER> to exit...\n";
-a-a-a std::fflush(stdout);
-a-a-a std::cin.get();
-a-a-a return 0;
}
_____________________________________
Am 11.01.2026 um 23:03 schrieb Chris M. Thomasson:
This is a reworked fun experiment I had about how to store and load
data in complex numbers:
https://groups.google.com/g/comp.lang.c++/c/bB1wA4wvoFc/m/OTccTiXLAgAJ
// updated code
Can you run it and tell me what you get? Thanks!
:^)
_____________________________________
// Chris M. Thomasson
// complex storage for fun...
#include <complex>
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cstring>
typedef std::int64_t ct_int;
typedef std::uint64_t ct_uint;
typedef double ct_float;
typedef std::numeric_limits<ct_float> ct_float_nlim;
typedef std::complex<ct_float> ct_complex;
typedef std::vector<ct_complex> ct_complex_vec;
I would recomment to use a ct_cout also.
That would increase the readability.
On 1/14/2026 4:33 AM, Bonita Montero wrote:
Am 11.01.2026 um 23:03 schrieb Chris M. Thomasson:
This is a reworked fun experiment I had about how to store and load
data in complex numbers:
https://groups.google.com/g/comp.lang.c++/c/bB1wA4wvoFc/m/OTccTiXLAgAJ
// updated code
Can you run it and tell me what you get? Thanks!
:^)
_____________________________________
// Chris M. Thomasson
// complex storage for fun...
#include <complex>
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cstring>
typedef std::int64_t ct_int;
typedef std::uint64_t ct_uint;
typedef double ct_float;
typedef std::numeric_limits<ct_float> ct_float_nlim;
typedef std::complex<ct_float> ct_complex;
typedef std::vector<ct_complex> ct_complex_vec;
I would recomment to use a ct_cout also.
That would increase the readability.
;^)
Iirc, back when I created it I used typedef's to try to get my own set
of types so that I can test double vs float, or even define them to something else. For instance, perhaps use my own complex with arbitrary precision for its real and imag components.
[...]
Am 14.01.2026 um 20:33 schrieb Chris M. Thomasson:
On 1/14/2026 4:33 AM, Bonita Montero wrote:
Am 11.01.2026 um 23:03 schrieb Chris M. Thomasson:
This is a reworked fun experiment I had about how to store and load
data in complex numbers:
https://groups.google.com/g/comp.lang.c++/c/bB1wA4wvoFc/m/OTccTiXLAgAJ >>>>
// updated code
Can you run it and tell me what you get? Thanks!
:^)
_____________________________________
// Chris M. Thomasson
// complex storage for fun...
#include <complex>
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cstring>
typedef std::int64_t ct_int;
typedef std::uint64_t ct_uint;
typedef double ct_float;
typedef std::numeric_limits<ct_float> ct_float_nlim;
typedef std::complex<ct_float> ct_complex;
typedef std::vector<ct_complex> ct_complex_vec;
I would recomment to use a ct_cout also.
That would increase the readability.
;^)
Iirc, back when I created it I used typedef's to try to get my own set
of types so that I can test double vs float, or even define them to
something else. For instance, perhaps use my own complex with
arbitrary precision for its real and imag components.
[...]
ct_xxx is counter-intuitive.
On 1/14/2026 6:33 PM, Bonita Montero wrote:
Am 14.01.2026 um 20:33 schrieb Chris M. Thomasson:
On 1/14/2026 4:33 AM, Bonita Montero wrote:
Am 11.01.2026 um 23:03 schrieb Chris M. Thomasson:
This is a reworked fun experiment I had about how to store and load >>>>> data in complex numbers:
https://groups.google.com/g/comp.lang.c++/c/bB1wA4wvoFc/m/OTccTiXLAgAJ >>>>>
// updated code
Can you run it and tell me what you get? Thanks!
:^)
_____________________________________
// Chris M. Thomasson
// complex storage for fun...
#include <complex>
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cstring>
typedef std::int64_t ct_int;
typedef std::uint64_t ct_uint;
typedef double ct_float;
typedef std::numeric_limits<ct_float> ct_float_nlim;
typedef std::complex<ct_float> ct_complex;
typedef std::vector<ct_complex> ct_complex_vec;
I would recomment to use a ct_cout also.
That would increase the readability.
;^)
Iirc, back when I created it I used typedef's to try to get my own
set of types so that I can test double vs float, or even define them
to something else. For instance, perhaps use my own complex with
arbitrary precision for its real and imag components.
[...]
ct_xxx is counter-intuitive.
I agree.
Well, ct_* as a namespace is fine. But I see what you mean.
On 1/14/2026 6:33 PM, Bonita Montero wrote:
ct_xxx is counter-intuitive.
I agree.
On 16/01/2026 04:45, Chris M. Thomasson wrote:
On 1/14/2026 6:33 PM, Bonita Montero wrote:
ct_xxx is counter-intuitive.
I agree.
I disagree.
The ct_ prefix is not intuitive, based on the code (clearly it comes
from your name).-a But it is not /counter-intuitive/, which would mean it goes directly against intuition.
"cs_int" could be called intuitive for a prefix used in code for
"complex storage".
"complex_int" would be counter-intuitive, because it would look like a
type for Gaussian integers rather than a local name for an integer type.
"ct_int" is neither intuitive nor counter-intuitive.-a It's just not particularly great, and looks like C-style code rather than C++ code
with namespaces, using directives, and templates rather than typedefs
like this.-a But it's perfectly good for trying things out while
developing - you can always polish it later if it is to be a general
library for wider use.
On 16/01/2026 04:45, Chris M. Thomasson wrote:
On 1/14/2026 6:33 PM, Bonita Montero wrote:
ct_xxx is counter-intuitive.
I agree.
I disagree.
The ct_ prefix is not intuitive, based on the code (clearly it comes
from your name).-a But it is not /counter-intuitive/, which would mean it goes directly against intuition.
"cs_int" could be called intuitive for a prefix used in code for
"complex storage".
"complex_int" would be counter-intuitive, because it would look like a
type for Gaussian integers rather than a local name for an integer type.
"ct_int" is neither intuitive nor counter-intuitive.-a It's just not particularly great, and looks like C-style code rather than C++ code
with namespaces, using directives, and templates rather than typedefs
like this.-a But it's perfectly good for trying things out while
developing - you can always polish it later if it is to be a general
library for wider use.
Am 16.01.2026 um 04:46 schrieb Chris M. Thomasson:
Well, ct_* as a namespace is fine. But I see what you mean.
The best thing to do is request the namespace ct from the
-astandardization committee. Alternatively, I can recommend
namespace 7777078A_AAAC_4761_9B34-5BC0F1059353, as this
GUID is particularly easy to remember.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 59 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 23:00:02 |
| Calls: | 810 |
| Calls today: | 1 |
| Files: | 1,287 |
| D/L today: |
12 files (21,036K bytes) |
| Messages: | 195,759 |