Live data from Hacker News

An intro to Zig's integer casting for C programmers

lagerdata.com

31–40 of 135 posts

Re: An intro to Zig's integer casting for C programmers

#31
post #23
post #7

Earlier quoted context omitted.

Or Modula-2, NEWP, BASIC, or really most languages that aren't copy paste compatibel with C. Ada suffered from its domain, original price of compilers, and the few UNIX vendors that cared to offer compiler like Sun, it was an additional acquisiation on top of UNIX SDK. Everyone knows that only newbies do coding errors in C, so why spend the extra money? /s

Is anyone actively using Modula-2? Actively as in starting new projects, compiler improvements, tooling, community etc.

The GNU M2 compiler is kept up to date with GCC, it is just kept out of tree.

https://www.nongnu.org/gm2/homepage.html

However I doubt the language gets much use nowadays beyond some legacy code bases, its opportunity is now gone.

Re: An intro to Zig's integer casting for C programmers

#32
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

> there's an incredibly safe and expressive language that's used in high-reliability applications like missile guidance systems, that nobody seems to talk about.

Maybe because they assume that it's only used for that purpose? You talk about rocket science and you expect people to jump on the bandwagon...

Re: An intro to Zig's integer casting for C programmers

#33
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

> Make something idiot-proof and the world invents a better idiot.

:D

My argument is that if I need that insane speed and safety features have to be off I should actively work for that not the other way around that I should actively work for safety.

Why? Because we forget to do things.

> Another language that does safety like this incredibly well is Ada (Ada/SPARK)

I wanted to learn Ada for a long time. Do you have a guide or tutorial how to get into it?

Re: An intro to Zig's integer casting for C programmers

#34

IME the strict conversion rules in Rust and Zig can be quite a bummer for somebody coming from C because they may add a surprising amount of friction in day-to-day coding. Yes, C code is often way too sloppy when it comes to picking the right type (signed vs unsigned vs float), and it conveniently hides the problems if the wrong choice was made. But sometimes the same value needs to be used in integer and floating-po…

Could you give some examples where the same value needs to be used in integer and floating-point math? I'm not quite sure if I understood you correctly, but in case you propose that integer float conversion should happen implicitly I have to disagree. While implicitly converting from integer to float/double would probably be fine, implicitly converting from float/double to integer sounds like a recipe for headaches:…

>Could you give some examples where the same value needs to be used in integer and floating-point math?

When you want to draw a pong game in a finite matrix representation, like a screen in ncurses, you cannot have something like screen[2.1][3.5], so you have to truncate (or round, depending what you want to do) the same float coordinates to write that matrix.

Of course, you could avoid these using fixed point but even there you need a type conversion.

Re: An intro to Zig's integer casting for C programmers

#35

IME the strict conversion rules in Rust and Zig can be quite a bummer for somebody coming from C because they may add a surprising amount of friction in day-to-day coding. Yes, C code is often way too sloppy when it comes to picking the right type (signed vs unsigned vs float), and it conveniently hides the problems if the wrong choice was made. But sometimes the same value needs to be used in integer and floating-po…

Could you give some examples where the same value needs to be used in integer and floating-point math? I'm not quite sure if I understood you correctly, but in case you propose that integer float conversion should happen implicitly I have to disagree. While implicitly converting from integer to float/double would probably be fine, implicitly converting from float/double to integer sounds like a recipe for headaches:…

> Could you give some examples where the same value needs to be used in integer and floating-point math?

Mainly when working with pixels. In some contexts, pixels are clearly integer values (for instance the width and height of a texture in a 3D API is almost always given as integers, a texture with a width of 12.5 pixels simply doesn't make sense).

Computations on 2D pixel coordinates on the other hand need to have subpixel precision, otherwise you'll get jittering artefacts. This results in code where integer values must be converted to floating point before going into computations, and sometimes the results need to be converted back to integer.

I started to add duplicate functions to my C APIs to reduce the need for explicit conversions when using those APIs from stricter languages like Zig or Rust, for instance:

    void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left);

    void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left);
PS: interestingly, even 3D-APIs don't agree here. For instance in OpenGL, the glViewport() function takes integer values, while in D3D11 and Metal a viewport is defined with floating point values.

Re: An intro to Zig's integer casting for C programmers

#36
post #29
post #9

Earlier quoted context omitted.

C programmers are well known to refer to such programming safety as straighjacket since the Pascal days anyway. There was more than enough time to learn why it was the right option to start with.

Rust does go overboard though, e.g. indexing requires `usize`. You either use `usize` for all your integers, or you end up with a cast-salad. arr[i as usize] This is actually risky, because even when you only meant to extend, you can also accidentally truncate or change sign. `as` does all of these things without a warning, and mixed with type inference it can easily lead to surprises. Rust makes it worse by insistin…

Just wait for the updated C++ standard with size_t indexers.

Re: An intro to Zig's integer casting for C programmers

#37
post #2

> If runtime safety is turned off, you get undefined behavior You already know someone will teach their students to always have it off because its "slower" or something. Make something idiot-proof and the world invents a better idiot. Another language that does safety like this incredibly well is Ada (Ada/SPARK), and I'm unsure why people aren't more hyped about it. So many people hype Rust or Zig or whatever new lan…

> incredibly safe […] high-reliability applications like missile guidance systems

https://en.m.wikipedia.org/wiki/Cluster_(spacecraft)

Re: An intro to Zig's integer casting for C programmers

#38

IME the strict conversion rules in Rust and Zig can be quite a bummer for somebody coming from C because they may add a surprising amount of friction in day-to-day coding. Yes, C code is often way too sloppy when it comes to picking the right type (signed vs unsigned vs float), and it conveniently hides the problems if the wrong choice was made. But sometimes the same value needs to be used in integer and floating-po…

Could you give some examples where the same value needs to be used in integer and floating-point math? I'm not quite sure if I understood you correctly, but in case you propose that integer float conversion should happen implicitly I have to disagree. While implicitly converting from integer to float/double would probably be fine, implicitly converting from float/double to integer sounds like a recipe for headaches:…

I agree that floatint conversions are evil. I would make an exception for int to float conversion for literals. When switching from C to Rust I was annoyed by:

   if x > 0 
not compiling, because that's an integer zero, not a float zero! This makes the compiler feel very petty.

Re: An intro to Zig's integer casting for C programmers

#39
post #29
post #9

Earlier quoted context omitted.

C programmers are well known to refer to such programming safety as straighjacket since the Pascal days anyway. There was more than enough time to learn why it was the right option to start with.

Rust does go overboard though, e.g. indexing requires `usize`. You either use `usize` for all your integers, or you end up with a cast-salad. arr[i as usize] This is actually risky, because even when you only meant to extend, you can also accidentally truncate or change sign. `as` does all of these things without a warning, and mixed with type inference it can easily lead to surprises. Rust makes it worse by insistin…

As I'm sure you're aware, this gets lost in bike shed land every time it comes up but Rust could implement `Index` pretty easily. Unlike C you don't need an implicit cast to do the right thing.

Personally, I have datastructure that uses non `usize` indexes I usually wrap my vector/array in in a custom type that implements index on whatever my common index types are.

Re: An intro to Zig's integer casting for C programmers

#40
post #21

Earlier quoted context omitted.

Ada / spark may be a great thing. But how do people know? Is there a good compiler that one can install for free? Are there a few good and comprehensive books / references / guides, available online for free? If you want something to become popular, give it away . Ideally, push it. At the very least, have a limited free version. Or turn a bling eye to small-time piracy, as many vendors did for a long time. Or make it…

There's an Ada compiler as part of gcc. The most prominent project that i know of written in ada is ghdl.

There was at least always the recommendation to only use the version of the Ada compiler released by the FSF as the regular GCC one is/was more encumbered.
Post reply on HN