+ 5

Why this code gives 153 is Armstrong when run in vs code and gives correct output when run in Sololearn

Why this code gives 153 is Armstrong when run in vs code and gives correct output when run in Sololearn? https://sololearn.com/compiler-playground/cq4XoOXk9HlB/?ref=app

28th Oct 2024, 10:27 AM
Shashikant Pandey
Shashikant Pandey - avatar
17 Answers
+ 4
Shashikant Pandey you're right, amstrong += remainder*remainder*remainder; is the safer method in this case. I also found another suggestion: amstrong += round(pow(remainder,3)); Jan there seem to be a bug in your code. Too many amstrong numbers...
29th Oct 2024, 12:39 AM
Bob_Li
Bob_Li - avatar
+ 4
Bob_Li already updated
29th Oct 2024, 4:45 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Mihaly Nyilas in vs code this code not give correct output
28th Oct 2024, 11:34 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Bob_Li actually I asked chat gpt it says amstrong number convert into float because of pow function then I removed pow and used multiplying 3 times then code excited properly you try with you own vs code same code of me please and tell me
28th Oct 2024, 3:48 PM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Bob_Li am using mingw c++ I don't know but 153 it gives me it's not amstrong when I use pow gpt say it happens because change datatype
28th Oct 2024, 4:53 PM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Bob_Li But I don't understand one thing when I had defined amstrong is integer type then why it store float or double in it? Doesn't matter I use pow or whatever but when I defined it is integer then how it get into float or double
29th Oct 2024, 2:54 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Bob_Li actually when I cout every step in program then like when I give input 153 then it gives 3 remainder and 27 cube of 3 then next time 5 remainder and 124 cube of 5 I am observing only at that step all error happen because of pow function then I removed all code and I just did pow(5, 3) then it gives 125 then why it gives 124 in program but one more thing I also saw there is no value in float or double all are in integer only problem happen because of cube of 5 become 124 I don't know how here not possible to share screenshot otherwise I try share with you that is why I don't like c++ 🥲 python is ❤
29th Oct 2024, 4:03 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 3
Bob_Li Jan Guys is this question was not worth? Why don't you guys appreciate it you know what am not new to it I was since scl time but my previous account is blocked just because of one argument in dm with someone then now started new I hope rest you understand. @bob_Li I don't understand what hard part is done for me? Yes I had did python also there is no such variable define data type python is ❤ but everyone say do c/c++ python is not enough for job etc so as compulsion I am doing c++ also because DSA is mostly available in c++ so....
29th Oct 2024, 7:46 AM
Shashikant Pandey
Shashikant Pandey - avatar
+ 1
C an C++ needs to be compiled. Your code is a good example. It compiles and runs fine in Sololearn, but problematic in your machine. You still have to make sure it works as expected on other platforms. But an equivalent code in Python would probably work ok in Sololearn and vscode... because it is interpreted by basically the same Python interpreter. compiled languages are good because you can optimize if for your device, interpreted languages are good because it is convenient to use.
29th Oct 2024, 7:57 AM
Bob_Li
Bob_Li - avatar
0
153 is Armstrong number, as well as 371. for me gives a correct output
28th Oct 2024, 10:58 AM
Mihaly Nyilas
Mihaly Nyilas - avatar
0
did you doublecheck the code in vscode? There might be a typographical error. you probably made an error is the "is" and "is not" message... or maybe did not copy the originalNum = num;
28th Oct 2024, 12:30 PM
Bob_Li
Bob_Li - avatar
0
I guess it depends on your machine and compliler. It didn't give me the wrong answer when I tried it on my laptop. But yes, pow could give you sneaky bugs because of it's implementation. I never realized that. https://stackoverflow.com/questions/59724493/why-cant-i-reproduce-floating-point-issues-with-pow-function https://stackoverflow.com/questions/26860574/pow-implementation-in-cmath-and-efficient-replacement
28th Oct 2024, 4:03 PM
Bob_Li
Bob_Li - avatar
0
pow works with double and ints are actually converted to double internally. Why they don't have int overload is a mystery to me, but that's how the powers that be decided to do it. Maybe you are using a 32 bitcomputer? Not sure, but rounding errors gives you wrong values for certain values https://stackoverflow.com/questions/2398442/why-isnt-int-powint-base-int-exponent-in-the-standard-c-libraries
29th Oct 2024, 3:39 AM
Bob_Li
Bob_Li - avatar
0
Shashikant Pandey pow is just badly implemented. Overthinking the edge cases while breaking the simple use case ... C++ unfortunately have things like these. Python is fine, but learning C++ is still worthwhile and learning things like these will definitely be helpful later on. maybe try updating your compiler to see if it makes any difference?
29th Oct 2024, 4:10 AM
Bob_Li
Bob_Li - avatar
0
Then the conclusion is that writing cross-platform code is not simple at all. Maybe that's why some code look bloated and overly complicated. Trying to write codes that compiles and work on different platforms is hard. Python have the advantage of being interpreted. The hard part is done for you.
29th Oct 2024, 5:39 AM
Bob_Li
Bob_Li - avatar
0
Jan oops, I was thinking of 3-digit numbers ... you're right . no bug 😅
29th Oct 2024, 6:59 AM
Bob_Li
Bob_Li - avatar
0
Hi
29th Oct 2024, 9:28 PM
Vlog
Vlog - avatar