Hacker News new | ask | show | jobs
by TinkersW 213 days ago
The author refers to casting ints to floats but seems to actually be talking about converting. Casting is when you change the type, but don't change the data..

I don't really think much of Zig myself for other reasons, but comptime seems like a good design.

2 comments

> Casting is when you change the type, but don't change the data..

Is that the case? That's not what I think of when I think of C-style casts.

    float val = 12.4;
    int val_i = (int) val;
The representation in memory of `val` should not match that of `val_i`, right? The value is encoded differently and the quantity is not preserved through this transformation. I don't think that means that the data weren't changed.

Maybe you're thinking of aliasing/type-punning? Casts in C do perform conversions as they do in C++.

Casting and type conversion are synonyms: https://en.wikipedia.org/wiki/Type_conversion
The post didn't say "type conversion", just conversion, like the int with value 3 landing in memory after calling atoi("3").
The post gave the example of casting a float to an int. That is a (type) conversion.

The point is that certain casts/type conversions can change the underlying data.

> like the int with value 3 landing in memory after calling atoi("3").

That's something else entirely. People may colloquially call this "converting a string to an integer", but what we're realling doing here is parsing a string as an integer.