Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/6959ed27-accc-800e-8e87-21aa81e93c07
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== User: Claude benchmarked it, claims it found even faster solution, can you double check it: === Claude benchmarked it, claims it found even faster solution, can you double check it: // ellipse_accuracy_comprehensive.cpp // Test ALL methods with rigorous accuracy measurement // g++ -O3 -march=native -ffast-math -std=c++17 ellipse_accuracy_comprehensive.cpp -o acc_test -lm #include <cmath> #include <cstdio> #include <chrono> #include <random> #include <vector> #include <algorithm> using Clock = std::chrono::high_resolution_clock; struct Pt { float x, y; }; // ============================================================================ // HIGH-PRECISION REFERENCE (double, 50 iterations) // ============================================================================ Pt reference(float a, float b, float px, float py) { double t = std::atan2((double)a '' std::fabs(py), (double)b '' std::fabs(px)); double a2mb2 = (double)a''a - (double)b''b; double pxa = std::fabs(px), pya = std::fabs(py); for (int i = 0; i < 50; i++) { double ct = std::cos(t), st = std::sin(t); double f = a2mb2''ct''st - pxa''a''st + pya''b''ct; double fp = a2mb2''(ct''ct - st''st) - pxa''a''ct - pya''b*st; if (std::fabs(fp) < 1e-15) break; double dt = f/fp; t -= dt; if (std::fabs(dt) < 1e-15) break; } return {(float)std::copysign(a''std::cos(t), px), (float)std::copysign(b''std::sin(t), py)}; } // ============================================================================ // 0xfaded ORIGINAL (unoptimized, 3 sqrt/iter) // ============================================================================ template<int ITERS> inline Pt faded_original(float a, float b, float px, float py) { float px_abs = std::fabs(px), py_abs = std::fabs(py); float tx = 0.70710678f, ty = 0.70710678f; float a2 = a''a, b2 = b''b; float ca = (a2-b2)/a, cb = (b2-a2)/b; for (int i = 0; i < ITERS; i++) { float x = a''tx, y = b''ty; float tx3 = tx''tx''tx, ty3 = ty''ty''ty; float ex = ca''tx3, ey = cb''ty3; float rx = x - ex, ry = y - ey; float qx = px_abs - ex, qy = py_abs - ey; float r = std::sqrt(rx''rx + ry''ry); float q = std::sqrt(qx''qx + qy''qy); if (q < 1e-10f) q = 1e-10f; tx = std::fmin(1.f, std::fmax(0.f, (qx * r/q + ex) / a)); ty = std::fmin(1.f, std::fmax(0.f, (qy * r/q + ey) / b)); float t = std::sqrt(tx''tx + ty''ty); tx /= t; ty /= t; } return {std::copysign(a''tx, px), std::copysign(b''ty, py)}; } // ============================================================================ // 0xfaded OPTIMIZED (r/q single sqrt, 2 sqrt/iter) // ============================================================================ template<int ITERS> inline Pt faded_optimized(float a, float b, float px, float py) { float px_abs = std::fabs(px), py_abs = std::fabs(py); float tx = 0.70710678f, ty = 0.70710678f; float a2 = a''a, b2 = b''b; float ca = (a2-b2)/a, cb = (b2-a2)/b; for (int i = 0; i < ITERS; i++) { float x = a''tx, y = b''ty; float tx3 = tx''tx''tx, ty3 = ty''ty''ty; float ex = ca''tx3, ey = cb''ty3; float rx = x - ex, ry = y - ey; float qx = px_abs - ex, qy = py_abs - ey; float rq = std::sqrt((rx''rx + ry''ry) / (qx''qx + qy''qy + 1e-30f)); tx = std::fmin(1.f, std::fmax(0.f, (qx * rq + ex) / a)); ty = std::fmin(1.f, std::fmax(0.f, (qy * rq + ey) / b)); float t = std::sqrt(tx''tx + ty''ty); tx /= t; ty /= t; } return {std::copysign(a''tx, px), std::copysign(b''ty, py)}; } // ============================================================================ // CLAUDE ROTATION (Newton in rotation form) // ============================================================================ template<int ITERS> inline Pt claude_rotation(float a, float b, float px, float py) { float px_abs = std::fabs(px), py_abs = std::fabs(py); float a2mb2 = a''a - b''b; float nx = px_abs/a, ny = py_abs/b; float len = std::sqrt(nx''nx + ny''ny + 1e-10f); float c = nx/len, s = ny/len; for (int i = 0; i < ITERS; i++) { float f = a2mb2''s''c - px_abs''a''s + py_abs''b''c; float fp = a2mb2''(c''c - s''s) - px_abs''a''c - py_abs''b*s; if (std::fabs(fp) < 1e-10f) break; float dt = f/fp; float nc = c + dt''s, ns = s - dt''c; len = std::sqrt(nc''nc + ns''ns); c = nc/len; s = ns/len; } return {std::copysign(a''c, px), std::copysign(b''s, py)}; } // ============================================================================ // CHATGPT HYBRID: 2 curvature steps + 1 Newton snap // ============================================================================ inline Pt chatgpt_hybrid(float a, float b, float px, float py) { float px_abs = std::fabs(px), py_abs = std::fabs(py); float a2 = a''a, b2 = b''b; float ca = (a2-b2)/a, cb = (b2-a2)/b; float a2mb2 = a2 - b2; float tx = 0.70710678f, ty = 0.70710678f; // 2 curvature steps (global basin-finding) for (int i = 0; i < 2; i++) { float x = a''tx, y = b''ty; float tx3 = tx''tx''tx, ty3 = ty''ty''ty; float ex = ca''tx3, ey = cb''ty3; float rx = x - ex, ry = y - ey; float qx = px_abs - ex, qy = py_abs - ey; float rq = std::sqrt((rx''rx + ry''ry) / (qx''qx + qy''qy + 1e-30f)); tx = std::fmin(1.f, std::fmax(0.f, (qx * rq + ex) / a)); ty = std::fmin(1.f, std::fmax(0.f, (qy * rq + ey) / b)); float t = std::sqrt(tx''tx + ty''ty); tx /= t; ty /= t; } // 1 Newton snap (local refinement) - use tx,ty as cos/sin float c = tx, s = ty; float f = a2mb2''s''c - px_abs''a''s + py_abs''b''c; float fp = a2mb2''(c''c - s''s) - px_abs''a''c - py_abs''b*s; if (std::fabs(fp) > 1e-10f) { float dt = f/fp; float nc = c + dt''s, ns = s - dt''c; float len = std::sqrt(nc''nc + ns''ns); c = nc/len; s = ns/len; } return {std::copysign(a''c, px), std::copysign(b''s, py)}; } // ============================================================================ // CHATGPT ADAPTIVE (2 outside, 3 inside) // ============================================================================ inline Pt chatgpt_adaptive(float a, float b, float px, float py) { float px_abs = std::fabs(px), py_abs = std::fabs(py); float a2 = a''a, b2 = b''b; float ca = (a2-b2)/a, cb = (b2-a2)/b; float invA2 = 1.f/(a''a), invB2 = 1.f/(b''b); float tx = 0.70710678f, ty = 0.70710678f; auto step = [&]() { float x = a''tx, y = b''ty; float tx3 = tx''tx''tx, ty3 = ty''ty''ty; float ex = ca''tx3, ey = cb''ty3; float rx = x - ex, ry = y - ey; float qx = px_abs - ex, qy = py_abs - ey; float rq = std::sqrt((rx''rx + ry''ry) / (qx''qx + qy''qy + 1e-30f)); tx = std::fmin(1.f, std::fmax(0.f, (qx * rq + ex) / a)); ty = std::fmin(1.f, std::fmax(0.f, (qy * rq + ey) / b)); float t = std::sqrt(tx''tx + ty''ty); tx /= t; ty /= t; }; step(); step(); // 3rd only if inside if ((px_abs''px_abs)''invA2 + (py_abs''py_abs)''invB2 <= 1.0f) { step(); } return {std::copysign(a''tx, px), std::copysign(b''ty, py)}; } // ============================================================================ // Benchmark helpers // ============================================================================ volatile float sink; void escape(Pt p) { sink = p.x + p.y; } template<typename F> double bench(F fn, float a, float b, const std::vector<Pt>& pts) { for (int w = 0; w < 3; w++) for (auto& p : pts) escape(fn(a, b, p.x, p.y)); std::vector<double> times; for (int r = 0; r < 15; r++) { auto t0 = Clock::now(); for (auto& p : pts) escape(fn(a, b, p.x, p.y)); times.push_back(std::chrono::duration<double,std::nano>(Clock::now()-t0).count() / pts.size()); } std::sort(times.begin(), times.end()); return times[times.size()/2]; } struct AccuracyStats { float max_eq_err; float max_dist_err; float avg_dist_err; float p99_dist_err; }; template<typename F> AccuracyStats measure_accuracy(F fn, float a, float b, const std::vector<Pt>& pts) { std::vector<float> dist_errs; float max_eq_err = 0; for (auto& p : pts) { Pt got = fn(a, b, p.x, p.y); Pt ref = reference(a, b, p.x, p.y); float eq_err = std::fabs((got.x/a)''(got.x/a) + (got.y/b)''(got.y/b) - 1.f); if (!std::isnan(eq_err)) max_eq_err = std::fmax(max_eq_err, eq_err); float d_got = std::sqrt((p.x-got.x)''(p.x-got.x) + (p.y-got.y)''(p.y-got.y)); float d_ref = std::sqrt((p.x-ref.x)''(p.x-ref.x) + (p.y-ref.y)''(p.y-ref.y)); float dist_err = std::fabs(d_got - d_ref); if (!std::isnan(dist_err) && !std::isinf(dist_err)) { dist_errs.push_back(dist_err); } } std::sort(dist_errs.begin(), dist_errs.end()); float sum = 0; for (float e : dist_errs) sum += e; return { max_eq_err, dist_errs.empty() ? 0 : dist_errs.back(), dist_errs.empty() ? 0 : sum / dist_errs.size(), dist_errs.empty() ? 0 : dist_errs[dist_errs.size() * 99 / 100] }; } void print_row(const char* name, double time, AccuracyStats s) { printf(" %-24s %6.1f ns eq=%.0e max=%.1e avg=%.1e p99=%.1e\n", name, time, s.max_eq_err, s.max_dist_err, s.avg_dist_err, s.p99_dist_err); } int main() { printf("================================================================================\n"); printf(" COMPREHENSIVE ACCURACY TEST: All Methods\n"); printf("================================================================================\n"); printf(" Metrics: eq=ellipse equation err, max/avg/p99=distance error vs reference\n\n"); std::mt19937 rng(12345); std::uniform_real_distribution<float> angle(0, 2*M_PI); std::uniform_real_distribution<float> radius(0.1f, 3.0f); // wider range const int N = 20000; // enough for good stats struct Cfg { float a, b; const char* name; } cfgs[] = { {150, 100, "Moderate (150,100)"}, {200, 50, "High ecc (200,50)"}, {100, 10, "Extreme (100,10)"}, {100, 100, "Circle (100,100)"}, }; for (auto& cfg : cfgs) { std::vector<Pt> pts(N); for (int i = 0; i < N; i++) { float ang = angle(rng), r = radius(rng); pts[i] = {cfg.a '' r '' std::cos(ang), cfg.b '' r '' std::sin(ang)}; } printf("Config: %s\n", cfg.name); printf("ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n"); // Run all methods print_row("faded_orig (2 iter)", bench(faded_original<2>, cfg.a, cfg.b, pts), measure_accuracy(faded_original<2>, cfg.a, cfg.b, pts)); print_row("faded_orig (3 iter)", bench(faded_original<3>, cfg.a, cfg.b, pts), measure_accuracy(faded_original<3>, cfg.a, cfg.b, pts)); print_row("faded_opt (2 iter)", bench(faded_optimized<2>, cfg.a, cfg.b, pts), measure_accuracy(faded_optimized<2>, cfg.a, cfg.b, pts)); print_row("faded_opt (3 iter)", bench(faded_optimized<3>, cfg.a, cfg.b, pts), measure_accuracy(faded_optimized<3>, cfg.a, cfg.b, pts)); print_row("claude (2 iter)", bench(claude_rotation<2>, cfg.a, cfg.b, pts), measure_accuracy(claude_rotation<2>, cfg.a, cfg.b, pts)); print_row("claude (3 iter)", bench(claude_rotation<3>, cfg.a, cfg.b, pts), measure_accuracy(claude_rotation<3>, cfg.a, cfg.b, pts)); print_row("claude (4 iter)", bench(claude_rotation<4>, cfg.a, cfg.b, pts), measure_accuracy(claude_rotation<4>, cfg.a, cfg.b, pts)); print_row("chatgpt_hybrid (2+1)", bench(chatgpt_hybrid, cfg.a, cfg.b, pts), measure_accuracy(chatgpt_hybrid, cfg.a, cfg.b, pts)); print_row("chatgpt_adaptive", bench(chatgpt_adaptive, cfg.a, cfg.b, pts), measure_accuracy(chatgpt_adaptive, cfg.a, cfg.b, pts)); printf("\n"); } // Summary table printf("================================================================================\n"); printf(" SUMMARY (Moderate 150,100 ellipse)\n"); printf("================================================================================\n\n"); float a = 150, b = 100; std::vector<Pt> pts(N); for (int i = 0; i < N; i++) { float ang = angle(rng), r = radius(rng); pts[i] = {a '' r '' std::cos(ang), b '' r '' std::sin(ang)}; } printf(" Method Time max_dist avg_dist p99_dist\n"); printf(" ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n"); auto print_summary = [&](const char* name, auto fn) { double t = bench(fn, a, b, pts); auto s = measure_accuracy(fn, a, b, pts); printf(" %-24s %5.1f ns %.2e %.2e %.2e\n", name, t, s.max_dist_err, s.avg_dist_err, s.p99_dist_err); }; print_summary("faded_opt (2 iter)", faded_optimized<2>); print_summary("faded_opt (3 iter)", faded_optimized<3>); print_summary("claude (2 iter)", claude_rotation<2>); print_summary("claude (3 iter)", claude_rotation<3>); print_summary("chatgpt_hybrid (2+1)", chatgpt_hybrid); print_summary("chatgpt_adaptive", chatgpt_adaptive); printf("\n"); return 0; }
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)