Why does 0.-5 evaluate to -5?
Clash Royale CLAN TAG#URR8PPP
Suppose I write 0.5
as 0.-5
in unexpected way, but it can still run. What does 0.
in 0.-5
do so that it can still run and evaluates to -5?
I also tried alert(0.-5+1)
which prints -4, does JavaScript ignore 0.
in 0.-5
?
javascript numbers
|
show 10 more comments
Suppose I write 0.5
as 0.-5
in unexpected way, but it can still run. What does 0.
in 0.-5
do so that it can still run and evaluates to -5?
I also tried alert(0.-5+1)
which prints -4, does JavaScript ignore 0.
in 0.-5
?
javascript numbers
50
0.
is like0.0
. Or just0
.
– Ry-♦
Feb 25 at 3:48
56
What makes you think that0.-5
is an "unexpected way"?
– Nico Haase
Feb 25 at 9:28
12
@NicoHaase: I'm pretty sure the OP used "unexpected way" to mean "I did not intend/expect to write that, but somehow I did".
– Filip Milovanović
Feb 25 at 9:54
12
This works in many other languages too (e.g. Python). I personally don't like it at all. I'd rather have0.-5
be a syntax error and not allow float literals with a trailing period but force people to write.0
at the end.
– Giacomo Alzetta
Feb 25 at 13:22
7
It's especially bad because allowing numbers to end in.
prevents you from writing something like123.toString(16)
(A common trick is to use123..toString(16)
, which is really(123.).toString(16)
)
– 12Me21
Feb 25 at 23:26
|
show 10 more comments
Suppose I write 0.5
as 0.-5
in unexpected way, but it can still run. What does 0.
in 0.-5
do so that it can still run and evaluates to -5?
I also tried alert(0.-5+1)
which prints -4, does JavaScript ignore 0.
in 0.-5
?
javascript numbers
Suppose I write 0.5
as 0.-5
in unexpected way, but it can still run. What does 0.
in 0.-5
do so that it can still run and evaluates to -5?
I also tried alert(0.-5+1)
which prints -4, does JavaScript ignore 0.
in 0.-5
?
javascript numbers
javascript numbers
edited Feb 25 at 9:48
Community♦
11
11
asked Feb 25 at 3:46
mmmaaammmaaa
2,5781514
2,5781514
50
0.
is like0.0
. Or just0
.
– Ry-♦
Feb 25 at 3:48
56
What makes you think that0.-5
is an "unexpected way"?
– Nico Haase
Feb 25 at 9:28
12
@NicoHaase: I'm pretty sure the OP used "unexpected way" to mean "I did not intend/expect to write that, but somehow I did".
– Filip Milovanović
Feb 25 at 9:54
12
This works in many other languages too (e.g. Python). I personally don't like it at all. I'd rather have0.-5
be a syntax error and not allow float literals with a trailing period but force people to write.0
at the end.
– Giacomo Alzetta
Feb 25 at 13:22
7
It's especially bad because allowing numbers to end in.
prevents you from writing something like123.toString(16)
(A common trick is to use123..toString(16)
, which is really(123.).toString(16)
)
– 12Me21
Feb 25 at 23:26
|
show 10 more comments
50
0.
is like0.0
. Or just0
.
– Ry-♦
Feb 25 at 3:48
56
What makes you think that0.-5
is an "unexpected way"?
– Nico Haase
Feb 25 at 9:28
12
@NicoHaase: I'm pretty sure the OP used "unexpected way" to mean "I did not intend/expect to write that, but somehow I did".
– Filip Milovanović
Feb 25 at 9:54
12
This works in many other languages too (e.g. Python). I personally don't like it at all. I'd rather have0.-5
be a syntax error and not allow float literals with a trailing period but force people to write.0
at the end.
– Giacomo Alzetta
Feb 25 at 13:22
7
It's especially bad because allowing numbers to end in.
prevents you from writing something like123.toString(16)
(A common trick is to use123..toString(16)
, which is really(123.).toString(16)
)
– 12Me21
Feb 25 at 23:26
50
50
0.
is like 0.0
. Or just 0
.– Ry-♦
Feb 25 at 3:48
0.
is like 0.0
. Or just 0
.– Ry-♦
Feb 25 at 3:48
56
56
What makes you think that
0.-5
is an "unexpected way"?– Nico Haase
Feb 25 at 9:28
What makes you think that
0.-5
is an "unexpected way"?– Nico Haase
Feb 25 at 9:28
12
12
@NicoHaase: I'm pretty sure the OP used "unexpected way" to mean "I did not intend/expect to write that, but somehow I did".
– Filip Milovanović
Feb 25 at 9:54
@NicoHaase: I'm pretty sure the OP used "unexpected way" to mean "I did not intend/expect to write that, but somehow I did".
– Filip Milovanović
Feb 25 at 9:54
12
12
This works in many other languages too (e.g. Python). I personally don't like it at all. I'd rather have
0.-5
be a syntax error and not allow float literals with a trailing period but force people to write .0
at the end.– Giacomo Alzetta
Feb 25 at 13:22
This works in many other languages too (e.g. Python). I personally don't like it at all. I'd rather have
0.-5
be a syntax error and not allow float literals with a trailing period but force people to write .0
at the end.– Giacomo Alzetta
Feb 25 at 13:22
7
7
It's especially bad because allowing numbers to end in
.
prevents you from writing something like 123.toString(16)
(A common trick is to use 123..toString(16)
, which is really (123.).toString(16)
)– 12Me21
Feb 25 at 23:26
It's especially bad because allowing numbers to end in
.
prevents you from writing something like 123.toString(16)
(A common trick is to use 123..toString(16)
, which is really (123.).toString(16)
)– 12Me21
Feb 25 at 23:26
|
show 10 more comments
5 Answers
5
active
oldest
votes
Trailing digits after a .
are optional:
console.log(0. === 0); // true
So
0.-5
evalutes to
0 - 5
which is just -5
. Similarly,
0.-5+1
is
0 - 5 + 1
which is
-5 + 1
or -4
.
3
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
add a comment |
0.-5
could be successfully parsed as 0.
[1], -
and 5
. Below is the abstract syntax tree for the expression generated by AST explorer:
This (in an unexpected way) is valid JavaScript and evaluates to -5
.
[1] According to the grammar for numeric literals the decimal digits and exponent parts are optional:
NumericLiteral ::
DecimalLiteral
[...]
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
6
And what is wrong with that?
– Salman A
Feb 25 at 10:18
how did you extract the AST?
– koλzar
Feb 26 at 10:38
5
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
add a comment |
In JS you can express a number with optional decimal point.
x = 5.; //5
x = 5. + 6. //11
And as of Tvde1's comment, any Number method can be applied too.
5..toString()
This syntax let us run the Number functions without parentheses.
5.toString() //error
(5).toString() //good
5..toString() //good
5 .toString() // awesome
See this question to find out why.
3
You can even do5..toString()
.
– Tvde1
Feb 25 at 6:15
2
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
3
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,1.0
is floating point but1.
is an integer.
– Sneftel
Feb 25 at 13:14
2
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.float
s have methods, and5.
is afloat
.
– chepner
Feb 25 at 19:58
2
@MasonWheeler It's certainly not the case that any other language where(1).toMethod()
is legal prohibits(1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.
– Sneftel
Feb 26 at 10:29
|
show 9 more comments
I would think that the real answer is not about the decimal point, but is about the minus sign: isn't that going to be interpreted as an operator if it is preceded by anything that looks like a number?
1
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
add a comment |
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
'0.' or '0' is the same in JavaScript because the type is unique for numbers, called Number. The minus operator is between Numbers, try always to convert what you pass to a Number.
In Python is different first is a Float and the second an Integer because it has several types.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54859228%2fwhy-does-0-5-evaluate-to-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Trailing digits after a .
are optional:
console.log(0. === 0); // true
So
0.-5
evalutes to
0 - 5
which is just -5
. Similarly,
0.-5+1
is
0 - 5 + 1
which is
-5 + 1
or -4
.
3
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
add a comment |
Trailing digits after a .
are optional:
console.log(0. === 0); // true
So
0.-5
evalutes to
0 - 5
which is just -5
. Similarly,
0.-5+1
is
0 - 5 + 1
which is
-5 + 1
or -4
.
3
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
add a comment |
Trailing digits after a .
are optional:
console.log(0. === 0); // true
So
0.-5
evalutes to
0 - 5
which is just -5
. Similarly,
0.-5+1
is
0 - 5 + 1
which is
-5 + 1
or -4
.
Trailing digits after a .
are optional:
console.log(0. === 0); // true
So
0.-5
evalutes to
0 - 5
which is just -5
. Similarly,
0.-5+1
is
0 - 5 + 1
which is
-5 + 1
or -4
.
console.log(0. === 0); // true
console.log(0. === 0); // true
edited Feb 26 at 15:02
doppelgreener
4,41963354
4,41963354
answered Feb 25 at 3:48
CertainPerformanceCertainPerformance
94.9k165685
94.9k165685
3
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
add a comment |
3
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
3
3
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
Leading digits are also optional, so .0-5 is also valid
– icenac
Feb 26 at 15:13
add a comment |
0.-5
could be successfully parsed as 0.
[1], -
and 5
. Below is the abstract syntax tree for the expression generated by AST explorer:
This (in an unexpected way) is valid JavaScript and evaluates to -5
.
[1] According to the grammar for numeric literals the decimal digits and exponent parts are optional:
NumericLiteral ::
DecimalLiteral
[...]
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
6
And what is wrong with that?
– Salman A
Feb 25 at 10:18
how did you extract the AST?
– koλzar
Feb 26 at 10:38
5
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
add a comment |
0.-5
could be successfully parsed as 0.
[1], -
and 5
. Below is the abstract syntax tree for the expression generated by AST explorer:
This (in an unexpected way) is valid JavaScript and evaluates to -5
.
[1] According to the grammar for numeric literals the decimal digits and exponent parts are optional:
NumericLiteral ::
DecimalLiteral
[...]
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
6
And what is wrong with that?
– Salman A
Feb 25 at 10:18
how did you extract the AST?
– koλzar
Feb 26 at 10:38
5
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
add a comment |
0.-5
could be successfully parsed as 0.
[1], -
and 5
. Below is the abstract syntax tree for the expression generated by AST explorer:
This (in an unexpected way) is valid JavaScript and evaluates to -5
.
[1] According to the grammar for numeric literals the decimal digits and exponent parts are optional:
NumericLiteral ::
DecimalLiteral
[...]
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
0.-5
could be successfully parsed as 0.
[1], -
and 5
. Below is the abstract syntax tree for the expression generated by AST explorer:
This (in an unexpected way) is valid JavaScript and evaluates to -5
.
[1] According to the grammar for numeric literals the decimal digits and exponent parts are optional:
NumericLiteral ::
DecimalLiteral
[...]
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
edited Feb 26 at 9:25
answered Feb 25 at 10:12
Salman ASalman A
184k67343441
184k67343441
6
And what is wrong with that?
– Salman A
Feb 25 at 10:18
how did you extract the AST?
– koλzar
Feb 26 at 10:38
5
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
add a comment |
6
And what is wrong with that?
– Salman A
Feb 25 at 10:18
how did you extract the AST?
– koλzar
Feb 26 at 10:38
5
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
6
6
And what is wrong with that?
– Salman A
Feb 25 at 10:18
And what is wrong with that?
– Salman A
Feb 25 at 10:18
how did you extract the AST?
– koλzar
Feb 26 at 10:38
how did you extract the AST?
– koλzar
Feb 26 at 10:38
5
5
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
Go to astexplorer.net, choose a language and a parser, paste the code, choose JSON output (or just take a screenshot).
– Salman A
Feb 26 at 10:45
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
oh wow, very nice. Thank you
– koλzar
Feb 26 at 10:49
add a comment |
In JS you can express a number with optional decimal point.
x = 5.; //5
x = 5. + 6. //11
And as of Tvde1's comment, any Number method can be applied too.
5..toString()
This syntax let us run the Number functions without parentheses.
5.toString() //error
(5).toString() //good
5..toString() //good
5 .toString() // awesome
See this question to find out why.
3
You can even do5..toString()
.
– Tvde1
Feb 25 at 6:15
2
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
3
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,1.0
is floating point but1.
is an integer.
– Sneftel
Feb 25 at 13:14
2
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.float
s have methods, and5.
is afloat
.
– chepner
Feb 25 at 19:58
2
@MasonWheeler It's certainly not the case that any other language where(1).toMethod()
is legal prohibits(1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.
– Sneftel
Feb 26 at 10:29
|
show 9 more comments
In JS you can express a number with optional decimal point.
x = 5.; //5
x = 5. + 6. //11
And as of Tvde1's comment, any Number method can be applied too.
5..toString()
This syntax let us run the Number functions without parentheses.
5.toString() //error
(5).toString() //good
5..toString() //good
5 .toString() // awesome
See this question to find out why.
3
You can even do5..toString()
.
– Tvde1
Feb 25 at 6:15
2
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
3
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,1.0
is floating point but1.
is an integer.
– Sneftel
Feb 25 at 13:14
2
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.float
s have methods, and5.
is afloat
.
– chepner
Feb 25 at 19:58
2
@MasonWheeler It's certainly not the case that any other language where(1).toMethod()
is legal prohibits(1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.
– Sneftel
Feb 26 at 10:29
|
show 9 more comments
In JS you can express a number with optional decimal point.
x = 5.; //5
x = 5. + 6. //11
And as of Tvde1's comment, any Number method can be applied too.
5..toString()
This syntax let us run the Number functions without parentheses.
5.toString() //error
(5).toString() //good
5..toString() //good
5 .toString() // awesome
See this question to find out why.
In JS you can express a number with optional decimal point.
x = 5.; //5
x = 5. + 6. //11
And as of Tvde1's comment, any Number method can be applied too.
5..toString()
This syntax let us run the Number functions without parentheses.
5.toString() //error
(5).toString() //good
5..toString() //good
5 .toString() // awesome
See this question to find out why.
edited Feb 26 at 8:58
answered Feb 25 at 4:44
Charlie HCharlie H
9,62342853
9,62342853
3
You can even do5..toString()
.
– Tvde1
Feb 25 at 6:15
2
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
3
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,1.0
is floating point but1.
is an integer.
– Sneftel
Feb 25 at 13:14
2
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.float
s have methods, and5.
is afloat
.
– chepner
Feb 25 at 19:58
2
@MasonWheeler It's certainly not the case that any other language where(1).toMethod()
is legal prohibits(1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.
– Sneftel
Feb 26 at 10:29
|
show 9 more comments
3
You can even do5..toString()
.
– Tvde1
Feb 25 at 6:15
2
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
3
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,1.0
is floating point but1.
is an integer.
– Sneftel
Feb 25 at 13:14
2
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.float
s have methods, and5.
is afloat
.
– chepner
Feb 25 at 19:58
2
@MasonWheeler It's certainly not the case that any other language where(1).toMethod()
is legal prohibits(1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.
– Sneftel
Feb 26 at 10:29
3
3
You can even do
5..toString()
.– Tvde1
Feb 25 at 6:15
You can even do
5..toString()
.– Tvde1
Feb 25 at 6:15
2
2
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
If fact, you have to do 5..toString() to call the method, otherwise, you have to use parenthesis: (5).toString()
– jo_va
Feb 25 at 9:49
3
3
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,
1.0
is floating point but 1.
is an integer.– Sneftel
Feb 25 at 13:14
@PeterMortensen Ruby, for one. Also ALGOL 68. In Common Lisp, weirdly enough,
1.0
is floating point but 1.
is an integer.– Sneftel
Feb 25 at 13:14
2
2
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.
float
s have methods, and 5.
is a float
.– chepner
Feb 25 at 19:58
@FlorianF You wouldn't, but that doesn't mean the parser should be special-cased to reject it.
float
s have methods, and 5.
is a float
.– chepner
Feb 25 at 19:58
2
2
@MasonWheeler It's certainly not the case that any other language where
(1).toMethod()
is legal prohibits (1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.– Sneftel
Feb 26 at 10:29
@MasonWheeler It's certainly not the case that any other language where
(1).toMethod()
is legal prohibits (1.)
from parsing properly. The most traditional approach to parsing (greedy lexing of tokens, including literals, then parsing) produces the Javascript behavior.– Sneftel
Feb 26 at 10:29
|
show 9 more comments
I would think that the real answer is not about the decimal point, but is about the minus sign: isn't that going to be interpreted as an operator if it is preceded by anything that looks like a number?
1
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
add a comment |
I would think that the real answer is not about the decimal point, but is about the minus sign: isn't that going to be interpreted as an operator if it is preceded by anything that looks like a number?
1
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
add a comment |
I would think that the real answer is not about the decimal point, but is about the minus sign: isn't that going to be interpreted as an operator if it is preceded by anything that looks like a number?
I would think that the real answer is not about the decimal point, but is about the minus sign: isn't that going to be interpreted as an operator if it is preceded by anything that looks like a number?
answered Feb 25 at 21:28
Sean_999Sean_999
811
811
1
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
add a comment |
1
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
1
1
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
Then, at least in some point of view, it still is about the decimal point being interpreted as part of a number literal, otherwise what preceded the minus sign wouldn't "looks like a number".
– Pac0
Feb 26 at 1:19
add a comment |
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
'0.' or '0' is the same in JavaScript because the type is unique for numbers, called Number. The minus operator is between Numbers, try always to convert what you pass to a Number.
In Python is different first is a Float and the second an Integer because it has several types.
add a comment |
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
'0.' or '0' is the same in JavaScript because the type is unique for numbers, called Number. The minus operator is between Numbers, try always to convert what you pass to a Number.
In Python is different first is a Float and the second an Integer because it has several types.
add a comment |
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
'0.' or '0' is the same in JavaScript because the type is unique for numbers, called Number. The minus operator is between Numbers, try always to convert what you pass to a Number.
In Python is different first is a Float and the second an Integer because it has several types.
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
'0.' or '0' is the same in JavaScript because the type is unique for numbers, called Number. The minus operator is between Numbers, try always to convert what you pass to a Number.
In Python is different first is a Float and the second an Integer because it has several types.
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
console.log(0. - 5) // -5
console.log(0 - 5) // -5
console.log('0.' - 5) // -5
console.log('0' - 5) // -5
console.log(0.-5 === -5) // true
edited Feb 26 at 15:04
doppelgreener
4,41963354
4,41963354
answered Feb 26 at 10:29
koλzarkoλzar
535517
535517
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54859228%2fwhy-does-0-5-evaluate-to-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
50
0.
is like0.0
. Or just0
.– Ry-♦
Feb 25 at 3:48
56
What makes you think that
0.-5
is an "unexpected way"?– Nico Haase
Feb 25 at 9:28
12
@NicoHaase: I'm pretty sure the OP used "unexpected way" to mean "I did not intend/expect to write that, but somehow I did".
– Filip Milovanović
Feb 25 at 9:54
12
This works in many other languages too (e.g. Python). I personally don't like it at all. I'd rather have
0.-5
be a syntax error and not allow float literals with a trailing period but force people to write.0
at the end.– Giacomo Alzetta
Feb 25 at 13:22
7
It's especially bad because allowing numbers to end in
.
prevents you from writing something like123.toString(16)
(A common trick is to use123..toString(16)
, which is really(123.).toString(16)
)– 12Me21
Feb 25 at 23:26