Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML highest number (float/integer) possible

Tags:

math

numbers

qt

qml

In JavaScript the highest integer possible is defined via

Number.MAX_SAFE_INTEGER.

and also in C++ can be obtained with the std:

std::numeric_limits<int>::max()

Is there such a constant in QML for ints or doubles?

like image 942
lackadaisical Avatar asked Dec 29 '16 11:12

lackadaisical


2 Answers

As originally suspected, the 2000000000 number listed in the documentation is incorrect. Also, IMO this is an important value that shouldn't really be subject to such careless approximations. "Around" should only be used when the actual value is unknown for certain or not crucial.

A simple test verifies that the largest possible value for an int property in QML is 2147483647, or as expected 2^31 - 1.

Note that this is different from Number.MAX_SAFE_INTEGER which is a JS thing, and that value is 2^53 - 1 - substantially higher than what int will give you. Number is a 64 bit real data type, and it supports integers by using the 53 fraction bits of the number, the 11 exponent bits are left unused.

Edit: In 5.15 and possibly earlier, it is possible to use the full range of an unsigned 32 bit int, doubling the effective range over qml's int type, but you have to use var as the property type, it may even be possible to use uint64 with values within the ^53 range.

like image 113
dtech Avatar answered Nov 16 '22 09:11

dtech


Another alternative is to use IntValidator. By default, top and bottom property contains the maximum and minimum Qt int value.

readonly property IntValdiator intValdiator: IntValidator {}
readonly property int MAX_VALUE: intValidator.top
readonly property int MIN_VALUE: intValidator.bottom
like image 44
Broatian Avatar answered Nov 16 '22 09:11

Broatian