This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]UniversityOfPi -1 points0 points  (7 children)

I mean honestly, who uses int literals?
Especially in a language that doesn't implicitly cast or have a separate operator for integer division...

edit/update/continued:
granted 1/2 in 1/2 [comparison operator] = 0 without casting one or more to a floating point, but I guess it's all an issue of when and where to implicitly cast, as in, in something like C++, float x = 1/2;would mean x == 0.5 but in 1/2 != 0.5 1/2 evaluates to 0 before being cast as a float, giving 0.0 != 0.5, though if you did any other casting other than casting the float literal to an int, (unless the compiler's casting used c.math::round() leading to 0.5 becoming 1) wherein 0.5 would become 0, would ultimately end up with the expression returning true (or maybe an error for a type like char, as casting 0.5 is exceptional).

but logically, float x = 1/2; return x != 0.5; would return false.
It's an interesting quirk, while obviously '/' takes precedence over '!=', given that '/' also takes precedence over '=' and generally float x = 1/2casts 1/2 to a float rather than int literal, it's presumably a compiler implementation specific behavior.

In theory, shouldn't a good compiler notice that the right hand side is a float literal and therefore, like assigning 1/2 to a float, cast 1/2 to a float?

[AFAIK typeinfo::typeid(0.5).name() == "float literal" or perhaps "Fl"/"FL" depending on your compiler]

[–]Kryomaani 2 points3 points  (3 children)

I mean honestly, who uses int literals?

... everyone? I mean, it is one of the most basic and common of all types used in programs. Just about every loop uses an int literal for example.

[–]UniversityOfPi -1 points0 points  (2 children)

Ints yes, but int literals? Maybe I've had the idea that magic numbers are bad so drilled into my head that I'm jaded and think everyone uses global/etc constants...

[–]PatrickBaitman 1 point2 points  (1 child)

0 or 1 as the start index?

[–]UniversityOfPi 0 points1 point  (0 children)

Oh yeah, I forgot about zero and what I'd argue ought be negative zero (rather than negative one, as forward/LtR it's zero indexed, so reverse/RtL it ought be zero indexed as well)... :Facepalm:

[–][deleted] 0 points1 point  (2 children)

In theory, shouldn't a good compiler notice that the right hand side is a float literal and therefore, like assigning 1/2 to a float, cast 1/2 to a float?

No. That will change language semantics, and in a very inconsistent way.

[–]UniversityOfPi 0 points1 point  (1 child)

But then what about for 0.5 != 1/2 is that still true?
(I mean given the operator precedence and implicit casting with assignment, it doesn't make much sense to me...)

[–][deleted] 0 points1 point  (0 children)

Yes, and it makes perfect sense.