I need a way to convert this string into its exact number representation. I've tried quite a few things and, from what I know, this should work (famous last words). I'm trying to stay away from installing libraries and I have A LOT of these numbers to convert.
I understand that it is difficult to represent long decimal numbers in binary, but I need a solution.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string data = "138.6470184568";
cout << atof(data.c_str()) << endl;
return 0;
}
Output
138.647
Turns out that it is converting correctly, but, when asked to cout the number, it only gives 3-4 decimal places. Anyone know why?
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
string data1 = "138.6470184568";
string data2 = "138.6470184568";
double data3 = atof(data1.c_str()) + atof(data2.c_str());
cout << data3 << endl;
cout << setprecision(15) << data3;
return 0;
}
Gives
277.294
277.2940369136
Related
Have googled a lot about avoiding rounding errors but none of them are solving my case. Need to round off 9.99999975e-05 upto 8 or more precision.
Have tried using stringstream along with setprecision which is pretty much the standard way according to other answers on this site.
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main(void){
double val = 9.99999975e-05;
stringstream tmp;
tmp << setprecision(8) << fixed << val;
cout << tmp.str();
return 0;
}
Ideally I would like the result to be 0.00009999
But I am getting 0.00010000 , which I believe is occurring due to rounding off from farthest to most significant '9'.
Any idea how can I stop rounding at 8th precision ?
Need to round off 9.99999975e-05 upto 8 or more precision. (...)
I would like the result to be 0.00009999
Without other test cases I can only guess the OP's intent. So why not truncating the string resulting from the maximum output precision possible?
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <limits>
int main()
{
double val = 9.99999975e-05;
std::stringstream tmp;
tmp << std::setprecision(std::numeric_limits<double>::digits10 + 1)
<< std::fixed << val;
auto repr = tmp.str();
// Truncate the string
repr = repr.erase(repr.find('.') + 9);
std::cout << repr << '\n'; // --> 0.00009999
}
I'll also assume that the OP are already aware of What Every Computer Scientist Should Know About Floating-Point Arithmetic.
Which platform are you using ? You can try using fesetround api in linux and _controlfp in windows.
I get Value 0.000099999998 on linux when rounding set to zero.
However, i will always recommend using a floating point library for such codes.
For the sake of completeness after going through How to truncate a floating point number after a certain number of decimal places (no rounding)? mentioned by #Arne above in comments, here's the changes that I made to above piece of code;
double val = 9.99999975e-05;
cout << setprecision(8) << fixed << trunc(100000000 * val) / 100000000;
return 0;
how to print specific number of digits in c++?For example ,printing 8 digits totally(before and after decimal point combined)
Edit: For further clarification, setprecision sets the digits when i have decimal digits to display.I want to display integer 30 also as 30.000000 ,in 8 digits.
The setprecision command puts fixed no. of digits after decimal and i don't want that.
In short , I want an alternative of c command printf("%8d",N) in C++.
You can do it using setprecision() function from include iomanip and fixed like:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double d = 1000;
double t = d;
int dc=0;
while(t>0.9)
{
dc++;
t= t/10;
}
cout<<"dc:"<<dc<<endl;
cout << fixed;
std::cout << std::setprecision(dc);
std::cout << d;
return 0;
}
The setprecision() will not work fine every time So you have to use fixed as well.
You should use the c++ header iomanip what you want is the setprecision() function:
std::cout << std::setprecision(5) << 12.3456789 << std::endl;
outputs 12.346. It also has other modifiers you can find here
EDIT
If you want to print trailing 0s, you need to also use std::fixed. This says to use that number of digits, regardless of whether or not they are significant. If you want that to be the total number, you could figure out the size of the number, then change the precision you set it to based on that, so something like:
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
double input = 30;
int magnitude = 0;
while(input / pow(10, magnitude))
{
++magnitude;
}
std::cout << std::fixed << std::setprecision(8 - magnitude) << input << std::endl;
return 0;
}
This returns 30.000000. You can also do something similar by outputting to a string, then displaying that string.
I am trying to separate whole number and decimal number
two int variable
#include <iostream>
#include <iomanip>
#include<math.h>
using namespace std;
int main()
{
double initialAmount = 42800.13;
double fractionalPart = initialAmount - floor(initialAmount);
cout<<int(initialAmount)<<"\n"<<fractionalPart<<"\n";
}
Present output is
42800
0.13
expected output
42800
13
It can be more than two decimal places .
any easy way to do that
Maybe not the best solution, but at least it is simple :)
Store your double initialAmount in a int - which will truncate the decimal part. Then you have the number before the decimal point. Then subtract that from the initial value and you'll be left with just the part after the decimal point. Multiply that by however many powers of 10 that you deem significant and then again store that in a int and you'll have your two values.
As suggested in the comments, it could help to convert the number into a string. Here's one possibility:
#include <sstream>
#include <iostream>
#include <vector>
#include <string>
int main() {
double d = 42800.13;
std::vector<std::string> numbers_s;
std::string s;
std::istringstream numberAsString(std::to_string( d ));
while (getline(numberAsString, s, '.')) {
numbers_s.push_back(s);
}
std::cout << "whole number:" << numbers_s[0] << std::endl;
//[ remove trailing zeros from fractional part:
numbers_s[1].erase(numbers_s[1].find_last_not_of('0') + 1, std::string::npos);
//]
std::cout << "fractional part:" << std::stoi(numbers_s[1]) << std::endl;
}
Error checks should be added to capture cases when the number does not contain a decimal point.
This is a snippet of my code. I need some help in removing the error shown below.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
char doubleStr[] = "5.2";
double d = 0.0;
std::stringstream stream (doubleStr);
stream >> d;
std::cout << d << std::endl;
std::cout << (d <= 5.2);
return 0;
}
This gives output:
5.200000000002
0
How to remove this precision error? Can I use std::setprecision() to solve the issue?
numeric_limits<double>::digits10 can be used to find the number of digits that are uniquely representable by a double.
I see you've tagged your question with Visual Studio. You can test this code on http://webcompiler.cloudapp.net/ to get Visual Studio's number of uniquely representable digits for a double:
#include <iostream>
#include <limits>
int main() { std::cout << std::numeric_limits<double>::digits10 << std::endl; }
This code will not output 2 it will output:
15
Which means that any double up to 14 decimal places will survive the round trip through a stringstream and still be equal to itself.
The above means that there is something you are not including in your example that is causing the round trip failure, or you are using non-standard source files that are not IEEE compliant. (For example I can do a live example on gcc that gives a contrary output to yours, and running the same code on Visual Studio disagrees with your output.)
Either way, for any uniquely representable double (like 5.2), you can ensure that round trip success through a stringstream by setting the precision. Precision is a sticky modifier, so you'll only need to set it once after stream construction. In your example you use stringstream stream so before you work with stream you'd need to set this modifier:
stream.precision(numeric_limits<double>::digits10 - 1);
You can use it like this in order to show the double number in the right way:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <limits>
int main()
{
char doubleStr[] = "5.2";
double d = 0.0;
std::stringstream stream (doubleStr);
stream >> d;
std::cout << std::setprecision(2)<< d << std::endl;
return 0;
}
http://cpp.sh/9lub
about comparing float point numbers you may look at this What is the most effective way for float and double comparison?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream basketFile;
basketFile.open("basket.txt");
double price;
while (!basketFile.eof()) {
basketFile >> price;
cout << price << endl;
}
}
basket.txt
27.9933
18.992
9.754
11.2543
Anyway I can make the numbers appear to only two significant digits? Also, if I want to round a number up how would I do that? For example, if I had the numbers 6.66 and 4.33 I want the 6.66->6.70 and 4.33->4.30. Any help?
Try setprecision.
For rounding a number, see round.
Also, if you decide to round to 0.1 precision, I believe you can just append the zero 0 after the rounded result.
#include <iostream>
#include <iomanip>
using namespace std;
void p(double x) {
cout << fixed << setprecision(1) << x << 0 << endl;
}
int main() {
p(27.9933);
p(18.992);
p(9.754);
p(11.2543);
p(6.66);
p(4.33);
return 0;
}
The code above outputs:
28.00
19.00
9.80
11.30
6.70
4.30
Hope this is what you want.