Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14
is it log, or using size_t that is the error? https://code.sololearn.com/cA99GlPl4PCd/?ref=app
15th Mar 2018, 6:23 AM
jay
jay - avatar
+ 11
yes, it seems the compiler is implicitly converting the floating point/double type returned by log to integer. Maybe there is something in here that can explain this behaviour: http://www.learncpp.com/cpp-tutorial/44-implicit-type-conversion-coercion/ http://en.cppreference.com/w/cpp/language/implicit_conversion
15th Mar 2018, 6:59 AM
jay
jay - avatar
+ 11
nice! well, I guess we should be aware that not all compilers are created equal
15th Mar 2018, 7:20 AM
jay
jay - avatar
+ 11
It's C++ 17. You can check the version using the __cpluscplus macro: 201703 is the value corresponding to C++ 17: https://code.sololearn.com/ckHSMY7tWjP4/?ref=app
15th Mar 2018, 6:30 PM
James Flanders
+ 9
how about size_t N = float(log(64)) / float(log(4)); or size_t N = log(64.0) / log(4.0);
15th Mar 2018, 10:15 AM
jay
jay - avatar
+ 9
Have you lodged a bug report?
15th Mar 2018, 10:36 AM
jay
jay - avatar
+ 9
It should return a double or float, but the template I suggested was a possible way to enforce that is was doing so. Prototype(s) for c+11 are: (plural) double log (double x); float log (float x); long double log (long double x); double log (T x); http://www.cplusplus.com/reference/cmath/log/
15th Mar 2018, 11:03 AM
jay
jay - avatar
+ 9
I assure you, they do read them.
15th Mar 2018, 11:05 AM
jay
jay - avatar
+ 9
;) Are we discussing schematics now :), we could change the float conversion to a double conversion. I am not saying this is a fix, it's a work around. I agree there is a problem that needs addressing. As far as I am aware gcc 4.8.1 supports c++11, so yeah. there is a bug somewhere
15th Mar 2018, 11:11 AM
jay
jay - avatar
+ 9
lol, not even a work around, the behaviour still occurs even after forcing the double via a template. So yeah, bug problems in the assignment operation or something.
15th Mar 2018, 11:18 AM
jay
jay - avatar
+ 9
:) I would say you are correct! (narrowing) It would be nice if the compiler had warnings enabled, it would probably allow us to further debug this
15th Mar 2018, 11:49 AM
jay
jay - avatar
+ 8
yeah, I know.. but it's a work around until it actually is updated template<typename T> float log(T num) { return float(std::log(num));} Maybe?
15th Mar 2018, 10:30 AM
jay
jay - avatar
+ 8
I would lodge a bug report regarding this, as it would lend more support for a case for upgrading the compiler.
15th Mar 2018, 10:48 AM
jay
jay - avatar
+ 7
I updated the code I used above if you want to see. It seems it is definitely in the assignment operation.
15th Mar 2018, 11:26 AM
jay
jay - avatar
+ 6
Hey guys, Seems like the compiler got updated! Check it out, your code samples work normally.
15th Mar 2018, 4:06 PM
James Flanders
- 1
i think i see one error on the code in the challenges: #include <iostream> #define func (x) x*x using namespace std; cout <<func (5+2) THE Answer is supose to be: 17 but THE Answer on THE question is another
15th Mar 2018, 10:43 PM
jose tovar rodriguez
jose tovar rodriguez - avatar