Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #36646 > unrolled thread
| Started by | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| First post | 2019-05-25 12:29 +0200 |
| Last post | 2019-05-25 23:40 +0200 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.javascript
Truth, the final frontier. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-05-25 12:29 +0200
Re: Truth, the final frontier. John G Harris <niam@jghnorth.org.uk.invalid> - 2019-05-25 11:40 +0100
Re: Truth, the final frontier. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-05-25 23:40 +0200
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2019-05-25 12:29 +0200 |
| Subject | Truth, the final frontier. |
| Message-ID | <XnsAA5A7F0878789eejj99@194.109.6.166> |
Truth, the final frontier: console.log( 'This is: ', .1 + .2 != .3 ); // This is: true More here: <https://0.30000000000000004.com/> =========== However: console.log( 'This is: ' + .1 + .2 ); // This is: 0.10.2 -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2019-05-25 11:40 +0100 |
| Message-ID | <os6iee53g5oo4mj3h8hinl558mntut041q@4ax.com> |
| In reply to | #36646 |
On Sat, 25 May 2019 12:29:16 +0200, "Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote: >Truth, the final frontier: > >console.log( 'This is: ', .1 + .2 != .3 ); >// This is: true > >More here: ><https://0.30000000000000004.com/> A well-known gotcha. If anyone complains remind them that answers are very accurate : enough to measure the relativity effect of someone cycling past you. >=========== > >However: > >console.log( 'This is: ' + .1 + .2 ); >// This is: 0.10.2 Another well-known gotcha. The compiler is being "helpful". John
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2019-05-25 23:40 +0200 |
| Message-ID | <4992cd16-e914-2bd0-6233-b99a17142955@PointedEars.de> |
| In reply to | #36646 |
Evertjan. wrote:
> Truth, the final frontier:
>
> console.log( 'This is: ', .1 + .2 != .3 );
> // This is: true
>
> More here:
> <https://0.30000000000000004.com/>
(This is NOT the Web. Tell news.)
Floating-point arithmetic:
{0.1, 0.2, 0.3} ∩ {b | b = ∑ m × 2^i; m ∈ {0, 1}} = ∅.
i ∈ ℤ
0.1 = 2^-4 + 2^-5 + 2^-8 + … = 0.0625 + 0.03125 + 0.00390625 + …
0.2 = 2^-3 + 2^-4 + 2^-7 + … = 0.125 + 0.0625 + 0.0078125 + …
0.3 = 2^-2 + 2^-5 + 2^-6 + … = 0.25 + 0.03125 + 0.015625 + …
> However:
>
> console.log( 'This is: ' + .1 + .2 );
> // This is: 0.10.2
The second statement is fundamentally different from the first statement in
that the method call has only *one* argument. Thus, the expression in the
argument is the result of *string concatenation*.
As those know who have been subscribed to this newsgroup for, say, more than
6 months, if the binary (by contrast to unary) “+” operator is used and one
of the operands evaluates to a String value, then all operands are converted
to String before *string concatenation* is performed:
1. 'This is: ' + .1 + .2
^^^^^^^^^^^ ^^ ^^
String Number Number
2. 'This is: ' + (.1).toString() + (.2).toString
3. 'This is: ' + '0.1' + '0.2'
4. 'This is: 0.10.2'
By contrast:
| > console.log('This is: ' + (.1 + .2));
| This is: 0.30000000000000004
|
| > console.log('This is: ', .1 + .2);
| This is: 0.30000000000000004
|
| > navigator.userAgent
| <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)
| Chrome/57.0.2987.98 Safari/537.36"
∎
Note that console.log() will automatically output a space character between
the evaluation results of its arguments.
Get a real name and address.
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web