Timeline for Is there a programming language where 1/6 behaves the same as 1.0/6.0?
Current License: CC BY-SA 3.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 13, 2012 at 15:26 | comment | added | supercat | @Sjoerd: The issue isn't just one of output; many numbers will round, during computation, to the exactly-correct value. Because 1/6 is 1.010101... * 2^-3 and 1/2 is 1.010101... * 2^-2, 1.0/3.0 will work just as well as 1.0/6.0. Actually, when using double-precision floating-point numbers, the expression ((1.0/N)*N == 1.0) will be true for 853,037,383 of the first 1,000,000,000 values of N including all values below 49; the expression (n/d)*d==n will be true for many whole-number double pairs as well; the smallest d where it fails is with 29/7. | |
| Aug 11, 2012 at 2:18 | comment | added | Sjoerd | @user281377 Printed values usually are rounded to avoid values like 1.0000000000001. Try ((1/6)*6 - 1). And as @supercat noted, it might be coincide that it works for 6, so please try (1/3)*3-1 (and 6, and 7, ...) as well. | |
| Aug 11, 2012 at 2:15 | comment | added | Sjoerd | @Supercat Okay, you could be right that (1.0/6.0)*6.0 might be rounded twice, resulting in exactly 1.0. However, if (1.0/6.0)*6.0 happens to be 1.0, (1.0/3.0)*3.0 will not, and vice versa. So whether the result is 'correct' depends on bit representation of the numbers, and it will not be correct for all values. | |
| Aug 9, 2012 at 19:29 | comment | added | user281377 | Sjoerd: Those I testet (Oracle SQL and Lua) output 1. | |
| Aug 9, 2012 at 18:46 | comment | added | supercat | @Sjoerd: It's not too surprosing that it's exact, actually. Consider in decimal the scenario of 1/11 * 11 with all values accurate to five significant figures. The value of 1/11 is 9.0909 * 10^-2. Multiply by 11 and one would get 99.9999 * 10/-2 before rounding. Round to five significant figures and the result will be 1.0000 * 10^0. Note that the key is that the mantissa of 1/6 is "...0101010101...". If the last bit of a representation is a "1", multiplying it by six and rounding will yield 1. If the last bit were zero, it would not. | |
| Aug 9, 2012 at 17:49 | comment | added | Sjoerd | I would be surprised to see (1.0/6.0)*6 being exactly equal to 1! The rounding of the result of (1.0/6.0) will lead to a small difference. (Although there will be a few languages that default to infinite precision) | |
| Aug 9, 2012 at 15:29 | history | edited | gnat | CC BY-SA 3.0 | clarification copied into answer from comments |
| Aug 9, 2012 at 15:11 | comment | added | MattDavey | @RocMartí yes, it really is... | |
| Aug 9, 2012 at 8:49 | comment | added | user281377 | Accidentally, in all those langauges 1/6 results in .166666667, or 0.16666666666667 or something similar. I chose the ((1/6)*6)==1 variant to get rid of those little differences, but it looks like I overestimated the math skills of some people. | |
| Aug 9, 2012 at 8:13 | comment | added | Roc Martí | That's not the question. | |
| Aug 9, 2012 at 8:12 | history | answered | user281377 | CC BY-SA 3.0 |