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.
Related
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);
}
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 4 years ago.
I was calculating the volume of a sphere and after tons of research I found that I cannot use:
float sphereRadius = 2.33;
float volSphere = 0;
volSphere = (4/3) * (M_PI) * std::pow(sphereRadius, 3);
But must add the 3.0 instead to get the right answer.
volSphere = (4/3.0) * (M_PI) * std::pow(sphereRadius, 3);
Why must a decimal be added to get the correct calculation?
(4/3) is one integer divided by another integer, which results in another integer. An integer can't be 1.33 or anything like that, so it gets truncated to 1. With the decimal, you're telling it to be a double instead, and dividing an integer by a double results in a double, which supports fractions.
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.
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
I did some search on the net. However, there is no solution for those case below.
if the value is less than or equal to 5, drop it and all the digits to the right of it.
if the value is greater than 5, increase by 1 the number to be rounded.
123.4561235 round to 123.456123.
123.4561236 round to 123.456124.
Is there any way to do so?
OK here you go.
Disclaimer: I haven't tested this thoroughly. Don't use this in the production code, and especially for money (you shouldn't use floating point for money at all).
double specialRound(double x, int precision) {
return std::round((static_cast<long long>(x * std::pow(10.0, precision + 1)) - 1) / 10.0 + 0.05)
/ std::pow(10.0, precision);
}
Demo
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.