5 ms·
The page never explicitly states what the right answer is, but based on the output of their suggested "correct" perl, we can infer that they expect 1. This is
by Retr0id 3y ago
The page never explicitly states what the right answer is, but based on the output of their suggested "correct" perl, we can infer that they expect 1.
This is just me being idiosyncratic I guess, but if I see a number with a decimal place, I default to interpreting it as an fp64 unless otherwise specified - which yields a "correct" answer of 2.0 (which is not an answer I can get to in my head, admittedly)
If the question-asker wanted integer arithmetic, they'd have left off the ".0", and if they wanted something other than fp64 roundTiesToEven they should've been more explicit :P
- GTP 3y agoCould you please explain me why 2 would be the correct answer?
- Retr0id 3y agoParsing the provided numbers to two IEEE 754 double-precision floats, and then subtracting them, yields 2.0 (due to rounding) In particular, 9999999999999999.0 rounds up to 10000000000000000.0 (note, this behavior depends on the rounding mode, https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules )
- GTP 3y agoBut here rounding is giving you the wrong answer.
- sdenton4 3y agoThe pointing finger is not the moon... 1 is the mathematically correct answer, and the floating point behavior is a noisy approximation.
- burkaman 3y agoThe page is asking which language answers the math question correctly, not which one implements a particular standard correctly. Perl6 also has a correct implementation, it's just using a different standard than the other languages.
- Retr0id 3y ago"math" is just yet another (implicit and at times poorly defined) standard. The answers are all equally "correct" given a particular set of operating rules, the interesting part is what rules they picked. Edit: See also, https://en.wikipedia.org/wiki/Definitions_of_mathematics https://en.wikipedia.org/wiki/Definitions_of_mathematics - "Mathematics has no generally accepted definition"
- burkaman 3y agoI agree that what we call "math" is just a set of rules that could be defined differently, but a standard like IEEE-754 is downstream of math, and is explicitly defined as a method to perform arithmetic (which is part of math). So they aren't on the same level, IEEE-754 is not an alternative standard to math. The answers can all be considered correct by a certain definition, but they are not equally correct in our shared context as human beings who know what math is.
- Retr0id 3y agoMath is not a singular set of rules, it depends on who you ask, and what the context is. We have a pretty decent shared understanding, but it's not perfectly uniform. 59 + 1 can very reasonably be 1:00, for example. I'm not saying 2.0 is the correct answer, I'm just saying it's not any less correct than 1 is.
- II2II 3y agoI wasn't surprised, but that is not what I would expect if I were to see that expression. Two reasons for that: even though there is a decimal, I still mentally parsed it as an integer, and I wouldn't be thnking of the precision of the value. (That said, I would never enter an integer with a decimal, and I would think of precision if it was a float. That said that said, there are other ways to bump into that problem which wouldn't make it so obvious - such as dealing with inputs from a user.)