zig icon indicating copy to clipboard operation
zig copied to clipboard

math.pow: add f16, f80, f128, c_longdouble, comptime_float support

Open vilhelmbergsoe opened this issue 8 months ago • 4 comments

Removes the compile error restricting math.pow to f32/f64, allowing its use with f16, f80, f128, c_longdouble and comptime_float.

~Adds a compile error when T is comptime_float. The current pow implementation relies on concrete float properties (e.g., bit layout via @typeInfo(T).float.bits) which aren't available for comptime_float as far as I can tell.~

~There may be other ways to handle this, but this approach at least adds support for the concrete floating point types :)~

comptime_float behavior tries to follow that described in: https://github.com/ziglang/zig/issues/21205#issuecomment-2613393221

Fixes: #23602

vilhelmbergsoe avatar Apr 20 '25 22:04 vilhelmbergsoe

Adds a compile error when T is comptime_float. The current pow implementation relies on concrete float properties (e.g., bit layout via @typeInfo(T).float.bits) which aren't available for comptime_float as far as I can tell.

I believe the current thinking is that comptime_float should mostly behave as f128, except for NaNs, or something like that. @mlugg can you confirm?

alexrp avatar Apr 20 '25 22:04 alexrp

It seems that comptime_float should behave like f128 but where NaN and inf result in compile errors. With that in mind, it seems like, when comptime_floats are passed in, you could just cast them to f128s, and if the result is NaN or inf, throw a @compileError(). Are there any problems with that approach that I'm missing?

Empika1 avatar Apr 26 '25 02:04 Empika1

Casting comptime_float inputs to f128 and then checking the result seems like the plan.

It seems that comptime_float should behave like f128 but where NaN and inf result in compile errors.

@Empika1 - Can you point to where you see this pattern? I wasn't able to find anything similar myself.

Still wondering if we should @compileError on both NaN and Inf results from the f128 cast, or only NaN?

If it's only NaN, perhaps the existing NaN handling within pow is sufficient, and we'd just need the conditional cast to f128?

@alexrp / @mlugg, could you clarify the intended behavior?

vilhelmbergsoe avatar Apr 27 '25 11:04 vilhelmbergsoe

https://github.com/ziglang/zig/issues/21205#issuecomment-2613393221

alexrp avatar May 02 '25 18:05 alexrp