I have an annoying problem in JavaScript.
> parseInt(1 / 0, 19)
> 18
Why does the parseInt
function return 18
?
I have an annoying problem in JavaScript.
> parseInt(1 / 0, 19)
> 18
Why does the parseInt
function return 18
?
The result of 1/0
is Infinity
.
parseInt
treats its first argument as a string which means first of all Infinity.toString()
is called, producing the string "Infinity"
. So it works the same as if you asked it to convert "Infinity"
in base 19 to decimal.
Here are the digits in base 19 along with their decimal values:
Base 19 Base 10 (decimal)
---------------------------
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
a 10
b 11
c 12
d 13
e 14
f 15
g 16
h 17
i 18
What happens next is that parseInt
scans the input "Infinity"
to find which part of it can be parsed and stops after accepting the first I
(because n
is not a valid digit in base 19).
Therefore it behaves as if you called parseInt("I", 19)
, which converts to decimal 18 by the table above.
parseInt('Infini',24)
. –
Araminta n
is also a valid digit, so it actually ends up doing parseInt("Infini", 24)
. –
Preponderant parseInt
explicitly converts the argument to be parsed to string by calling ToString
on it. What is the correct string presentation of Infinity
? "Infinity"
seems reasonable to me. But there is no implicit conversion anywhere. –
Fogle parseInt
is meant to parse a string, not an arbitrary object on which toString
is called. The ability to pass in Infinity
(NOT a string) and have it converted to "Infinity"
is in fact an non-obvious, implicit conversion. –
Busyness int
to long
in C is very well documented, still implicit though. –
Friedcake x/0
to return undefined
, not Infinity
- just like mattdeboard says. –
Annmarie int
to long
or what is described in that part of the article at all, it's closer to calling itoa
, except of course it's not integer to string but double (which can be Infinity
) to string –
Fogle int
to long
, or int
to string
, or converting a string
to a TextWriterFormat<'T>
in F# are all implicit as long as the automatic calling of itoa or another suitable conversion function is handled automatically and not called explicitly by the programmer. –
Friedcake 2*parseInt(1/0,19)+6
gives you the ultimate answer ... ;-) –
Theophrastus parseInt
is meant to parse a string", as indeed the "parse" part of its name implies (and as all reputable JS references state), so if somebody deliberately passes an argument that is not a string they really shouldn't be too surprised if they don't get whatever result they expected. –
Chaworth parseInt
is willing to parse an argument where only a prefix is a valid number in the given base? Rather than report an error and/or return undefined
when the input has garbage after the numeric prefix, it simply ignores it. Unfortunately, this is quite common; PHP does the same thing, as does C's atoi()
and similar functions. –
Crownwork Here's the sequence of events:
1/0
evaluates to Infinity
parseInt
reads Infinity
and happily notes that I
is 18 in base 19parseInt
ignores the remainder of the string, since it can't be converted.Note that you'd get a result for any base >= 19
, but not for bases below that. For bases >= 24
, you'll get a larger result, as n
becomes a valid digit at that point.
parseInt
will accept is 36, since there are 26 letters in the English alphabet, and the convention is to use digits then letters as the set of valid digits in the given base. –
Rexer Infinity
to "Infinity"
... –
Lobbyism parseInt
expects a string as its first argument, so Infinity
is coerced to "Infinity"
; also that should be an ordered list, not an unordered one. But the accepted answer already explains everything… –
Squadron To add to the above answers:
parseInt is intended to parse strings into numbers (the clue is in the name). In your situation, you don't want to do any parsing at all since 1/0 is already a number, so it's a strange choice of function. If you have a number (which you do) and want to convert it to a particular base, you should use toString with a radix instead.
var num = 1 / 0;
var numInBase19 = num.toString(19); // returns the string "Infinity"
To add to the above answers
parseInt(1/0,19)
is equivalent to parseInt("Infinity",19)
Within base 19 numbers 0-9
and A-I
(or a-i)
are a valid numbers. So, from the "Infinity" it takes I
of base 19 and converts to base 10 which becomes 18
Then it tries to take the next character i.e. n
which is not present in base 19 so discards next characters (as per javascript's behavior of converting string to number)
So, if you write parseInt("Infinity",19)
OR parseInt("I",19)
OR parseInt("i",19)
the result will be same i.e 18
.
Now, if you write parseInt("I0",19)
the result will be 342
as I X 19 (the base)^1 + 0 X 19^0
= 18 X 19^1 + 0 X 19^0
= 18 X 19 + 0 X 1
= 342
Similarly, parseInt("I11",19)
will result in 6518
i.e.
18 X 19^2 + 1 X 19^1 + 1 X 19^0
= 18 X 19^2 + 1 X 19^1 + 1 X 19^0
= 18 X 361 + 1 X 19 + 1 X 1
= 6498 + 19 + 1
= 6518
© 2022 - 2024 — McMap. All rights reserved.
if
might help. – Obadias0 / 0 == NaN
:) – Inaint(1/0, 19)
raises aZeroDivisionError
, andint('Infinity', 19)
raises aValueError
. If you consider this to be similar to js.... – Decahedron