Qt: rounding 12.625 by 2 returns 12.62

I can't figure out why the ff code is returning 12.62 instead of 12.63

 QDebug << QString::number(12.625, 'f', 2);

      

and also what will be the solution for this? Currently my solution is

QDebug << QString::number(12.625 + 0.0001, 'f', 2);

      

and it will return 12.63

.

btw my OS is ubuntu 11.04

+3


source to share


2 answers


GCC uses "Round to Even" or "Banker Rounding" as the default mode, while Visual Studio uses "Round away from Zero"

From the GCC docs : Rounding

This is the default mode. It should be used if needed for one of the others. In this mode, results are rounded to the nearest representable value. If the result is halfway between two representable values, an even representable is selected. Even here means that the least significant bit is zero. This rounding mode prevents statistical bias and guarantees numerical stability: rounding errors in a long run will be less than half of FLT_EPSILON.



To change the rounding mode, use fesetround(FE_UPWARD)

to round up. If you are not using GCC, your compiler will be similar.

Rick Regan provides a list of software environments and their default rounding behavior. Inconsistent rounding

0


source


There is a bug report for this:



https://bugreports.qt-project.org/browse/QTBUG-38171

0


source







All Articles