Here is my work on gradient descend algorithm
#include<iostream>
#include<math.h>
#include<float.h>
using namespace std;
double f_prime(double x)
{
return (double)(4.0*powf(x,3)-9.0*powf(x,2));
}
void gradient()
{
double x_old=0;
double x_new=6;
double eps=0.01;
double precision=0.00001;
while(abs(x_new-x_old)>precision)
{
x_old=x_new;
x_new=x_old-eps*f_prime(x_old);
}
cout<<" local minimum at : "<<x_new<<endl;
}
int main()
{
gradient();
return 0;
}
The above code gives me warnings of a non correct conversion from double to float, possible loss of data, so as a result it gives me some undefined values like -1.IND. Can anyone explain why this is?
abs is defined only for int and long types. For floating point numbers use fabs.
Change powf to pow although I'm not sure that will solve your problem.
Related
my solution to find area of a triangle when base and height is given(the question specifically asked to also use float typcasting):
#include <iostream>
using namespace std;
int area()
{
int b=7,h=5;
float area;
area=(float)b*h/2;
return area;//Write a expression to find Area as float using typecasting
}
correct solution:
#include<iostream>
using namespace std;
void area()
{
int b=7,h=5;
float area;
area=(float)b*h/2;
cout<<area;
}
what's wrong with my code?
By itself I would say nothing much.
When you want to calculate a floating point number, makes sure ALL you values are floating point. Eg. not 2 but 2.0f
Casting in C++ is usually done with static_cast (or one of the other cast functions). The notation you use is more "C" style type casting
I am not a big fan of using, "using namespace" specially in larger projects. In source it can be ok, NEVER do this in header files though.
I would code it like this:
float calculate_triangle_area(const int b, const int h) noexcept
{
float area = static_cast<float>(b) * static_cast<float>(h) / 2.0f;
return area;
}
std::cout << calculate_triangle_area(5,7);
Some more notes:
I always make things functions as soon as I can give it a name. This has the added benefit of making your code more "self explaining".
The "const int" this mean you promise you won't change the values of b and h in your calculations.
The noexcept is another promise, but that's a whole topic on its own ;)
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int solve(int t){
float ans = static_cast<float>(320)/100;
cout<<fixed<<setprecision(2)<<ans;
}
int main(){
int t=320;
cout<<solve(t);
return 0;
}
output:3.204745728
how should I get output as 3.20
You are using cout twice, when you only want to output one thing. You are also outputting the return value of solve when you don't return anything from solve, this explains the extra digits that you see.
Either do the cout in solve or do it in main, don't do it in both places.
First way
#include <iostream>
using namespace std;
void solve(int t){
float ans = static_cast<float>(320)/100;
cout<<fixed<<setprecision(2)<<ans; // print the answer
}
int main(){
int t=320;
solve(t);
return 0;
}
Second way
#include <iostream>
using namespace std;
float solve(int t){
float ans = static_cast<float>(320)/100;
return ans; // return the answer
}
int main(){
int t=320;
cout<<fixed<<setprecision(2)<<solve(t);
return 0;
}
Another issue with your code is that you are passing the parameter 320 to solve, but you aren't using it there. You might want to change that
float ans = static_cast<float>(t)/100;
It's quite common for beginners to get confused between returning a value from a function, and printing a value in a function, but these are quite different things. The first code above prints the value in the function, the second code above returns the value from the function (and prints it in main).
I am trying to create a simple (absolute) function in c++, I have created two functions with the same name one that takes an integer and returns an integer and one that takes a float and returns a float but every time I try to run the code I receive this error:
"error: call of overloaded 'absolute(double)' is ambiguous"
I tried changing the input parameters of the second function so that it takes a double and returns a float and the code ran perfectly, I'd like to know why the code won't run when the parameters and return type are both set to float, thank you.
#include <iostream>
#include <fstream>
using namespace std;
int absolute(int x){
if (x<0){
x=-x;
}
return x;
}
float absolute (float x)
{
if (x<0){
x=-x;
}
return x;
}
int main( )
{
cout << absolute(3.5);
}
The type of the literal 3.5 is double, not float.
Choosing either of the overloads would require a conversion. Hence the ambiguity.
You can use 3.5f to make it a float literal.
cout << absolute(3.5f);
A better solution, IMO, would be to use a function template.
template <typename T>
T absolute(T x)
{
return (x < 0 ? -x : x);
}
Read that error message again. Notice how it says double as the argument type it want to use.
That's because floating point constants like 3.5 are of type double. And the compiler don't know if it should convert the double value to an int or a float, thereby giving you the error.
If you want to call the float overload, use 3.5f to make it a float value. Or change your overload to use type double instead of float.
you write that 3.5 is a float this value is not a float it is a double.
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.
Here is a link to the code and I have posted it below too.
#include<math.h>
void pentagon(int n)
{
int k,p[10],a[10],b[10];
if(n<0)
p[n]=0;
if(n==0)
p[n]=1;
for(k=1;k<n;k++)
{
a[k]=((3*pow(k,2))-k)/2;
b[k]=((3*pow(k,2))+k)/2;
}
for(k=1;k<n;k++)
{
p[n]=pow(-1,k-1)(pentagon(n-a[k])+pentagon(n-b[k]));
}
cout<<p[n];
}
int main()
{
pentagon(4);
return(0);
}
I am getting the following error :
In function 'void pentagon(int)':
Line 11: error: call of overloaded 'pow(int&, int)' is ambiguous
compilation terminated due to -Wfatal-errors
Replace 2 with 2.0 as the second argument to pow (line 11, 12).
See also: http://www.cplusplus.com/reference/clibrary/cmath/pow/
Collecting and correcting all the errors (and warnings) leads to the following code (codepad). I made some comments about what changed.
#include <math.h>
#include <iostream> // Some compilers will complain if missing
using namespace std; // Some compilers will complain if missing
// returns int instead of being void
int pentagon(int n)
{
int k,p[10],a[10],b[10];
// recursion end - we want to jump out here right away
if(n<0) return 0;
if(n==0) return 1;
for(k=1;k<n;k++)
{
a[k]=((3*(int)pow(k,2.0))-k)/2; // pow needs double as second argument
b[k]=((3*(int)pow(k,2.0))+k)/2; // and returns double
}
for(k=1;k<n;k++)
{
// pow casting and double as second argument, multiplication with pentagon
p[n]=(int)pow(-1,k-1.0)*(pentagon(n-a[k])+pentagon(n-b[k]));
}
cout<<p[n]<<endl; // cleaner output
return p[n]; // return the calculated value
}
int main()
{
pentagon(4);
return(0);
}
but I guess the underlying algorithm is still wrong, as the output is:
-1084535312
-1084535311
1074838088
0
3
4
0
Here are some errors or improvements I spotted:
Added iostream & using namespace std:
#include <cmath>
#include <iostream>
using namespace std;
Changed pow(k,2) to k*k:
a[k]=((3*(k*k))-k)/2;
b[k]=((3*(k * k))+k)/2;
Add multiplication symbol to p[n] assignment:
p[n] = pow(-1.0,k-1) * (pentagon(n-a[k]) + pentagon(n-b[k]));
The pentagon method needs to return a value in order to use it in the above statement.
You're missing the summation part of the equation.
See http://en.wikipedia.org/wiki/Pentagonal_number_theorem.
Your pentagon function only calculates one term at a time. There is no code that sums all of the terms.