Calc BASIC function like oCell.getValue to handle long integers?

In the example below, getValue works with smaller long integers, but if a legitimate long integer gets to larger values, the program blows with a “Inadmissible value or data type Overflow.” error. Is there a function similar to getValue that works with any valid long integer? getLong seems to be associated with SQL.
= = =

dim x as long

oCell = oSheet.getCellByPosition(0,j)
x = oCell.getValue()

Please don’t insert an ordinary text as a hyperlink.
= = =

Please format code (Basic) using the tool Preformatted text (Ctrl+E). (The tool icon with angle brackets.)

Assuming your consent I fixed it for you.

In LibreOffice Basic there is no integer type using more than 32 bit.
Calc uses exclusively the non-integer type Double as described in IEEE 754. It has 1 bit for the sign, 11 for 2-based magnitude(exponent) and 52 bits for significant dyadic digits.

As I just found researching for this question, LibO Basic (not Calc cells!) has a strange type Currency which is described as sign + up to 15 decimal digits for the integer part + 4 decimal digits for the fractional part.
Using Dim and conversion cautiously, you can (mis-) use this type for the representation of very long integers and possibly/limited for calculations with such numbers.

It almost seems as if this type of number was created with wise foresight to describe the “fortunes” of the really big crooks.

Just a note.

Despite we do not expose it, Basic has 64-bit types. E.g., it is possible to create such a value like

oService = CreateUNOService("com.sun.star.script.Converter")
Dim x As Variant ' important! '
x = oService.convertToSimpleType(oCell.getValue(), com.sun.star.uno.TypeClass.HYPER)

The value will be successfully converted to a signed 64-bit value. However, since the support for these types is poor, such a variable has to be handled with care. E.g., without the Dim x As Variant, the code would create a Long - and would fail for large integers, because by default, it would try to convert to the maximal “standard” type. All kinds of strange things can be expected with such variables. (Just for fun.)

2 Likes

Yes. Of course, your posts are much better founded on knowledge than mine, but I also post often just for fun.

BTW

Are negatives represented by “two’s complements”? (I don’t know the correct English term.)

And, of course, we can use Python, which supports integers that are not limited in size. :slight_smile:
pyfunc.ods (15.4 KB)

2 Likes

Yes. There is also an unsigned counterpart (the type class for it is com.sun.star.uno.TypeClass.UNSIGNED_HYPER)

Wow, thanks for all the help! I will add Python to my programming language options.

I found the largest number that doesn’t blow up my example above is 2147483647 (prime).

= 2^31 - 1, the largest value a 32-bit integer can take.

What do you mean by “prime” in this context?

No factors other than 1 or itself.

=S=

Yes. As I posted it, my question was obsolete.
Assume I meant something like “How do you know, and for what reason is this relevant in the context?”

Apologies for “(prime)”! Relevant to me, but probably irrelevant to anyone else here. My application tests numbers to find whether they are prime. First I found the first 10,000 primes, then used that list to test larger numbers.

I have to apologize for the bad wording. No apologies needed on your behalf.
= = =
I checked your statement with wxMaxima.
Just want to state that not all numbers like 2^(2n-1)-1 are prime.

1 Like

Mersenne prime

getValue doesn’t return a (long) integer, but a double. The error happens not in the function (your assumption that it cannot handle some of the numbers in the cell is wrong), but when you assign the value to your variable. Declare your variable as double, and round if you only need whole numbers.

1 Like