Bug in conversion from double to integer (only some value)
-
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.7is actually1.6999999999999and* 10gives16.999999999would be enough foruint_t()to produce16rather 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.5likeuint8_t(s17.toDouble() * 10 + 0.5);? I always add 0.5 (or 0.4999 depending on what you want to happen tox.5) beforeinting 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.5rather than* 10+0.5as 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 adoublenot anint. Then if the conversion of that fromdoubletointstill 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.5rather than* 10+0.5as 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 adoublenot anint. Then if the conversion of that fromdoubletointstill 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
lroundandllround(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
lroundandllround(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.