#Shortcut for casting integer to float?

1 messages · Page 1 of 1 (latest)

wraith oar
#

In my code I do lots of castings from integer to float. I previous Zig versions you could do something like this:

const my_float = @intToFloat(f32, my_integer) * 0.5;

In the latest zig version, you need to do:

const my_float = @as(f32, @floatFromInt(my_integer)) * 0.5;

Which seems a bit verbose. I was wondering if there might be a shorter way, maybe in the std library.

Similarly, casting pointers has also become more verbose:

const glDebugMessageControl = @as(c.PFNGLDEBUGMESSAGECONTROLKHRPROC, @ptrCast(c.eglGetProcAddress("glDebugMessageControl"))).?;

Whereas I could do this before:

const glDebugMessageControl = @ptrCast(PFNGLDEBUGMESSAGECONTROLKHRPROC, c.eglGetProcAddress("glDebugMessageControl")).?;

I mean, it's just a bit more verbose but the additional parentheses makes it harder to read, and since it's quite a common thing it becomes a bit annoying.

Cheers!

sly fog
#

const my_float: f32 = @floatFromInt(my_integer)