added linear regression square root approximation#1565
Open
AMNTDEVIL wants to merge 1 commit intoTheAlgorithms:masterfrom
Open
added linear regression square root approximation#1565AMNTDEVIL wants to merge 1 commit intoTheAlgorithms:masterfrom
AMNTDEVIL wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces a fast square root approximation using least squares linear regression,
optimized for the interval
[1.0, 2.0]. It is designed for performance-critical environmentssuch as game engines, signal processors, and embedded systems where calling the standard
sqrt()is too expensive.The core idea is to fit a linear equation
y = αx + βthat minimizes the squared erroragainst
√xover the target range, then use bit-level tricks to extend it to all positivefloats via IEEE 754 exponent manipulation.
How It Works
1. Coefficient Calculation
Uses the normal equations of linear regression over
N = 1000sampled points in[1.0, 2.0]to compute the optimal slope
αand interceptβ:2. IEEE 754 Bit-Level Representation
The coefficients are inspected at the bit level, which is useful for low-level systems
that need hardcoded hex constants instead of runtime computation:
3. Fixed-Point Conversion (16.16 format)
For hardware without an FPU, the coefficients are scaled by
2^16 = 65536and stored as integers:4. Range Extension via Bit Manipulation
To handle inputs outside
[1.0, 2.0]:xinto[1.0, 2.0)2^(exp/2)√2 ≈ 1.41421356Performance vs Accuracy
Use Cases
References
Checklist