+ 10

[ANSWERED] Anomaly with implementation of algorithm for reversing a number (integer)

Hi SoloLearners, It all started as I read this post https://www.sololearn.com/Discuss/3064986/?ref=app - asking how to reverse a number (integer). I understand this could be easy in Python should we choose to convert the number to string and reverse the string instead. I also have a little idea how to deal with negative sign, still in regards to using string version of the number. I have learned how a number is reversed using loop, modulo and division, and it works just fine disregarding whether number was positive or negative. But my attempt to implement the same logic in Python is failing me on negative numbers. The loop goes infinite when the number was negative. The ultimate question is, why this logic works perfectly fine in C/C++ (and some others I guess) yet it's glitching in Python. Please enlighten me And Thank you in advance 🙏 https://code.sololearn.com/cNLi6Xs9Wlu9/?ref=app https://code.sololearn.com/criN8639x744/?ref=app

25th Jul 2022, 2:23 PM
Ipang
9 Answers
25th Jul 2022, 3:39 PM
Lisa
Lisa - avatar
+ 7
I found that ,in python n//10 first applied n/10 then applies floor(n/10) When it comes to negetives -123/10 => -12. 3 floor( -12.3) returns -13 , not -12. floor(-13/10) => -2 floor( -2/10) => -1 floor( -1/10) => -1 ... .. So it causing infinite loop.. edit: interesting, there is no need to check for negetives for reversing in c/c++. I don't found it previously.. Ipang I am sharing your code to that OP.
25th Jul 2022, 3:17 PM
Jayakrishna 🇮🇳
+ 6
In short, in C++, -1%10 == -1, in python -1 % 10 == 9, consequently -1//10 == -1, not 0. I am in transit and will elaborate later. But maybe you can figure things out from here.
25th Jul 2022, 2:57 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
Lisa Thank you for the read 🙏 It seems Jayakrishna's analysis was inline to what the article described, with additional proofs. I didn't anticipate this actually, I just thought these basic stuffs would just work the same disregarding language. So to sum it up, the difference was due to the different way for C++ and Python in handling division. Glad you shared that, I'm keeping it for my reference in future when I'm confronted with something similar.
25th Jul 2022, 4:17 PM
Ipang
+ 3
lol so what Jaya said is confirmed by the Lisa link, which explains why Ani Jona's - 1//10 = - 1 since floor(-0.1) = - 1 on the other hand though -1 ~ 9 in mod 10 (-1 +10) -2 ~ 8 and so on so it's not that weird Like, Python's looks more like the mod in math, and in C++, according to the shared article it's just a remainder, not equivalency. The article says that for both languages but Python version, due to the way Jaya said it works, kind of looks like what mod really is. Why was % called mod when it really is remainder?
25th Jul 2022, 4:34 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
This one's a little too long a read, not for my tired eyes, now. Just adding it here for additional resource. https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-modulo-operator/
25th Jul 2022, 4:45 PM
Ipang
+ 2
Ani Jona 🕊 Interesting how it works that way. I'm currently trying to search for this oddity on the web, this far no luck yet though. Maybe I should try a better search term Thank you for the insight 🙏
25th Jul 2022, 3:03 PM
Ipang
+ 2
Jayakrishna🇮🇳 I was thinking that possibility but I wasn't sure. Thank you for bringing it up here 🙏 I never would've guessed Python // operation uses a floating point internally without this news
25th Jul 2022, 3:23 PM
Ipang
+ 1
Cool stuff 😎
26th Jul 2022, 8:51 AM
Mugabi David
Mugabi David - avatar