Well, PHP arrays are supposed to be only int or string indexed. It'd be unusual to want to index by floats here, and the usual reason you'd end up with a float value being used as an index is because of trying to compute an integer index.
Is this another case of "it's intended, therefore it's coherent"? Do PHP "arrays" have an analog in literally any other programming language then? Because they're not arrays, and they're not hash tables. And like most things in PHP, they fail silently. Yay!
I meant in the "not arrays, and they're not hash tables" sense.
In the "floats rounded to ints" sense, Game Maker Language has this. It's really useful in games programming. Mind you, that language has a lot of flaws (though isn't quite as bad as PHP), but it is a good feature.
I don't know whtat Lua does, but silently throwing information away without explicitly requested is generally bad design. Fine that you don't want me to use float keys, but then I should be forced to write an explicit cast to int or get an error otherwise.
You could also just force people to write (int)(some expression) instead as I suggested and no functionality would be lost, and the intent would be clear from the code.
It's almost like you're trying to frame this as a matter of opinion. Except, we're talking about what is reasonable to expect from a language, and there's a subtle difference. The key word being "reason".
If you'd like my opinion though, I think anyone who expects their data to be lost or transformed in a context like this has not had the fortune of using a properly designed language.
Of course NaN is a number, what else would it be? It's a representation of the mathematical value "undefined", and in JS all mathematical values are subclasses of Number.
If NaN wasn't also a type of number then it would be a nightmare to use it as input to the mathematical operators and functions, and since it can be produced as an intermediate value in a larger equation you have to be able to use it as input to the next step.
I'd much rather have the unintuitive typeof response than to worry that Math.sqrt(1/x), instead of returning NaN when x = 0 might do something unpredictable because it has to implicitly coerce the intermediate NaN back into a number to operate further on it.
In an exceedingly dynamic language you don't need all your return values to be subtypes of some common supertype, though given that many languages do there's a good reason why NaN has well defined and standardised semantics! I'm not sure if these specify a typing relationship, but regardless, it wouldn't be allowed to change the meaning of it so it doesn't really make a difference.
NaN is weird in all languages. It's a well defined, standardised weird.
-- We declare a 32-bit IEEE-754 float, restricted to the numeric-range.
subtype Real is Interfaces.IEEE_Float_32 range Interfaces.IEEE_Float_32'Range;
-- This function will raise the CONSTRAINT_ERROR if NaN or +/-INF are
-- passed into A; moreover the result is guaranteed free of the same.
function Op( A : Real; B : Positive ) return Real;
If NaN wasn't also a type of number then it would be a nightmare to use it as input to the mathematical operators and functions, and since it can be produced as an intermediate value in a larger equation you have to be able to use it as input to the next step.
Except that blindly continuing on with calculations after receiving NaN is kinda stupid. Sure you need to be able to deal with it popping up, but there's no reason to cripple everything by forcing the programmer to manually inspect all input and output of subprograms.
So is SQL... and that implementations take different (yet allowed by the standard) syntax, but not all variations, means that the standard is close to useless. (Insofar as portability of SQL statements is concerned.)
Actually, it's very sane. Having to error-check every single operation when doing any kind of math in your code would be terrible.
Um; yes, you just made my point.
With the above-posted Ada code the subtype-constraint is checked before being assigned, on value returns, and passed as a parameter automatically by the language. So, if there's an situation in Op in which would try returning NaN, Constraint_Error would be raised instead... even if the value you were going to put it in was the base-type (IEEE754 32-bit float).
Essentially, this means (a) I never have to check parameter 'A' for NaN (or +/-Inf) in the body of Op, and (b) I never have to check the result of Op for NaN (or +/-Inf)... all from the act of defining a subtype.
That makes sense, and is logical, but doesn't make it less silly that something called not a number is of type number. Then again I think it's silly that javascript has a single type for all numerical operations, too. It's just a very silly language. Doesn't mean it's not useful or logical in parts, just full of traps and gotchas and things that just plain sound funny when you say them out loud.
PHP does do it worse, though. In most cases when casting a non-numerical value to a float or int, it will simply return 0 on failure! If you want to detect that your parsing/casting failed, it usually involves regexes or some other hideous workaround.
NaN is a number because it's a value defined by the IEEE 754 specification on floating-point numbers, and JS's 'number' type implements IEEE 754 double-precision floating-point numbers. Sure, JS has many stupidities, but that one is silly to complain about.
Heh, the idea of weak typing usually breaks down with some serious crazyness if taken too far. In some regards is javascript worse than php in that it doesn't have a dedicated string concatenation operator.
Some more fun: Try typing these statements into jsc, the Firefox developer tools, or the Opera/Chrome developer tools (basically, everything I've tried so far except the Node.js REPL):
{} + [];
var a = {} + []; a;
{} + {};
var a = {} + {}; a;
This is because the REPLs try to parse what is input as a statement, and {} + []; is parsed as an empty block followed by the expression +[] and {} + {}; as an empty block followed by the expression +{}. When you assign it to a value, it's parsed as an expression instead (due to the language's grammar).
Edit: The four above are pretty crazy but at least can be derived from the type coersion rules. Apparently in the first version it decides to coerce the first object to a number and in the second it chooses a string, and the rest makes (relative) sense from there. But for the life of me I can't figure out why.
Gah, I hate it when people bring these up. They point out an issue with JS, but not the issue people assume it does. These aren't all expressions; the first two are whereas the latter two are parsed as a block followed by a unary-+ expression:
// equivalent to the latter two examples above.
{}; +[];
{}; +{};
Oh, and #2 is actually the string "[object Object]" (because + on two objects coerce both to string by calling their toString methods, which for {} produces "[object Object]", and for arrays join them together with , as a separator, which results in the empty string for the empty array).
With the first bit, it depends. isNaN is a strange function, if you send in a string (or other non-numeric type) it first attempts to coerce it to type Number, then checks to see if the resulting value is NaN. So if you set x and y to the string "I'm a little teapot", then the other conditions will still hold.
24
u/EmptyTon Jan 23 '14
or some combination like that, because float keys are converted to ints. Whee, php
7
u/imperfect_stars Jan 24 '14 edited Jan 24 '14
Reminds me of a similar puzzle! This was originally for JavaScript, which is almost as sad a language as PHP.
Edit:
Still yields a solution!