Using boost multiprecision with trigonometric function - c++

Consider the following code which creates a multiprecision floating-point number 'a' by using boost.
How do I use boost library to invoke trigonometric functions?
For example, I hope to calculate sin(a).
#include <iostream>
#include "boost/multiprecision/cpp_bin_float.hpp"
using namespace std;
using namespace boost::multiprecision;
typedef number<backends::cpp_bin_float<24, backends::digit_base_2, void, boost::int16_t, -126, 127>, et_off> float32;
int main (void) {
float32 a("0.5");
return 0;
}

It looks like there is a limitation in the library. When the precision is dropped too low, the sin implementation no longer compiles.
Some intermediate calculations are being done in double precision. The assignment into the result type would be lossy and hence doesn't compile.
Your chosen type actually corresponds to cpp_bin_float_single. That doesn't compile.
As soon as you select cpp_bin_float_double (precision 53 binary digits) or higher, you'll be fine.
I suppose this limitation could be viewed as a bug in some respects. You might report it to the library devs, who will be able to judge whether the related code could use single-precision floats there without hurting the convergence of the sin approximation.

#include <boost/multiprecision/cpp_bin_float.hpp>
#include <iostream>
using namespace std;
using namespace boost::multiprecision;
int main() {
cpp_bin_float_100 a = 1;
cout << setprecision(50) << endl;
cout << sin(a) << endl;
return 0;
}
I've verified digits with Wolfram Mathematica and they are correct:

Related

Read string math-formula into cpp as double from external input-file

I am relatively new to c++ coding and try to write a program to solve differential equations numerically. I use codeBlocks as the compiler for that and work under windows. The numeric solver already works well.
My program contains of some very long formulas which are created by mathematica and converted into cpp-language. Then the formulas are stored in a .txt-file.I can already read the formula as a string, but not use it to calculate things because the program has to interpret the formula as a double-type math and not as a string. The problem here is, that my formula does not contain only numbers, but letters as variables (their value is set in the program) and other mathematical symbols. That is why I think I cannot just use "atof" (http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof) or other conversion functions. (If I am wrong at this point, I would be glad to learn how to use the function for this problem!)
Here is some example code from my little program:
//Program to solve ODEs
#include <iostream>
#include <math.h>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h> //to measure the time
#include <stdio.h>
#include <conio.h>
using namespace std;
int main(void)
{
double k1=0;
ifstream file("Formelvu1.txt");//file with the fromula
string line;
stringstream longform;
while(getline(file, line)){ //read the formula and store them
longform << line; //store the string in "longform"
cout << longform;
}
return 0;
for(double t=0; t<10; t++){
k1 = (longform) * t; //simple operation with the formula
}
return 0;
}
This code doesn't work, becuase longform is no double...
longform is a string with something like: ab+pow(t,3)-sin(tb)/x.
I already found several questions related to this topic, but none of them was easy enough for me to understand or the right thing I want to do:
How can I convert string to double in C++?
From what I understand is this guy trying the nearest from what I wish to do:
Evaluate math formula from String using ScriptEngine
But I don't understand the code completely.
If it is useful for my problem: What does this part do?
try{
return (Double)engine.eval(tmp);
}
catch(Exception fexp)
{
}
I also heard about parser which can interpret the xpressions line muparser:
http://muparser.beltoforion.de/mup_eval.html
But I don't know if this would be more than I need...
I appreciate every answer/response and help with this problem.
Thank you!
You have to study carefully what is included in the basic C/C++ language and standard library and what not.
However, if you get a C++ compatible expression from mathematica, then you can let the C++ compiler do its work as in
double myfunc(double a, double b, double c, double t, double x) {
return
#include "Formelvu1.txt"
;
}

cmath and math.h giving different answers

When I use the atan function from cmath and math on a floating point number, I seem to get different answers:
#include <cmath>
#include <math.h>
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::setprecision(20) << atan(-0.57468467f) << std::endl;
std::cout << std::setprecision(20) << std::atan(-0.57468467f) << std::endl;
// I get:
// -0.52159727580733605823
// -0.52159726619720458984
}
Why does this happen? Does two libraries implement atan differently?
math.h's atan takes a double and returns a double, yet cmath's is overloaded so that a float argument (as used here) will be used as a float and yield a float result. Thus, the difference in output comes from using two different floating-point types. To make them use the same type, either remove the f at the ends of the numbers or change the first atan to atanf.

Program failed to compile

Here is my code. I am using Dev-C++ 4.9.8.0 and I can not figure out why this wont compile.
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
int main() {
int n; // Number to test for prime-ness
int i; // Loop counter
int is_prime = true; // Boolean flag...
// Assume true for now.
// Get a number form the keyboard.
cout << "Enter a number and press ENTER: ";
cin >> n;
// Test for prime by checking for divisibility
// by all whole numbers from 2 to sqrt(n).
i = 2;
while (i <= sqrt(n)) { // While i is <= sqrt(n),
if (n % i == 0) // If i divides n,
is_prime = false; // n is not prime.
i++;
}
// Print results
if (is_prime)
cout << "Number is prime." << endl;
else
cout << "Number is not prime." << endl;
system("PAUSE");
return 0;
}
Im getting various error messages about overloading. Can someone please help me figure out why its not compiling correctly.
As predicted, the error is a result of a symbol clash between an std::sqrt and sqrt, due to your use of using namespace std;.
The header cmath has a function called std::sqrt, and the symbol name sqrt is being imported into your namespace because of your using namespace std;. Even though you are not including math.h, for some reason your compiler is importing that header as well, and math.h defines a sqrt function.
The compiler is complaining that it does not know which sqrt to use.
The correct solution to this is to not use using namespace std;. See also: Why is "using namespace std" considered bad practice?.
In your particular case, you can replace using namespace std; with the following:
using std::cout;
using std::cin;
using std::endl;
to avoid typing std:: in front of these all the time.
To be honest the compiler should not be including math.h, and as others have pointed out, using a 10+ year old compiler is just silly. Use a modern compiler.
Edit: Also, please, never again post half a dozen comments in a row to communicate a multi-line error message. Just edit your original post.
This compiles fine in gcc.
Although thre are some things you can improve like not including #include <stdlib.h> including stdlib instead of stdlib.h and making is_prime bool.
line 22 call of overloaded 'sqrt(int&)' is ambiguous
try sqrt<int>(n) or sqrt((int) n)
#Andrey gives you the answer: use ::sqrt(n) or std::sqrt(n) or include math.h instead of cmath. The best is still as he suggests: don't use using namespace std;.
My advice: switch to a more mainstream compiler like gcc, clang or Visual Studio. They better conform with the standard.
The book i am using uses Dev-C++
i don't want to be mean but switch to another book two. I wouldn't trust a book that makes you include stdlib.h. It's a header from the time that C wasn't standardized yet. So... yeah... switch the book...

Printing pi with large precision using GMP

Right now I have this little code that I want to print pi to x decimals:
#include <iostream>
#include <gmpxx.h>
#include <math.h>
using namespace std;
int main()
{
mpf_set_default_prec(1000);
mpf_t pi;
mpf_init(pi);
mpf_set_d(pi, atan(1)*4);
cout << pi << endl;
}
I just sat default_prec to 1000 as I thought that would give me plenty of decimals, but no matter what I set it to, I only get 5. How can I print more?
The problem is atan(1)*4 will only be evaluated with single (or double) precision, since this part of the code has nothing to do with gmp and will use the standard c++ types. The program will evaluate atan(1)*4 first and then convert the result into an mpf_t.
On the GMP homepage there is a page on how to calculate Pi with many digits.

C++ Program Outputting Wrong Number

This is my program:
#include "stdafx.h"
#include <iostream>
using namespace std;
double areaofcircle(double r)
{
return 3.14 * r * r;
}
int main()
{
cout << areaofcircle(5);
}
I should be getting the output of "78.5" but I keep getting "78.512". What is going on?!
I've also tried float, but I still get the same output.
Also just a side question, do I really need to add "return 0;" to the main function?
One more side question, do I need to write "using namespace std;" inside every function, or can I just write it outside of everything, like how I've been doing it.
You're passing the literal for an integer (5) so somewhere an implicit conversion is required to turn it into a double. You would be better off passing 5.0. The C++ default for doubles requires no specifier so your 3.14 is fine. (specifying a float requires 3.14f). That said, I tried both with 5 and 5.0 and got 78.5 both times on my compiler.
How you're using the std namespace is fine, but as pointed out it does bring ALL of the standard namespace into scope. I see this a lot in teaching material. It is better to just use
using std::cout;
or just explicitly add std::cout to all uses. However, there is nothing "wrong" from a compilation standpoint in the way you did it.
I think you are doing something wrong. I tried the same on GCC compiler, and I do get 78.5. Which compiler are you using?
Regarding your other questions
It is always a good idea to return the state of your program from main. Usually, you can return EXIT_SUCCESS if everything works okay, else you can return EXIT_FAILURE.
No it is not necessary to include using namespace std. Instead, it is bad practice to pollute your standard namespace. You should include only those functions that you use very frequently.
To read more about C++. Check this link
Hope this helps.
Tried a few experiments on VS 2008 to see if I could get a similar error. By changing pi to a float I do get 78.500002622604370 which is different but not the same as your issue. But I do get 78.5 when pi is a double.
I'd recommend you let us know which compiler and version you're using, then possibly someone may be able to help.
#include "stdafx.h"
#include <iostream>
const double pi = 3.14;
double areaofcircle(double r)
{
return pi * r * r;
}
int _tmain(int argc, _TCHAR* argv[])
{
double temp = areaofcircle(5);
std::cout << temp;
return 0;
}