I want to round a float number.
in python, i have:
round(x, 2) # 3.1415 -> 3.14
but in c++, i find round function can only round to integer.
Is there any similar method in c++?
AFAIK the standard library provides no such function, but it shouldn't be too hard to roll out your own:
#include <iostream>
#include <cmath>
// fast pow for int, credit to https://stackoverflow.com/a/101613/13188071
int ipow(int base, int exp)
{
int result = 1;
while (true)
{
if (exp & 1)
result *= base;
exp >>= 1;
if (exp == 0)
break;
base *= base;
}
return result;
}
double round_prec(double n, int prec)
{
return std::round(n * ipow(10, prec)) / ipow(10, prec);
}
int main()
{
std::cout << round_prec(3.1415, 2) << '\n';
}
Output:
3.14
This is, however, a bit of a roundabout way of doing it, there's probably a better way that I don't know of.
You can use the built-in round functions and some scientific notation.
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
float x = 3.14159;
int val = 2;
x = round(x * pow(10, val)) / pow(10, val);
cout << x << endl;
return 0;
}
Related
So, I'm pretty inexperienced with calculus, and in C++, so please bear with me if I'm misunderstanding this completely. I'm supposed to be testing this Taylor series program for convergence, which I'm told is when the output value does no change. However, the way mine is written out, it seems impossible as I'm iterating through for loops while implementing it. After around 12 it's no longer accurate to the library sin(); but I'm not sure if that's the same thing because it doesn't seem to be. Advice on what I'm looking for would be grand, I really appreciate it. Apologies again if this question is stupid!
Here is my code:
#include <iostream>
#include<cmath>
using namespace std;
double getFactorial (double num)
{
long double factorial = 1.0;
for (int i = 1; i <= num; ++i)
{
factorial *= i; //iterates from 1 to num value, multiplying by each number
}
return factorial;
}
double taylorSin(double num){
double value=0;
for(int i=0;i<20;i++){
value+=pow(-1.0,i)*pow(num,2*i+1)/getFactorial(2*i+1);
}
return value;
}
int main ()
{ cout<<getFactorial(6);
for(double i=1;i<=12;i++){
//loops through given amount of values to test function
double series=i; //assign double type variable with value of i
//cout<<"Taylor function result is "<<taylorSin(series)<<endl;
//cout<<"Library sin result is "<<sin(series)<<endl;
}
return 0;
}
Based on your answer, I wrote a taylor summing program:
#include <iostream>
#include <cmath>
#include <limits>
#include <concepts>
template<typename F>
concept my_lambda = requires(F f, unsigned long long int x) {
{ f(x) } -> std::same_as<long double>;
};
template<my_lambda ftn>
long double Taylor_sum(ftn term) noexcept {
using namespace std;
long double value = 0, prev = 0;
unsigned long long int i = 0;
try {
do {
if (i == numeric_limits<unsigned long long>::max() || !isfinite(prev)) return numeric_limits<long double>::quiet_NaN();
prev = value;
value += term(i++);
} while (prev != value);
return value;
}
catch (...) { return numeric_limits<long double>::quiet_NaN(); }
};
int main() {
using namespace std; long double x; cin >> x ;
long double series_sum = Taylor_sum([x](unsigned long long int i) -> long double { return /*Your implementation here*/; });
if (!isfinite(series_sum)) cout << "Series does not converge!" << endl;
else {
cout << "Series converged, its value is : " << series_sum << endl;
cout << "Compared to sin : " << sinl(x) << endl;
}
return 0;
}
Although comparing term before & after summing is not much rigorous way to check convergence, but this approach is usual approach in practice.
Note: When I used this function for large x, it differed from the std::sin() and diverged when x is large enough. This is because the floating-point arithmetic has limited precision (std::sin() is more accurate because it takes the periodic nature of the original sine function)
I want to print 12345.0 as 0.1235e+05. But the below code gives 1.23450e+04. Is there a way to control the format of std::scientific?
#include <iostream>
#include <string>
#include <sstream>
int main()
{
double a = 12345.0;
std::stringstream s;
s.precision(3);
s << std::scientific;
s << a;
std::cout << s.str() << std::endl;
return 0;
}
The output is
1.235e+04
I also tried the code here Print exponential notation with one leading zero with C++ but there is a problem about rounding. If I set precision to 4 it does not round and gives 0.1234E5 however it should have been 0.1235E5.
Any solution to this?
Then that isn't scientific form. Scientific form must produce a number between 1-10 and multiplied by a power of 10
Source: http://chemed.chem.purdue.edu/genchem/topicreview/bp/ch1/scinot.html
A problem with re-inventing double to string conversion is the many corner cases.
Consider printing values near 99995.0 to 4 places
void foo(double value, int precision) {
if (value == 0.) {
printf("%g\n", 0.0);
}
int exponent = (int) floor(log10(fabs(value)));
double base = value / pow(10, exponent);
base /= 10.0;
int p = precision;
base = round(base * pow(10, p)) / pow(10, p);
exponent++;
printf("%.*fE%d\n", p, base, exponent);
bar(value, precision);
}
foo(99994.99, 4);
foo(99995.01, 4);
Prints
0.9999E5
1.0000E5
Rather than the hoped for
0.9999E5
0.1000E6
Rather than try to out-compute the library conversion of floating point to a string, simply post process the string to the desired format.
Below is C, yet OP is looking for C++, so take this as a guide.
void bar(double value, int precision) {
precision--;
char buf[400];
snprintf(buf, sizeof buf, "%.*e", precision, value);
if (isfinite(value)) {
char *e = strchr(buf, 'e');
char *first_digit = e - precision - 2;
int expo = atoi(e + 1) + 1;
printf("%.*s0.%c%.*sE%d\n", !isdigit((unsigned char )buf[0]), buf,
*first_digit, precision, first_digit + 2, expo);
} else {
printf("%s\n", buf);
}
}
bar(99994.99, 4);
bar(99995.01, 4);
Prints
0.9999E5
0.1000E6
"If I set precision to 4 it does not round and gives 0.1234E5 however it should have been 0.1235E5."
This is a result of the default rounding mode is "round to nearest, ties to even" and OP is hoping for "round to nearest, ties to away".
Code such as base = round(base * pow(10, p)) / pow(10, p); may achieve OP's goal in select cases as the multiplication and division here can incur rounding due to imprecision, sometimes in the desired direction. This is not reliable across all double to achieve "round to nearest, ties to away".
I corrected the error in the solution given in the link Print exponential notation with one leading zero with C++ by adding two lines of code. I also repeated the process for correct formatting:
int p = stream.precision();
base = round(base * pow(10, p)) / pow(10, p);
It now makes correct rounding and gives 0.1235E5. Here is the whole code:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
class Double {
public:
Double(double x) : value(x) {}
const double value;
};
std::ostream& operator<< (std::ostream& stream, const Double& x) {
// So that the log does not scream
if (x.value == 0.) {
stream << 0.0;
return stream;
}
int exponent = floor(log10(std::abs(x.value)));
double base = x.value / pow(10, exponent);
// Transform here
base /= 10.0;
int p = stream.precision(); // This line is added to correct the code
base = round(base * pow(10, p)) / pow(10, p); // This line is added to correct the code
exponent += 1;
double x2 = base * pow(10, exponent);
exponent = floor(log10(std::abs(x2)));
base = x2 / pow(10, exponent);
// Transform here
base /= 10.0;
base = round(base * pow(10, p)) / pow(10, p); // This line is added to correct the code
exponent += 1;
stream << base << "+e" << exponent;
return stream;
}
int main()
{
//double a = 12345.0;
double a = 99995.01;
std::stringstream s;
s.precision(4);
s << Double(a);
std::cout << s.str() << std::endl;
return 0;
}
Given two integers X and Y, whats the most efficient way of converting them into X.Y float value in C++?
E.g.
X = 3, Y = 1415 -> 3.1415
X = 2, Y = 12 -> 2.12
Here are some cocktail-napkin benchmark results, on my machine, for all solutions converting two ints to a float, as of the time of writing.
Caveat: I've now added a solution of my own, which seems to do well, and am therefore biased! Please double-check my results.
Test
Iterations
ns / iteration
#aliberro's conversion v2
79,113,375
13
#3Dave's conversion
84,091,005
12
#einpoklum's conversion
1,966,008,981
0
#Ripi2's conversion
47,374,058
21
#TarekDakhran's conversion
1,960,763,847
0
CPU: Quad Core Intel Core i5-7600K speed/min/max: 4000/800/4200 MHz
Devuan GNU/Linux 3
Kernel: 5.2.0-3-amd64 x86_64
GCC 9.2.1, with flags: -O3 -march=native -mtune=native
Benchmark code (Github Gist).
float sum = x + y / pow(10,floor(log10(y)+1));
log10 returns log (base 10) of its argument. For 1234, that'll be 3 point something.
Breaking this down:
log10(1234) = 3.091315159697223
floor(log10(1234)+1) = 4
pow(10,4) = 10000.0
3 + 1234 / 10000.0 = 3.1234.
But, as #einpoklum pointed out, log(0) is NaN, so you have to check for that.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
float foo(int x, unsigned int y)
{
if (0==y)
return x;
float den = pow(10,-1 * floor(log10(y)+1));
return x + y * den;
}
int main()
{
vector<vector<int>> tests
{
{3,1234},
{1,1000},
{2,12},
{0,0},
{9,1}
};
for(auto& test: tests)
{
cout << "Test: " << test[0] << "," << test[1] << ": " << foo(test[0],test[1]) << endl;
}
return 0;
}
See runnable version at:
https://onlinegdb.com/rkaYiDcPI
With test output:
Test: 3,1234: 3.1234
Test: 1,1000: 1.1
Test: 2,12: 2.12
Test: 0,0: 0
Test: 9,1: 9.1
Edit
Small modification to remove division operation.
(reworked solution)
Initially, my thoughts were improving on the performance of power-of-10 and division-by-power-of-10 by writing specialized versions of these functions, for integers. Then there was #TarekDakhran's comment about doing the same for counting the number of digits. And then I realized: That's essentially doing the same thing twice... so let's just integrate everything. This will, specifically, allow us to completely avoid any divisions or inversions at runtime:
inline float convert(int x, int y) {
float fy (y);
if (y == 0) { return float(x); }
if (y >= 1e9) { return float(x + fy * 1e-10f); }
if (y >= 1e8) { return float(x + fy * 1e-9f); }
if (y >= 1e7) { return float(x + fy * 1e-8f); }
if (y >= 1e6) { return float(x + fy * 1e-7f); }
if (y >= 1e5) { return float(x + fy * 1e-6f); }
if (y >= 1e4) { return float(x + fy * 1e-5f); }
if (y >= 1e3) { return float(x + fy * 1e-4f); }
if (y >= 1e2) { return float(x + fy * 1e-3f); }
if (y >= 1e1) { return float(x + fy * 1e-2f); }
return float(x + fy * 1e-1f);
}
Additional notes:
This will work for y == 0; but - not for negative x or y values. Adapting it for negative value is pretty easy and not very expensive though.
Not sure if this is absolutely optimal. Perhaps a binary-search for the number of digits of y would work better?
A loop would make the code look nicer; but the compiler would need to unroll it. Would it unroll the loop and compute all those floats beforehand? I'm not sure.
I put some effort into optimizing my previous answer and ended up with this.
inline uint32_t digits_10(uint32_t x) {
return 1u
+ (x >= 10u)
+ (x >= 100u)
+ (x >= 1000u)
+ (x >= 10000u)
+ (x >= 100000u)
+ (x >= 1000000u)
+ (x >= 10000000u)
+ (x >= 100000000u)
+ (x >= 1000000000u)
;
}
inline uint64_t pow_10(uint32_t exp) {
uint64_t res = 1;
while(exp--) {
res *= 10u;
}
return res;
}
inline double fast_zip(uint32_t x, uint32_t y) {
return x + static_cast<double>(y) / pow_10(digits_10(y));
}
double IntsToDbl(int ipart, int decpart)
{
//The decimal part:
double dp = (double) decpart;
while (dp > 1)
{
dp /= 10;
}
//Joint boths parts
return ipart + dp;
}
Simple and very fast solution is converting both values x and y to string, then concatenate them, then casting the result into a floating number as following:
#include <string>
#include <iostream>
std::string x_string = std::to_string(x);
std::string y_string = std::to_string(y);
std::cout << x_string +"."+ y_string ; // the result, cast it to float if needed
(Answer based on the fact that OP has not indicated what they want to use the float for.)
The fastest (most efficient) way is to do it implicitly, but not actually do anything (after compiler optimizations).
That is, write a "pseudo-float" class, whose members are integers of x and y's types before and after the decimal point; and have operators for doing whatever it is you were going to do with the float: operator+, operator*, operator/, operator- and maybe even implementations of pow(), log2(), log10() and so on.
Unless what you were planning to do is literally save a 4-byte float somewhere for later use, it would almost certainly be faster if you had the next operand you need to work with then to really create a float from just x and y, already losing precision and wasting time.
Try this
#include <iostream>
#include <math.h>
using namespace std;
float int2Float(int integer,int decimal)
{
float sign = integer/abs(integer);
float tm = abs(integer), tm2 = abs(decimal);
int base = decimal == 0 ? -1 : log10(decimal);
tm2/=pow(10,base+1);
return (tm+tm2)*sign;
}
int main()
{
int x,y;
cin >>x >>y;
cout << int2Float(x,y);
return 0;
}
version 2, try this out
#include <iostream>
#include <cmath>
using namespace std;
float getPlaces(int x)
{
unsigned char p=0;
while(x!=0)
{
x/=10;
p++;
}
float pow10[] = {1.0f,10.0f,100.0f,1000.0f,10000.0f,100000.0f};//don't need more
return pow10[p];
}
float int2Float(int x,int y)
{
if(y == 0) return x;
float sign = x != 0 ? x/abs(x) : 1;
float tm = abs(x), tm2 = abs(y);
tm2/=getPlaces(y);
return (tm+tm2)*sign;
}
int main()
{
int x,y;
cin >>x >>y;
cout << int2Float(x,y);
return 0;
}
If you want something that is simple to read and follow, you could try something like this:
float convertToDecimal(int x)
{
float y = (float) x;
while( y > 1 ){
y = y / 10;
}
return y;
}
float convertToDecimal(int x, int y)
{
return (float) x + convertToDecimal(y);
}
This simply reduces one integer to the first floating point less than 1 and adds it to the other one.
This does become a problem if you ever want to use a number like 1.0012 to be represented as 2 integers. But that isn't part of the question. To solve it, I would use a third integer representation to be the negative power of 10 for multiplying the second number. IE 1.0012 would be 1, 12, 4. This would then be coded as follows:
float convertToDecimal(int num, int e)
{
return ((float) num) / pow(10, e);
}
float convertToDecimal(int x, int y, int e)
{
return = (float) x + convertToDecimal(y, e);
}
It a little more concise with this answer, but it doesn't help to answer your question. It might help show a problem with using only 2 integers if you stick with that data model.
Here is the C++ program i wrote to solve the above series:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int factorial(int a)
{
if (a > 1)
return a * factorial(a - 1);
else
return 1;
}
float series(float x, int n, float b)
{
if (abs(pow(x, n) / factorial(n)) < pow(10, -6) || abs(pow(x, n) / factorial(n)) == pow(10, -6)) { return b; }
else return b = (pow(x, n) / factorial(n)) + series(x, n + 1, b);
}
int main()
{
float x;
cout << "Enter x: "<<endl;
cin >> x;
cout << "E^x = " << series(x,0,0);
system("pause");
return 0;
}
It works fine when abs(x) < 2 but when abs(x) >= 2 this error appears:
Unhandled exception at 0x00F02539 in 33b.exe: 0xC00000FD: Stack
overflow (parameters: 0x00000001, 0x00F22FF8). occurred
I want to know why does this happen and how can i fix it?
Your problem is too deep recursion. Consider loop instead.
float series(float x)
{
const float epsilon = 1e-6f;
double error = 1;
double res = 1.f;
int iter = 1;
while (abs(error) > epsilon) {
error *= (x / iter++);
res += error;
cout << error << endl;
}
return res;
}
int main()
{
cout << "E^x = " << series(3);
system("pause");
return 0;
}
To be clearer about what happens:
When you call a function inside another function, the context of the parent function is saved to make room for the new context. When you make millions of inception, the memory stack in charge to save these context is full and overflows.
This is a Stack Overflow.
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int factorial[200];
int Factorial(int a)
{ if(a>0){
factorial[a]=a * factorial[a-1];
return factorial[a];
}
else
factorial[a]=1;
return 1;
}
double series(double x, int n, double b)
{ double temp=(abs(pow(x, n)) / Factorial(n));
if (temp <= 0.000001) { return b; }
else return (temp + series(x, n + 1, b));
}
int main()
{
float x;
cout << "Enter x: "<<endl;
cin >> x;
cout << "E^x = " << series(x,0,0);
system("pause");
return 0;
}
umm this solution is working. all i did was i took your code removed abs(pow(x, n) / factorial(n)) wherever its repeating and intialised to a new variable temp. then instead of < || == u can directly put <=. and rather than invoking a a function to calculate .000001 every time you could just give that value to reduce time further. however i believe that the reason why the code may not have worked is too much recursion. so for factorials i used dynamic programming to reduce its complexity. the above code is working perfectly fine.
Can you give me advice about precision of computing Taylor series for an exponent? We have a degree of exponent and a figure of precision calculating as imput data. We should recieve a calculating number with a given precision as output data. I wrote a program, but when I calculate an answer and compare it with embedded function's answer, it has differents. Can you advice me, how I can destroy a difference between answeres? formula of exponent's calculating
#include "stdafx.h"
#include "iostream"
#include <math.h>
#include <Windows.h>
#include <stdlib.h>
using namespace std;
int Factorial(int n);
double Taylor(double x, int q);
int main()
{
double res = 0;
int q = 0;
double number = 0;
cout << "Enter positive number" << "\n";
cin >> number;
cout << "Enter rounding error (precision)" << "\n";
cin >> q;
cout << "\n" << "\n";
res = Taylor(number, q);
cout << "Answer by Taylor : " << res;
cout << "Answer by embedded function: " << exp(number);
Sleep(25000);
return 0;
}
int Factorial(int n) {
int res = 1;
int i = 2;
if (n == 1 || n == 0)
return 1;
else
{
while (i <= n)
{
res *= i;
i++;
}
return res;
}
}
double Taylor(double x, int q) {
double res = 1;
double res1 = 0;
int i =1;
while (i)
{
res += (pow(x, i) / Factorial(i));
if (int(res*pow(10, q)) < (res*pow(10, q)))
{//rounding res below
if ( ( int (res * pow(10,q+1)) - int(res*pow(10, q))) <5 )
res1 = (int(res*pow(10, q))) * pow(10, (-q));
else
res1 = (int(res*pow(10, q))) * pow(10, (-q)) + pow(10,-q);
return res1;
}
i++;
}
}
There are two problems in your code. First, the factorial is very prone to overflow. Actually I dont know when overflow occurs for int factorials, but I remember that eg on usual pocket calculators x! overflows already for x==70. You probably dont need that high factorials, but still it is better to avoid that problem right from the start. If you look at the correction that needs to be added in each step: x^i / i! (maths notation) then you notice that this value is actually much smaller than x^i or i! respectively. Also you can calculate the value easily from the previous one by simply multiplying it by x/i.
Second, I dont understand your calculations for the precision. Maybe it is correct, but to be honest for me it looks too complicated to even try to understand it ;).
Here is how you can get the correct value:
#include <iostream>
#include <cmath>
struct taylor_result {
int iterations;
double value;
taylor_result() : iterations(0),value(0) {}
};
taylor_result taylor(double x,double eps = 1e-8){
taylor_result res;
double accu = 1; // calculate only the correction
// but not its individual terms
while(accu > eps){
res.value += accu;
res.iterations++;
accu *= (x / (res.iterations));
}
return res;
}
int main() {
std::cout << taylor(3.0).value << "\n";
std::cout << exp(3.0) << "\n";
}
Note that I used a struct to return the result, as you should pay attention to the number of iterations needed.
PS: see here for a modified code that lets you use a already calculated result to continue the series for better precision. Imho a nice solution should also provide a way to set a limit for the number of iterations, but this I leave for you to implement ;)