Skip to content

Add Floor, Ceil, Round, Abs Float32 and Implement Pow(x, y) with ln64/exp64 helpers#53

Open
Zapaxe wants to merge 5 commits into
rux-lang:devfrom
Zapaxe:dev
Open

Add Floor, Ceil, Round, Abs Float32 and Implement Pow(x, y) with ln64/exp64 helpers#53
Zapaxe wants to merge 5 commits into
rux-lang:devfrom
Zapaxe:dev

Conversation

@Zapaxe

@Zapaxe Zapaxe commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

  • Replaces Floor, Ceil, and Round stubs with real implementations using truncate-then-correct via as int. Fixes Abs so float32 works, previously the stub returned 0.0f32 and was unusable.

Additions

Floor / Ceil / Round

  • Floor/Ceil/Round (float64) -> Cast to int (trunc toward zero), then adjust: Floor shifts negative non-integers down by 1, Ceil shifts positive non-integers up by 1, Round adds 1 when the fractional part is >= 0.5.

  • Floor32/Ceil32/Round32 (float32) -> Delegate to the float64 implementation via cast.

Abs (float32)

  • Abs(x: float32) -> Previously a stub returning 0.0f32. Now returns x < 0.0f32 ? -x : x.

Why?

  • Floor, Ceil, Round, and Abs for float32 are basic operations that should work out of the box. The float32 Abs stub was unusable, this PR gives it a real implementation, and Floor/Ceil/Round stubs are replaced with working code.

@Zapaxe Zapaxe changed the title Add Floor, Ceil, Round, Abs Float32 Add Floor, Ceil, Round, Abs Float32 and Implement Pow(x, y) with ln64/exp64 helpers Jun 15, 2026
@Zapaxe

Zapaxe commented Jun 15, 2026

Copy link
Copy Markdown
Member Author

Summary

  • Replaces the Pow stub with a real implementation via exp(y * ln(x)). Adds private ln64 and exp64 helpers using Taylor series with range reduction.

Additions

  • ln64 (float64) -> Range reduces to [1, 2) by counting divisions by 2, computes 2 * atanh(z) where z = (x-1)/(x+1) via Taylor series (18 terms). Handles subnormal inputs.

  • exp64 (float64) -> Range reduces via k = floor(x / ln2), computes exp(r) via Taylor series (18 terms), then scales by 2^k. Overflow/underflow guards for extreme inputs.

  • Pow (float64, float64) -> Handles edge cases (x^0 = 1, 0^y = 0, 1^y = 1, x^1 = x). For x < 0 with integer y, computes Pow(-x, y) and adjusts sign. General case: exp(y * ln(x)).

  • Pow32 (float32, float32) -> Delegates to the float64 implementation via cast.

Why?

  • Power/exponentiation is a fundamental math operation required for scientific computing. The existing stub returned 0.0, making Pow unusable. This implementation achieves sub-1e-10 relative accuracy across the normal float64 domain using carefully range-reduced Taylor series.

@Zapaxe

Zapaxe commented Jun 18, 2026

Copy link
Copy Markdown
Member Author

Summary

Abs64 branchless sign-bit clearing ->

  • Replaced the conditional if x >= 0.0 { return x; } return -x; with IEEE 754 bit manipulation using the new FloatBits64/FloatFromBits64 built-in intrinsics. Clears the sign bit directly (bits & 0x7FFFFFFFFFFFFFFF) branchless and constant-time.

sin64 / cos64 int64 overflow guard ->

  • Added a guard before (r / Tau()) as int64 to prevent undefined behavior when the quotient exceeds INT64_MAX (~9.22e18). Returns 0.0 for such extreme arguments.

Floor / Ceil / Round identity bypass at ±2^53 ->

  • Added if Abs64(x) >= 9007199254740992.0 { return x; } to short-circuit for float64 values at or beyond 2^53, where every value is an exact integer. This avoids the as int truncation cast which would lose precision for these values.

Sqrt normalized Newton–Raphson ->

  • Rewrote the square root implementation to first normalize x into [1, 4) by halving/doubling, then apply 8 Newton–Raphson iterations. This ensures correct results across the full float64 exponent range, including subnormals and very large values.

@Natuworkguy Natuworkguy requested a review from pascalecu June 20, 2026 19:23
@Natuworkguy Natuworkguy added enhancement New feature or request help wanted Extra attention is needed labels Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants