Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Rename @trunc and @truncate #3788

Open
Srekel opened this issue Nov 27, 2019 · 6 comments
Open

Proposal: Rename @trunc and @truncate #3788

Srekel opened this issue Nov 27, 2019 · 6 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@Srekel
Copy link

Srekel commented Nov 27, 2019

There's both @trunc and @truncate. I think at least one of them should change name, but probably both?

Simplest solution:
@trunc -> @floatTruncate
@truncate -> @intTruncate

Alternatively (as suggested by @daurnimator):
Replace @trunc/round/ceil/floor and provide a single builtin that takes an enum:
@round(comptime T, value: T, mode: enum {Floor, Ceil, TowardsZero, AwayFromZero})

I think I prefer that solution for @trunc, but I feel like @intTruncate might be good anyway, to make it clearer.

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Nov 27, 2019
@andrewrk andrewrk added this to the 0.7.0 milestone Nov 27, 2019
@andrewrk
Copy link
Member

Good catch, I hadn't noticed we had such similar names.

One thing to consider, although it doesn't necessarily mean not to do this, is that trunc matches the libc math function.

It's no coincidence - Zig semantics allows the compiler to replace calls to this builtin with calls to libc (when libc is being linked), as well as peephole optimizations that turn sections of code into calls to this builtin. When libc is not being linked Zig puts trunc & others in compiler-rt, so that they are still available.

I like the @round idea, but it does depart from the convention of using the math function names. That's OK though - let's not let C's old decisions hold us down.

For @intTruncate, I'm going to counter-propose @intTrunc. That builtin is one of the safe ones, so I'd like to avoid making the name more punishing to type.

@daurnimator
Copy link
Collaborator

I like the @round idea, but it does depart from the convention of using the math function names. That's OK though - let's not let C's old decisions hold us down.

One extra thing: if it's only going to be for floats, lets call it @floatRound?

Then we can "float round the ceiling" ;)

@ikskuh
Copy link
Contributor

ikskuh commented Nov 29, 2019

I like the idea of an explicit @round() with a mode as it reads more literal:
@round(f32, 1.5, .floor) ⇒ "Round 32-bit float 1.5 downwards"

@ghost
Copy link

ghost commented Apr 29, 2020

I lean toward either obviously named functions

  • @roundTowardNegative(v)
  • @roundTowardPositive(v)
  • @roundTowardZero(v)
  • @roundTowardInfinity(v)

or obviously named enums

  • @round(v, .TowardNegative)
  • @round(v, .TowardPositive)
  • @round(v, .TowardZero)
  • @round(v, .TowardInfinity)

Both are very clear, thanks to keystrokes! Eliminates ambiguous @round() / .Round variants. These functions can also be implemented for fixedpoint numbers (toot).

As for @truncate(v), @truncateHigh(v) and @truncateLow(v) are more descriptive and handy, as we may want to truncate from different ends of the value! Communicate Intent Precisely!

@fengb
Copy link
Contributor

fengb commented Aug 24, 2020

I believe that @truncate should have "cast" in its name, e.g. @truncCast. This is currently the only type shifting builtin that does not (excluding @as). The casting behavior will be much less obvious once #5909 is implemented.

@daurnimator
Copy link
Collaborator

I believe that @truncate should have "cast" in its name, e.g. @truncCast. This is currently the only type shifting builtin that does not (excluding @as). The casting behavior will be much less obvious once #5909 is implemented.

A cast in zig is always a no-op in machine code, and is the programmer making as assertion to the type system about the values.

truncate is a bit-masking operation, not a cast.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

5 participants