[Solved+Issue] Currency variables in OOo Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
lorenzo2
Posts: 4
Joined: Tue Jul 15, 2008 8:12 pm

[Solved+Issue] Currency variables in OOo Basic

Post by lorenzo2 »

Posted originally on the Calc board, but it's really an OOo Basic problem.

Here's a short little macro. It reads the contents of cell A1 of Sheet1 into an integer
variable called "buy", and the contents of cell B1 into a currency variable called "cost".
The product of those two numbers is a currency variable called "total", which is then
written into cell C1.

Code: Select all

Sub Main

   Dim oSheet as object, oCell as object
   Dim cost as currency, total as currency
   Dim buy as integer
   
   oSheet=ThisComponent.Sheets.getByName("Sheet1")
   
   oCell=oSheet.getCellByPosition(0,0) 'A1  
   buy = oCell.getValue				   
   
   oCell=oSheet.getCellByPosition(1,0) 'B1
   cost = oCell.getValue
     
   total = buy*cost
   oCell=oSheet.getCellByPosition(2,0) 'C1
   oCell.setValue(total)
 
End Sub
Now, if you set A1 to 5 and B1 to 40000 and execute the macro, cell C1 contains 200000,
as expected.

If you now change B1 to 50000 and execute the macro again, C1 doesn't contain the expected
value of 250000 - on my computer, it's now 38,252,449,584,978.1. (It works fine if I change
cost and total from type currency to type long, but that's treating the symptoms, not addressing
the problem.

Looks to me like some kind of overflow. Where/how/to whom do I report this?

Lorenzo
Last edited by lorenzo2 on Wed Jul 16, 2008 10:40 pm, edited 1 time in total.
OOo 2.4.X on Ms Windows XP
User avatar
squenson
Volunteer
Posts: 1885
Joined: Wed Jan 30, 2008 9:21 pm
Location: Lausanne, Switzerland

Re: Currency variables in OOo Basic

Post by squenson »

This is definitively a bug!

After some tests, I conclude that the largest number you can obtain by the multiplication is (2^31 - 1)/10000 (the 32nd bit is for the + or - sign), which is equal to 214,748.3647. When you multiply 5 * 50,000, you have an overflow.

What certainly happens is that internally, a 32-bit integer is not properly "casted" (converted) into a currency type. I have updated a similar bug with your example and your reference, you can have a look here: http://qa.openoffice.org/issues/show_bug.cgi?id=31001

As a work around, I suggest that you only do math with double, and round the result to two decimals and the end of the calculation.
LibreOffice 4.2.3.3. on Ubuntu 14.04
lorenzo2
Posts: 4
Joined: Tue Jul 15, 2008 8:12 pm

Re: Currency variables in OOo Basic

Post by lorenzo2 »

I have updated a similar bug with your example and your reference

Thank you. I'll mark this thread as solved, even though it isn't exactly. I'll look
forward to it being fixed in some future version of OO, and in the meantime will
work with doubles.
OOo 2.4.X on Ms Windows XP
Post Reply