Unsolved Problem with real values
-
Hello, I divide two integer numbers like this:
qreal scaleFactorX = ui->workSpace->width() / width;
but in my application it equals 0 (zero!). qDebug prints 0 and debbuger shows it is 0. How to repair it, whats wrong with this code?
-
@clostridium_difficile said in Problem with real values:
but in my application it equals 0
Everything else would surprise me - C basics. Doing arithmetics with integer values will result in an integer value.
-
@Christian-Ehrlicher said in Problem with real values:
@clostridium_difficile said in Problem with real values:
but in my application it equals 0
Everything else would surprise me - C basics. Doing arithmetics with integer values will result in an integer value.
Well, dividing two integer values and assgning result to qreal, should be casted to double, shouldn't it?
-
@clostridium_difficile said in Problem with real values:
should be casted to double, shouldn't it?
For sure, the result of the aritmetic will be casted to the left hand side type.
-
Hi
Beware of dragonsqreal scaleFactorX = 10 / 3; == 3
qreal scaleFactorX = 10 / 3.0 == 3.33333;
qreal scaleFactorX = static_cast<float>(10) / 3; == 3.33333;
the reason is that 2 ints will use the integer division version of operator / so
even when you assign it to a double the precision is lost. -
@clostridium_difficile said in Problem with real values:
qreal scaleFactorX = ui->workSpace->width() / width;
qreal scaleFactorX = ((qreal)ui->workSpace->width()) / width;
-
@JonB
uuh, c-style cast 😨still an upvote as you're technically correct, the best kind of correct.
-
-
@JonB said in Problem with real values:
is that right?
As we are long done with this question anyway, you could use constructor-style cast instead:
qreal scaleFactorX = ui->workSpace->width() / qreal(width);
(moved it to the end for readability)
-
@kshegunov
Indeed you could. All this fuss effectively overqreal(width)
versus(qreal)width
;-) -
@JonB said in Problem with real values:
All this fuss
There's no fuss. They're exactly the same - a C-style cast.
-
@kshegunov said in Problem with real values:
They're exactly the same - a C-style cast.
Oh dear! I thought we went through this a while ago. You don't really mean they are exactly the same, do you? Only because it's
qreal
here? If it wereClassName(variable)
it would be an instance creation instead of a cast, wouldn't it? -
@JonB said in Problem with real values:
I thought we went through this a while ago.
We may've, I don't remember ... and I'm bored so I have nothing better to do than to argue uselessly ... :)
If it were ClassName(variable) it would be an instance creation instead of a cast, wouldn't it?
This is where C++ really shines ... in its stupidity. It's going to be trying a cast if it can't match a suitable constructor, but if there is a matching constructor you're going to get an anonymous object ... fun, ain't it? And compilers differ in their interpretation ... I've had this:
QMutex x; { QMutexLocker(&x); }
generate "can't cast to QMutexLocker" error with MSVC, but fly seamlessly through gcc ... that language, go figure ...
-
@kshegunov said in Problem with real values:
This is where C++ really shines ... in its stupidity. It's going to be trying a cast if it can't match a suitable constructor, but if there is a matching constructor you're going to get an anonymous object ... fun, ain't it?
Ah ha! That at least explains it, a bit :) Phew! I thought you were going to shoot me down!
What I had in mind is: when I see
(Something)something
I just know it's a cast (yes,static_cast<>
is better). But when I seeSomething(something)
am I looking at a function call, an initializer, a cast, even (yikes!) a macro, ...? So personally I wouldn't use that one for a cast, it has enough other uses already!