quick q: double stripped to int?

When I pass 3.0 & 4.0 from calc in main, I expect to see double values inside the function, which are not. The values of 4 and 3 are also returned. Why are the parameters "losing" the decimal place passed to it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>

int calc(int a, int b)
{
	return a+b;
}

double calc(double a, double b)
{
	std::cout<<a<<"  "<<b<<std::endl;
	return a+b;
}

double calc(int pi)
{
	return pi * pi;
}

int main()
{
	const double pi = 3.14159;
	std::cout<<calc(3,4)<<std::endl;
	std::cout<<calc(3.0, 4.0)<<std::endl;
	std::cout<<calc(pi)<<std::endl;


	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

double calc( double a, double b )
{
	std::cout << a << " + "  << b << '\n' ;
	return a+b;
}

int main()
{
    // http://en.cppreference.com/w/cpp/io/manip/showpoint
    std::cout << std::showpoint ;
    std::cout << calc( 3.0, 4.0 ) << "\n\n" ;

    std::cout << std::noshowpoint ;
    std::cout << calc( 3.0, 4.0 ) << '\n' ;
}
Thx working now~
Last edited on
Topic archived. No new replies allowed.