+ 1
[SOLVED]why I'm getting this weird output?
may somebody explain why the output is not 42? https://code.sololearn.com/cchcznzqzT6n/?ref=app
15 Réponses
+ 5
042 is octal base (8) here not decimal base(10)
+ 3
well, if you know the four basic mathematical operations, you are able to understand at least how decimal (base 10) works ;)
if you write 142, you know that you use digit in range 0..9 (10 different digits/signs) because we have 10 fingers, so that's the more human logical base to use, as we can count with our fingets...
now, the most right digit is called the unit (correspond to one digit), the next one the ten, and next one the hundred... 1 is one, 10 is ten, 100 is hundred, so you could see that we multipli by 10 each time we shift digit by one place to the left (by appending a zero at right): unit is digit*1, ten is digit*10, hundred is digit*10*10.
raising a number to a power mean multiplying that number by itself power times... 10*10 = 10 raised at power 2, 10 is 10 raised at power 1, and 1 is 10 raised at power 0.
Could you say at wich power is raised thousand (1000)?
I'm quite sure you can see that's 10*10*10, so that's 10 raised to power of 3 ;)
Are you getting the principle?
+ 3
visph yes thoroughly
+ 3
so, math operator for "raised to power" is ^ (almost, in real math equation, the power follow the number superscripted), but in most of programming languages having one it is **...)
now 142, can be decomposed to 1*(10^2) + 4^(10^1) + 2*(10^1) = 1*100 + 4*10 + 2*1 = 100+40+2
in other bases, the principle remain the same: the base number describe how many signs are available for "digits"...
in base 2 (binary): 0 or 1 (2 'digits')
in base 16 (hexadecimal): 0..9 + a..f (16 'digits' a=10, b=11,... f=15)
in base 8 (octal, less used, but popular in early years of computers): 0..7 (8 'digits')
octal numbers can be written in literal notation of most of programming language by prefixing them with a zero.
and 042 can now be decomposed as '42' in octal / base 8, and computed as 4*(8^1) + 2*(8^0) = 4*8 + 2*1 = 32 + 2 = 34 (decimal)
+ 3
visph yeah, that works. I'm really really thankful for the quite clear explanation. I finally got it.
+ 2
because 042 is octal (base 8) litteral number... and '42' in octal is (4*8)+2 == 34 (decimal).
for decimal, don't use 0 prefix: 42.
+ 2
you said that 42 in octal is (4*8)+2 but when you change the number to 42 it prints 42 instead of 34
+ 2
I putted quote to '42' to differenciate from a literal in code (meaning the number composed by 4 and 2 digits)
+ 2
in base 10, each digit is multiplied by 10 raised to the position of digit from the right (starting at 0) and then all added, in base n, each digit is similarly multiplied by n raised to the power of the digit position ;)
+ 2
hamid no problem for delay: each of us could have other things to do as well as time to digest ;)
however, answer here cannot be very long, so feel free to ask for clarification if needed ^^
(could I ask what's your math knowledge level? I mean: are you very young, few young, old, quite old, very old?)
+ 2
visph just the way I ask everyone will finally figure that out😂(😪). unfortunately few young or to be more honest very young. as one of my posts shows how much I hate math and I always tried to understand and do things without mixing them up with math while sometimes it's pure math and bypassing them is impossible. I really think that my brain doesn't have a part for analyzing and understanding math(LoL) and the four basic mathematical operations that I know, is actually something that has happened as time went on.(yep that was my math level)
+ 1
parseInt doesn't imply base 10 (decimal); it treat your string number as literal number, so 042 is still octal and 42 is still decimal
+ 1
visph well it's seems you explaining clearly I don't know why I don't follow😬 so please consider these delays in replying as digesting process I need to search more.
0
I ,don't know