I am trying to implement a cube root after Herons method.
Square root is working fine , but I am strugling with cubes root.
My formula for cubes root is: x[i+1]= (3*x[i] + N/x[i]*x[i])/4
#include <cstdlib>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
double Heron(int Nummer)
{
double x = Nummer/2;
for(int NumAppr = 0;NumAppr<100;NumAppr++)
{
x = (3*x+Nummer/x*x)/4;
}
//double y = x/3;
cout << "root is: " << x << endl;
return x;
}
int main()
{
Heron(27);
system("PAUSE");
return EXIT_SUCCESS;
}
Hmmm. You're missing parentheses around x*x:
x = (3*x+Nummer/(x*x))/4;
To get the cube root, simply call std::cbrt:
#include <cmath>
#include <iostream>
int main()
{
std::cout << "Root is: " << std::cbrt(27) << std::endl;
}
// Output: "Root is: 3"
(live demo)
Related
I'm starting learning c++ and stepped on this problem, trying to make the following calculation: place + (place / 10)² which if place = 90 it should be 171.
#include <iostream>
#include <iomanip>
#include <cmath>
#include "TestFunction.h"
using namespace std;
int main() {
TestFunction test1 ("John", 90);
test1.getInfo();
}
here is the TestFunction header
#include <iostream>
#include <string>
#include <iomanip>
class TestFunction {
public:
TestFunction(std::string userName, int userPlace) {
name = userName;
place = userPlace;
}
int getPlace() {
return place;
}
int getResult() {
return num1;
}
void getInfo() {
std::cout << "Using getPlace():" << getPlace() << std::endl;
std::cout << "Using getResult(): " << getResult() << std::endl;
std::cout << "Using num1: " << num1 << std::endl;
std::cout << "calculate here: " << getPlace() + pow(getPlace() / 10, 2) << std::endl;
}
private:
std::string name;
int place;
int num1 = place + pow(place / 10, 2);
};
and get this result:
Using getPlace():90
Using getResult(): -2147483648
Using num1: -2147483648
calculate here: 171
I really don't know what I am missing when trying to use getResult() or num1, any advice or simple explanation will be welcome, thanks.
You need to keep track of when your calculations are done.
The init of num1 is done earlier than the initialisation of place, which is something to avoid at all cost.
You could move that calculation into the constructor:
TestFunction(std::string userName, int userPlace) {
name = userName;
place = userPlace;
num1 = place + pow(place / 10, 2);
}
There are other ways, but this is probably most accessable to you.
I'd like to display some integer with currency index (I mean dot and double zero)
For example like here:
#include <iostream>
int main() {
int w1=700,c1=99,c2=98;
double noh2o=w1*(100.0-c1)/100.0;
double w2=noh2o+noh2o/(100.0-c2)*c2;
std::cout<<w2<<std::endl;
}
If somebody can help me I will be thankful.
You are supposed to do it with the locale library.
Mostly copied from https://en.cppreference.com/w/cpp/io/manip/put_money like so:
#include <iomanip>
#include <iostream>
#include <locale>
int main() {
long double val = 239.9;
std::cout.imbue(std::locale("en_US.UTF-8"));
std::cout << std::showbase
<< "en_US: " << std::put_money(val)
<< std::endl;
return 0;
}
Use std::fixed and std::setprecision.
Try it online!
#include <iostream>
#include <iomanip>
int main() {
int w1=700,c1=99,c2=98;
double noh2o=w1*(100.0-c1)/100.0;
double w2=noh2o+noh2o/(100.0-c2)*c2;
std::cout << std::fixed << std::setprecision(2) << w2 << std::endl;
}
Output:
350.00
I have a code which will convert the float value to string, i have written like below
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
return 0;
}
But the problem is when my value is 10.66 its coming 10.66 but when its 10.80 its coming like 10.8 or when its 10.00 its coming 10 only .
How can i print the complete value
Try this code .
Use the setprecision function with '2' .
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
float myFloat= 10.80;
stringstream stream;
stream << fixed << setprecision(2) << myFloat;
string s = stream.str();
cout<<"value = " << s;
return 0;
}
The trailing zeros are only kept if you set either fixed or scientific mode.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 4.2;
cout << fixed << setprecision(2);
cout << x << endl;
return 0;
}
It seems you want something like below.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << fixed << setprecision(2) << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
}
Probably the least complicated way would be to use printf instead of std::cout. There you can specifically specify how many digits are to be displayed.
#include "stdio.h"
printf("%3.2f",myfloat);
where 3 is the # of digits before and 2 the # of digits after the dot, either can be left out. Append '\n' to the string if you want a new line.
EDIT: Ok, I did not know about setprecision(2).
I'm trying to figure out if this is a bug in Eigen or something I'm doing wrong. I simply want the dot product of two complex vectors [ 1 , i] and [1 , -i]. The answer is 1*1 + i*(-i) = 2. But Eigen gives zero as answer. See code below:
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <map>
#include <sys/sysinfo.h>
#include <algorithm>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main( void ){
VectorXcd xd0(2);
complex<double> c_i( 0.0 , 1.0 );
xd0[0] = 1.0 ;
xd0[1] = c_i;
VectorXcd xd0conj = xd0.conjugate();
cout <<" vec is \n" << xd0 <<endl;
cout <<" conj vec is \n" << xd0conj <<endl;
cout << "eigen dot = " << (xd0conj).dot(xd0 ) <<endl;
complex<double> outVal = 0.0;
for( int a=0; a<xd0.size(); a++){
outVal += xd0[a]*xd0conj[a];
}
cout << "naive dot = " << outVal<<endl;
};
From the documentation:
"
If the scalar type is complex numbers, then this function returns the hermitian (sesquilinear) dot product, conjugate-linear in the first variable and linear in the second variable.
"
This product is often used, for example in quantum mechanics.
It computes conj(x)*y which is what you are seeing. I guess you want the simple / ordinary dot:
cout << "eigen dot = " << xd0conj.transpose()*xd0 <<endl;
This outputs
eigen dot = (2,0)
I have a problem using atof,
here is the code:
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(){
std::string num ("1.0");
//std::string num ("1.1");
cout<< atof(num.c_str());
return 0;
}
If the num string is "1.1" , it can correctly cout 1.1. But if I want to keep the zero when the num string is "1.0" (want it to be 1.0 but not 1), what should I do?
You need to use std::fixed and std::setprecision, like so:
std::cout<< std::fixed << std::setprecision(1) << atof(num.c_str());
This will require that you include the iomanip header.
A possible solution is
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::cout.precision(3);
std::cout.setf(std::ios::fixed);
std::string s("1.0");
float f = 0.0f;
sscanf(s.c_str(), "%f", &f);
// alternative way of setting this flags
// std::cout << std::fixed << std::setprecision(3) << f << "\n";
std::cout << f << "\n";
return (0);
}
notice that there are at least 2 ways of accomplishing the same format for the output, I left one of them commented out .