[Qt-qml] Assingments of reals to ints round rather than truncate - why?
Artem Marchenko
artem.marchenko at gmail.com
Sun Nov 6 20:59:08 CET 2011
Hi Alistair and all
If my memory doesn't let me down all the numbers in JavaScript are always floats (that you can round/floor/ceil using Math). ints are something QML specific. The fact that QML uses Math.round() when assigning to int properties sounds very logical to me - as it is the closest to the source value.
> Easy to fix by subtracting 0.5 but more to the point
I would still go for Math.floor() if flooring down is what you want
> Interestingly “property int foo=69.6” fails but “property int foo=696/10.0” doesn’t.
This is interesting :)
I'd love to hear from somebody who knows. My guess is that "foo: 69.6" is evaluated by QML parser that knows about types, but "696/10" is a function call that is executed by JavaScript engine once QML has prepared the object tree.
The following code, for example works perfectly demonstrating that you can assign exactly 79.6 if it happens at run-time (80 is actually assigned):
import QtQuick 1.0
Rectangle {
// That would have failed with "Invalid property assignment: int expected "
//property int foo: 69.6
property int foo: 696/100
width: 100
height: 62
Text {
anchors.fill: parent
text: foo
}
Component.onCompleted: {
foo = 79.6
}
}
Best regards,
Artem.
On Nov 5, 2011, at 6:54 AM, <alistair.adams at nokia.com> <alistair.adams at nokia.com> wrote:
> property int foo=696/10 evaluates to 70, not 69 as I expected.
>
> QML appears to do a round rather than a truncate when assigning a real expression to an int. Easy to fix by subtracting 0.5 but more to the point, is this intended behavior? I could not find anything in the documentation that describes how it should behave.
>
> Interestingly “property int foo=69.6” fails but “property int foo=696/10.0” doesn’t.
>
> -Alistair
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20111106/c2fb6e84/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-1.png
Type: image/png
Size: 15435 bytes
Desc: not available
Url : http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20111106/c2fb6e84/attachment-0001.png
More information about the Qt-qml
mailing list