Implicit casting acting strange - c++

I have a question about implicit casting for the following code
#include <iostream>
using namespace std;
float max(int a, int b)
{
if (a > b)
return a;
else return b;
}
int main() {
double a, b;
cin >> a >> b;
cout << max(a, b)<<endl;
cout << a;
return 0;
}
Now Supposing that a = 30.5 & b = 26.4.
The anticipated result is 30 however on one computer(MinGW & VS 2005) I get 30.5.
Does anyone have an interpretation for this ? It makes no sense to me.
Edit 1 :
on third line output is 30.5 instead of the anticipated 30
Solution
std::max() is shadowing it, but why it shadows it on one computer and it doesn't on another I didn't investigate in that.
So try to avoid naming your functions or classes with names reserved for the standard library.

This is whats resulting in the weird output:
using namespace std;
When calling max() you may be calling std::max() which may be included in <iostream> with no guarantees. Try this:
cout << ::max(a, b)<<endl; //forces global scope
Should print out 30.

Related

Difference in the function return value and direct operation in cpp

I am not able to understand why the function (log_a_to_base_b) is returning a different value in comparison to the same operation if given with the input values in single line (Please refer below and code for much clearer explanation of problem)
-Basicaly why the function value and the variable x returned values are different
as function return type is INT and the x is also storing the answer in INT shouldn't both answer be same
If given the same value
The function value in the below code is coming as 3 while the x value in the below code is coming as 2
For refrence the log of 445943744 with base of 764 is 3
// C++ program to find log(a) on any base b
#include <bits/stdc++.h>
using namespace std;
int log_a_to_base_b(int a, int b)
{
return log2(a) / log2(b);
}
// Driver code
int main()
{
int a = 445943744;
int b = 764;
cout << log_a_to_base_b(a, b) << endl;
int x=log2(445943744)/log2(764);
cout<<x<<endl;
return 0;
}
// This code is contributed by shubhamsingh10, yousefonweb

(C++) Add int and/or double and display sum

So I have just completed "Sum of two numbers" on SPOJ. My code passed the test cases; however, it doesn't seem very elegant to me. My first approach was to try implementing a template to handle multiple data types. I could not figure out how to successfully do this.
My question: How could this program be written so that it makes use of a class as well as a function template (which handles int and double). I feel that using floor() is a bit weird, inappropriate. If using a template is not a good solution then a better one would be nice to see as well. Thanks.
EDIT:
The solution below works just fine. I am very interested in learning more about classes (OOP is new to me) and I would also like to learn more about templates. Here is a link to the problem on SPOJ: http://www.spoj.com/problems/CHITEST1/
My code:
//For t test cases, output the sum of two numbers
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
double a, b, sum;
cin >> a >> b;
sum = a + b;
if (sum != floor(sum)) cout << sum << endl;
else cout << static_cast<int>(sum) << endl;
}
return 0;
}
First, I'd say that applying !=/= to floating-point types is very dangerous. Even if you're using floor. Use comparisons to some very low epsilon value. (if (abs(a - b) < eps), where epsilon is something like 0.00001, depending on your desired precision).
Second, I don't really understand the idea of floor being here, because noshowpoint cout modifier is enabled by default, which means, 3.3 + 4.7 should print exactly 8, if it calculated without any errors - not 8.00000(I wouldn't rely on that, because it can result in 7.99999998 or 8.00000001 as well). Floor has the same level of reliability, because floor(7.999998) is 7, not 8. To be honest, I'd use round here.
Third, if you want to learn OOP or templates, these are not the tasks you want to do. This site looks like too much of a contest-related, and you're in need of educational info. Look here: http://www.cplusplus.com/doc/tutorial/
Maybe you are looking for this:
#include <iostream>
using namespace std;
template <typename T>
void getAddAndPrint()
{
T a, b;
cin >> a >> b;
T sum = a + b;
cout << sum << endl;
}
int main() {
// your code goes here
getAddAndPrint<double>();
getAddAndPrint<int>();
return 0;
}

Inbuilt __gcd(A,B) function in C++

recently I get to know about a special function in c++ : __gcd(A,B). this will return the greatest common divisor of A and B.
#include<iostream>
#include<algorithm>
using namespace std;
main()
{
cout<<__gcd(10,40); //op: 10
}
is there any special reason for starting function definition with 2 underscores?
It could be as simple as gcd(A,B) like other STL functions.
Names starting with two underscores are reserved for the implementation, which means that you are not allowed to define such names in your code, and there are no standard guarantees what those names mean if they do exist. However, a vendor might choose to document some such names, in which case you can use them with the product for which the vendor documents them.
In C++17 there are standard library functions for GCD and LCM.
#include <iostream>
#include <numeric>
int main ()
{
int a, b;
std::cin >> a >> b;
std::cout << std::gcd(a,b) << '\n';
return (0);
}

xcode is not showing the output

I tried several time to find where is the problem, but I can not find any thing.So, could anyone help me to find the problem and why I can not see a result?
It might seem stupid question, but I new to programming world :)
This is my code :
#include <iostream>
using namespace std;
// There is the declraction of all functions
float max();
float min();
// This is the main program
int main ( int argc, char ** argv )
{
// Here you can find max
max(504.50,70.33);
// Here you can find min
min(55.77, 80.12);
return 0;
}
// This is the max function
int max(float a, float b){
float theMax;
if (a>b) {
theMax = a;
cout <<theMax ;
}
else{
theMax = b;
cout << b;
}
return theMax;
}
// This is the min function
int min( float c, float d){
float theMin;
if (c >d ) {
theMin =c;
cout << theMin;
}
else {
theMin =d;
cout << theMin;
}
return theMin;
}
You're calling std::max and std::min. That's because you wrote using namespace std, and did not declare your own min and max prior to using them. (You did declare two other min and max functions, but those take zero arguments, not two). So, when the compiler sees max(504.50,70.33); the only candidate is std::max.
You declare these overloads:
float max();
float min();
which are functions that take no arguments and return float.
You're calling
max(504.50,70.33);
and
min(55.77, 80.12);
which are functions that takes two doubles and may or may not return anything.
These match std::max and std::min, not the prototypes you declared.
You then define
int min( float c, float d){
which also doesn't match the prototypes you declared.
In other words, these functions are unknown in main, and the functions that are actually called are std::min and std::max.
Don't use using namespace std; - what you save in typing is lost in clarity and debugging.
You should also rename the functions - it's not a good idea to reuse standard library names.

cout uint8_t as integers instead of chars

#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
cout << (uint8_t)123 << endl;
}
This will output { , since {'s ASCII is 123.
But I want to get 123 instead. I found cout << (int)123 << endl; will do this, but I'm not willing to cast uint_8 to int every times. Can I configure cout to achieve this?
I definitely do not condone the solution I am about to suggest. I also suspect that it may not be permitted by the standard, but I cannot prove it, as of yet. If someone can provide me a reference that shows that it is not permitted, then I will delete this answer. Anyway, my tests so far indicate that simply overloading the operator in the global scope seems to work.
#include <iostream>
#include <cstdint>
std::ostream & operator<<(std::ostream & os, std::uint8_t val)
{
return os << static_cast<int>(val);
}
int main()
{
std::uint8_t val = 123;
std::cout << val;
}
I wouldn't have thought this would work, but then I realized that the char/unsigned char/signed char overloads for operator<< are all free functions in the std namespace picked up by ADL. And I guess global functions are considered a better match than ADL functions, but I'm not sure about that.