How to find square root with 20 digit precision in C++? - c++

Hi guys I'm stuck with this homework where I need to find the root of equation using Bisection method with precision 10^-20 aka 0.00000000000000000001 so at first I though it was cause I wasn't using long double and also L at the end of the numbers, however even when I use it my last 3 digits are not correct, for the code that is given below ask you to give the number for a in my case is 5 , so I get 2.3227751229355622087
while the correct answer should be 2.3227751229355622988, I really can't find my mistake , will be happy if some1 assist me with this problem.
For your reference, here's a description and illustration of the Bisection method.
Here's my code:
#include<iostream>
#include<cmath>
#include<math.h>
#include<iomanip>
using namespace std;
long double f(long double x, long double a);
long double F = 123456L % 100L;
long double f(long double x, long double a)
{
long double sum = pow(x, 5) - a*x - F;
return sum;
}
int main()
{
cout.setf(ios::fixed);
long double a, b, c, fa, fb, fc;
long double e;
long double aa;
bool flag = true;
while (cin >> aa)
{
cout.precision(19);
flag = true;
a = 0L;
b = 10L;
e = 0.00000000000000000001L;
if (f(a, aa)*f(b, aa)>0)
{
flag = false;
}
while(fabs(a-b)>=e){
c = (a + b) / 2.0L;
fa = f(a, aa);
fb = f(b, aa);
fc = f(c, aa);
if (fc == 0)
{
break;
}
if (fa*fc>0)
{
a = c;
}
else if (fa*fc<0)
{
b = c;
}
}
if (flag == true)
{
cout << c << endl;
}
else
{
cout << "NO SOLUTION" << endl;
}
}
return 0;
}

The problem was that I was using the wrong compiler (Visual Studio).
As soon as installed another compiler, there wasn't any problem with the 20 digit precision. As I investigated more, it appears that some compilers have a limit after the decimal to 14 or 15.
If c++ refuses to round your number to higher precision change the compiler :)

Related

Odd function pointer segmentation fault [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have to find the value of x such that f(x)=C, where f is a monotonically increasing function over the interval [a,b]. It has to have a logarithmic complexity so I've made this function which I believe to be correct:
double search(double a, double b, double c, double (*f)(double x)) {
double pivot;
do {
pivot = abs((a-b)/2);
if (abs((*f)(pivot) - c) < 0.001) { //f(x) == c
return pivot;
} else if ((*f)(pivot) > c) {
b = pivot;
} else {
a = pivot;
}
} while (abs(a-b) != 0);
return 0;
}
which works whenever I call it like this:
int main(void) {
double a = 0.0, b = 10.0, c = 5.0;
cout << search(a, b, c, func1) << endl;
return 0;
}
but if I change it to this (the value of b):
int main(void) {
double a = 0.0, b = 100.0, c = 5.0;
cout << search(a, b, c, func1) << endl;
return 0;
}
I get a segmentation fault when the function is called. What is going wrong here?
(I can see that the program crashes when the function is called when I use the VSCode debugger. Otherwise, when I just compile and run it, it seems to get stuck in an infinite loop or something i.e. it doesn't throw the segmentation fault error).
If needed, func1 simply does return 2 * x.
EDIT:
Here's the full .cpp file if someone needs it to reproduce:
#include <iostream>
using namespace std;
double func1(double x) {
return 2 * x;
}
double func2(double x) {
return x + 0.5;
}
double search(double a, double b, double c, double (*f)(double x)) {
double pivot;
do {
pivot = abs((b-a)/2);
if (abs((*f)(pivot) - c) < 0.1) { //f(x) == c
return pivot;
} else if ((*f)(pivot) > c) {
b = pivot;
} else {
a = pivot;
}
} while (abs(b-a) != 0);
return 0;
}
int main(void) {
double a = 0.0, b = 10.0, c = 5.0;
cout << search(a, b, c, &func1) << endl;
cout << search(a, b, c, func1) << endl;
return 0;
}
where changing the value of b to 100.0 will cause the fault to occur.
pivot = abs((a-b)/2); is wrong it as it computes half of the distance from a to b not the middle point of them, use:
pivot = abs((a-b)/2)+a; // provided that *a* is less than *b*

LCM and GCD not working

I made a program for codechef and its wrong apparantly (although all tests have been positive). The code is:
#include <iostream>
using namespace std;
int g (int a,int b){
return b == 0 ? a : g(b, a % b);
}
int l (int a, int b){
return (a*b)/(g(a,b));
}
int main() {
int n;
cin >> n;
int a[n],b[n];
for (int x = 0;x<n;x++){
cin >> a[x] >> b[x];
}
for (int x = 0;x<n;x++){
cout << g(a[x],b[x]) << " "<< l(a[x],b[x]) << endl;
}
return 0;
}
Codechef won't tell me what integers dont work, and im pretty sure my gcd function is legit.
Since gcd is properly defined as the largest non-negative common divisor, you can save yourself the annoying details of signed division, e.g.,
static unsigned gcd (unsigned a, unsigned b)
{
/* additional iteration if (a < b) : */
for (unsigned t = 0; (t = b) != 0; a = t)
b = a % b;
return a;
}
Likewise for lcm; but the problem here is that (a*b) may overflow. So if you have two large (signed) int values that are co-prime, say: 2147483647 and 2147483629, then gcd(a,b) == 1, and (a*b)/g overflows.
A reasonable assumption on most platforms is that unsigned long long is twice the width of unsigned - although strictly speaking, it doesn't have to be. This is also a good reason to use exact types like [u]int32_t and [u]int64_t.
One thing you can be sure of is that a/g or b/g will not cause any issues. So a possible implementation might be:
static unsigned long long lcm (unsigned a, unsigned b)
{
return ((unsigned long long) a) * (b / gcd(a, b)));
}
If your test values are 'positive' (which is what I think you mean), you can cast them prior to (unsigned) prior to call. Better yet - replace all your int variables with unsigned int (though the loop variables are fine), and save yourself the trouble to begin with.

Find root without while and for loop

The program I'm coding should find the root of a given double.
The condition is: you're not allowed to use while and for loops. any kind of loops are not allowed.
Our professor said it's forbidden to use the stdlib function sqrt().
I started to code but it's still not working. hope anyone can help.
#include <cstdlib>
#include <iostream>
using namespace std;
double mysqrt(double a, double b, double c);
int main(int argc, char** argv) {
double dBegin{0};
double dOldroot{0};
double dNewroot{0};
double a{0};1
cin >> dBegin;
dOldroot = dBegin;
mysqrt(a, dOldroot, dNewroot);
cout << dNewroot;
return 0;
}
double mysqrt(double a, double b, double c) {
c = (b + (a / b)) / 2.0;
if (a != 8) {
c = mysqrt(a++, b, c);
}
return c;
}
I'm using Newton-Raphson's Method to find out the square-root of a given number num (in my code).
You may find this video link useful: Click Here. Using this algorithm i've solved this problem.
Here is my code.
#include <iostream>
using namespace std;
/* we are gonna use Newton-Raphson's method to find its square because
it converges quickly, even calculators use this algo. to find the sqr-root */
double find_sqrt(double x, int num, int count) {
if(count == 0)
return x;
double f_x = x*x - num;
double f_dx = 2*x;
double res = x - (f_x/f_dx);
x = find_sqrt(res, num, count -1);
return x;
}
int main() {
double num;
cin >> num;
/* Here 20 is the maximum number of times it will run and
num/2 is the random number send to the function between the range 1 to num */
cout << find_sqrt(num/2, num, 20);
return 0;
}
The result might not be much precise but it will always be almost near to the actual square-root of the number.
Reason: Floating-precision error. You must be knowing about this i believe.
You have junk 1 to cause compile error after double a{0};
Your code will do infinite recursion because the first argument won't updated. Using double for counter isn't also a good idea.
You are throwing away the value returned from mysqrt in main().
Using arguments as local variables without reading its value isn't a good idea.
Fixing these errors, your code will be like this:
#include <cstdlib>
#include <iostream>
using namespace std;
double mysqrt(int a, double b);
int main(int argc, char** argv) {
double dBegin{0};
double dOldroot{0};
double dNewroot{0};
int a{0};
cin >> dBegin;
dOldroot = dBegin;
dNewroot = mysqrt(a, dOldroot);
cout << dNewroot;
return 0;
}
double mysqrt(int a, double b) {
double c = (b + (a / b)) / 2.0;
if (a != 8) {
c = mysqrt(a + 1, b);
}
return c;
}
This code failed to calculate square root, but compiled and exited soon when ran.

Keep getting 1.#INF as my output

#include <iostream>
using namespace std;
double calc(int a, int b);
int main()
{
int n1, n2;
cout << "Enter a number for a: ";
cin >> n1;
cout << "Enter a number for b: ";
cin >> n2;
cout << calc(n1, n2) << endl;
system("PAUSE");
return 0;
}
double calc(int a, int b)
{
double s;
s = (a) / ((sqrt(a / b)));
return s;
}
This program is meant to check whether the two integers are greater than zero. If it is it will calcualte the formula. Otherwise if one of the integers is zero or less than zero it will not return anything and exit the program.
My question here is that no matter what I input for a and b, i keep getting 1.#INF as the output and I have no idea why. I've checked the formula in a seperate program and it worked fine.
Any ideas?
Here, you are operating with int numbers:
s = (a) / ((sqrt(a / b)));
If a is less then b, then a/b (both are integers, remember, so the fractional part of the result will simply be lost) will be equal to 0, which leads to division by 0. You need to cast one of the numbers to double:
s = (a) / ((sqrt(static_cast<double>(a) / b)));
sqrt takes and returns a double. When you call it with integer arguments it will be converted in a double, and will thus get the value of infinity.
change your function signature to:
double calc(double a, double b);
and declare n1 and n2 as double.
You say that the function will exit the program when one of the integers are 0 or less, but where?
Try to check them like this:
Additionally, you should have a check whether a is greater than b
double calc(int a, int b)
{
double s;
if(a <= 0) exit(-1);
if(b <= 0) exit(-1);
if(a < b) exit(-1);
s = (a) / ((sqrt(a / b)));
return s;
}
You are having problems with infinity. For it use isinf. Here is some sample usage:
#include <stdio.h> /* printf */
#include <math.h> /* isinf, sqrt */
int main()
{
printf ("isinf(-1.0/0.0) : %d\n",isinf(-1.0/0.0));
printf ("isinf(sqrt(-1.0)): %d\n",isinf(sqrt(-1.0)));
return 0;
}
output:
isinf(-1.0/0.0) : 1 isinf(sqrt(-1.0): 0

Pow() calculates wrong?

I need to use pow in my c++ program and if i call the pow() function this way:
long long test = pow(7, e);
Where
e is an integer value with the value of 23.
I always get 821077879 as a result. If i calculate it with the windows calculator i get 27368747340080916343.. Whats wrong here? ):
I tried to cast to different types but nothing helped here... What could be the reason for this? How i can use pow() correctly?
Thanks!
The result is doesn't fit in long long.
If you want to deal with very big numbers then use a library like GMP
Or store it as a floating point (which won't be as precise).
Applying modulo:
const unsigned int b = 5; // base
const unsigned int e = 27; // exponent
const unsigned int m = 7; // modulo
unsigned int r = 1; // remainder
for (int i = 0; i < e; ++i)
r = (r * b) % m;
// r is now (pow(5,27) % 7)
723 is too big to fit into a long long (assuming it's 64 bits). The value is getting truncated.
Edit: Oh, why didn't you say that you wanted pow(b, e) % m instead of just pow(b, e)? That makes things a whole lot simpler, because you don't need bigints after all. Just do all your arithmetic mod m. Pubby's solution works, but here's a faster one (O(log e) instead of O(e)).
unsigned int powmod(unsigned int b, unsigned int e, unsigned int m)
{
assert(m != 0);
if (e == 0)
{
return 1;
}
else if (e % 2 == 0)
{
unsigned int squareRoot = powmod(b, e / 2, m);
return (squareRoot * squareRoot) % m;
}
else
{
return (powmod(b, e - 1, m) * b) % m;
}
}
See it live: https://ideone.com/YsG7V
#include<iostream>
#include<cmath>
int main()
{
long double ldbl = pow(7, 23);
double dbl = pow(7, 23);
std::cout << ldbl << ", " << dbl << std::endl;
}
Output: 2.73687e+19, 2.73687e+19