Bug in conversion from double to integer (only some value)
-
This is working fine for me:
QString s16 = "1.6"; QString s17 = "1.7"; QString s18 = "1.8"; auto i16 = uint8_t(s16.toDouble() * 10); auto i17 = uint8_t(s17.toDouble() * 10); auto i18 = uint8_t(s18.toDouble() * 10); qDebug() << int(i16) << int(i17) << int(i18);
Please provide a minimal, compilable example of your problem - no QLineEdit needed as I showed you.
Also we don't care what QtCreator version you are using but the used Qt version. -
I have made a program as you said and the result is 16,16,18.
Sorry the version is Qt 5.15.2 MinGW_32_bit
I think is a bug from this version Qt 5.15.2 MinGW_32_bitA colleague have tested with Qt 6.6.0 MinGW_32_bit has the right value.
16,17,18. -
I dobut there is a bug in such a simple thing.
Please provide the output of the three toDouble() without multiplication. -
The double value are right.
QString s16 = "1.6"; QString s17 = "1.7"; QString s18 = "1.8"; auto i16 = uint8_t(s16.toDouble() * 10); auto i17 = uint8_t(s17.toDouble() * 10); auto i18 = uint8_t(s18.toDouble() * 10); qDebug() << s16.toDouble() << s17.toDouble() << s18.toDouble(); qDebug() << int(i16) << int(i17) << int(i18);
10:27:17: Starting D:\TrabajoJose\DoubleConversion\build-DoubleConversion-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\DoubleConversion.exe ...
1.6 1.7 1.8
16 16 18 -
So no Qt problem here.
-
So no Qt problem here.
@Christian-Ehrlicher , @JAHT
uin8_t(double)
does floor not any kind of round, right? So a tiny difference where what shows as1.7
is actually1.6999999999999
and* 10
gives16.999999999
would be enough foruint_t()
to produce16
rather than17
? Still not sure why 32-bit worse than 64-bit, are doubles not 64-bit in both (try printingsizeof(double)
in each)? I think this code should not directly useuint_t(double)
, some sort of mathround()
first? Or add0.5
likeuint8_t(s17.toDouble() * 10 + 0.5);
? I always add 0.5 (or 0.4999 depending on what you want to happen tox.5
) beforeint
ing adouble
. -
Again: this has nothing to do with Qt but maybe with the compiler you're using (mingw version whatever). And @JonB is correct - you should round your number before otherwise it's truncated.
-
Yes, it seems that that is the issue, I was wrong when saying the version used by my colleague that it worked, it is Qt 6.6.0 MinGW_64_bit
-
Yes with:
QString s16 = "1.6"; QString s17 = "1.7"; QString s18 = "1.8"; auto i16 = uint8_t(s16.toDouble() * 10+0.5); auto i17 = uint8_t(s17.toDouble() * 10+0.5); auto i18 = uint8_t(s18.toDouble() * 10+0.5); qDebug() << s16.toDouble() << s17.toDouble() << s18.toDouble(); qDebug() << int(i16) << int(i17) << int(i18);
The result is as expected with MingW_32
11:22:25: Starting D:\TrabajoJose\DoubleConversion\build-DoubleConversion-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\DoubleConversion.exe ...
1.6 1.7 1.8
16 17 18Thank you very much @Christian-Ehrlicher @JonB
-
Yes with:
QString s16 = "1.6"; QString s17 = "1.7"; QString s18 = "1.8"; auto i16 = uint8_t(s16.toDouble() * 10+0.5); auto i17 = uint8_t(s17.toDouble() * 10+0.5); auto i18 = uint8_t(s18.toDouble() * 10+0.5); qDebug() << s16.toDouble() << s17.toDouble() << s18.toDouble(); qDebug() << int(i16) << int(i17) << int(i18);
The result is as expected with MingW_32
11:22:25: Starting D:\TrabajoJose\DoubleConversion\build-DoubleConversion-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\DoubleConversion.exe ...
1.6 1.7 1.8
16 17 18Thank you very much @Christian-Ehrlicher @JonB
@JAHT said in Bug in conversion from double to integer (only some value):
+0.5
use a proper round function or you will get wrong results with negative numbers.
-
Great that is working now. But can you please write
s16.toDouble() * 10 + 0.5
rather than* 10+0.5
as that just encourages mis-reading :)@Christian-Ehrlicher said in Bug in conversion from double to integer (only some value):
use a proper round function or you will get wrong results with negative numbers.
Never thought about that as I don't have negative numbers! What would you suggest? Because I am worried that
round()
from the math library delivers its result as adouble
not anint
. Then if the conversion of that fromdouble
toint
still has some tiny floating point precision issue, as your tests claim to show, I am wondering if that still might produce 16 where it ought be 17...? -
Great that is working now. But can you please write
s16.toDouble() * 10 + 0.5
rather than* 10+0.5
as that just encourages mis-reading :)@Christian-Ehrlicher said in Bug in conversion from double to integer (only some value):
use a proper round function or you will get wrong results with negative numbers.
Never thought about that as I don't have negative numbers! What would you suggest? Because I am worried that
round()
from the math library delivers its result as adouble
not anint
. Then if the conversion of that fromdouble
toint
still has some tiny floating point precision issue, as your tests claim to show, I am wondering if that still might produce 16 where it ought be 17...?@JonB said in Bug in conversion from double to integer (only some value):
Because I am worried that round() from the math library delivers its result as a double not an int.
Well, there are also
lround
andllround
(https://en.cppreference.com/w/cpp/numeric/math/round). Hopefully, these two functions don't provide any problems with corner cases. -
@JonB said in Bug in conversion from double to integer (only some value):
Because I am worried that round() from the math library delivers its result as a double not an int.
Well, there are also
lround
andllround
(https://en.cppreference.com/w/cpp/numeric/math/round). Hopefully, these two functions don't provide any problems with corner cases.@SimonSchroeder I didn't know about
std::lround()
, that should be acceptable if preferred to adding 0.5 and then takingint
.