c++11 double precision [duplicate] - c++

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 months ago.
Doing the following calculation:
double result = 58/10;
Will result: 5.7999999999999998
I know c++ will round it on further calculations or printing, but I need to compare output with where rounding doesn't occur so I can't have rounding happen because that may just lead to differences in other places in the output.
Is there any way to improve the precision of such calculations and having 58/10 result 5.8?

5.8 cannot be exactly represented in floating point. If you are working with 2 digits precision after the decimal point, for example if you need to store monetary values, store it as "580" (e.g., 580 cents) and do your own formatting when printing.
printf("%d.%02d", n / 100, n % 100);
Alternatively store the decimal as a fraction:
struct Fraction {
int numerator;
int denominator;
};
int main() {
Fraction f(29, 5);
}

Related

Rounding Floating Point Number To two Decimal Places in C++ [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
After Rounding to two decimal places, i alwayse get the same value
float TestT =round(t*100)/100;
i would like to round it and fix it to two decimal places: 0.009999 -> 0.01
You can do this in 2 steps:-
Step1:- transform your float value to string and use the inbuilt QString feature to perform the rounding for you
Eg:-
#QString::number( 99.48, 'f', 2);
In your case:- QString TestS =QString::number( t, 'f', 2);
Step2:- transform your rounded string value to float
Eg:-
in your case :- float TestT= TestS.toFloat()
More info about this here is some links:-
QString":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#number-2 may help you.

Why is the difference between 2 double values wrongly calculated? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I need to calculate the difference value between 2 string numbers by only taking only the first precision. I have to convert to double first then calculate the difference as below
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main()
{
string v1 = "1568678435.244555";
string v2 = "1568678435.300111";
double s1 = atof(v1.substr(0,12).c_str()); // take upto first precision and convert to double
double s2 = atof(v2.substr(0,12).c_str()); // take upto first precision and convert to double
std::cout<<s1<<" "<<s2<<" "<<s2-s1<<endl;
if (s2-s1 >= 0.1)
cout<<"bigger";
else
cout<<"smaller";
return 0;
}
I expect the calculation would be 1568678435.3 - 1568678435.2 = 0.1 . But this program returns this value :
1.56868e+09 1.56868e+09 0.0999999
smaller
Why is that and how to get the value that I want properly?
Floating point format has limited precision. Not all values are representable. For example, the number 1568678435.2 is not representable (in IEEE-754 binary64 format). The closest representable value is:
1568678435.2000000476837158203125
1568678435.3 is also not a representable value. The closest reprecentable value is:
1568678435.2999999523162841796875
Given that the floating point values that you start with are not precise, it should be hardly surprising that the result of the calculation is also not precise. The floating point result of subtracting these numbers is:
0.099999904632568359375
Which very close to 0.1, but not quite. The error of the calculation was:
0.000000095367431640625
Also note that 0.1 is itself not a representable number, so there is no way to get that as the result of a floating point operation no matter what your inputs are.
how to get the value that I want properly?
To print the value 0.1, simply round the output to a sufficiently coarse precision:
std::cout << std::fixed << std::setprecision(1) << s2-s1;
This works as long as the error of the calculation doesn't exceed half of the desired precision.
If you don't want to deal with any accuracy error in your calculation, then you mustn't use floating point numbers.
You should round the difference between the values.
if (round((s2-s1) * 10) >= 1)
cout<<"bigger";
else
cout<<"smaller";

Floor vs int cast difference [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
I have a weird problem. Here's part of my code:
int temp=1100;
int foo=floor(0.03*temp);
int foo1=0.03*temp;
if(foo-foo1){
cout<<foo<<endl;
cout<<foo1<<endl;
}
If 3% of temp = integer then foo differs from foo1 by 1.
For example: 1100*0.03=33. foo=33 foo1=32.
In addition if i write it like that :
int foo=floor(0.03*1100);
int foo1=0.03*1100;
There is no such problem.
Why?
Floating point numbers have trouble rendering decimal places. It is more of an approximation.
2**-6 + 2**-7 + 2**-8 + 2**-9 + 2**-11 + 2**-13 + 2**-14 + 2**-15 + 2**-20 + 2**-22 + 2**-26
= 0.0299999863
Using binary, I drove the accuracy to 26 binary. I got close to 0.03 but not quite. In my example I chose to be under 0.03 but I could have gone a little above (i.e. 0.03000001. I don't think it is possible to represent 0.03 perfectly in floating point notation.
Multiplying 0.03 by any number produces yet another approximation. Casting to type int will cut out everything after the decimal place. I assume the implementation of floor is more elegant. Your compiler probably choose a floating point value of 32.99999 so and int would make 32.

Issue while rounding decimal fileds in C++ using setprecision [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Does setprecision in c++ round? If so why am I seeing this?
Here is the function I'm using
char* round(double value, int decimal_places)
{
decimal_places = decimal_places+1;
std::stringstream s2;
s2 << std::setprecision(decimal_places) << std::setiosflags(std::ios_base::fixed) << value;
std::string st = s2.str();
return st;
}
My input value is 0.89425 and the number of decimal palaces is 4
My output is 0.8942 but i want 0.8943 i.e., if next digit after my required decimal places is >= 5 then the output should be rounded to the next value.
0.89425 is not representable exactly in binary floating point; the nearest exactly representable value is 0.894249999999999989341858963598497211933135986328125, which is correctly rounded to decimal 0.8942.
If you want to see decimal rounding behaviour, use fixed-point or decimal floating point.

Sum Float number c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Floating point error in representation?
I have problem with this code
int cent;
int dollar ;
float cnt2dlr;
//convert cnt to doloar ;
cnt2dlr=(cnt)/(100);
The problem is when cnt = 175, cnt2dlr = 0.17,444444 and not 0.17,5
Any help?
Floating point numbers are often inexact. There is nothing you can do about it.
Your code is a good illustration of why you should not use floating point numbers for calculations involving money. Just because currency values have a decimal point does not make money a floating point quantity. Floating points should be used for quantities which vary infinitesimally, like temperature or speed, not for quantities which vary in chunks, like money.
Floating point numbers are not exact representations. They are approximations, so you cannot guarantee much precision. Read What Every Computer Scientist Should Know About Floating-Point Arithmetic
To increase the precision of the numbers, consider using a 64-bit double instead of a 32-bit float.
I am a bit puzzled. If you mean (cent) instead of (cnt), then
cnt2dlr=(cent)/(1000);
(note the e in cent) is an int / int division, and 175 / 1000 should return int 0.
Do you get the same result if you do, eg
cnt2dlr=(cent)/(1000.0);
note the decimal point.