Bug 92016 - Return value from Function
Summary: Return value from Function
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
5.0.0.0.beta1
Hardware: Other All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-11 22:46 UTC by Eduardo Moreno
Modified: 2018-07-21 23:16 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments
test file (16.76 KB, application/x-zip)
2015-06-13 12:52 UTC, raal
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eduardo Moreno 2015-06-11 22:46:43 UTC
Copy this code in IDE Editor

Option Explicit

Function Pulgada() As Single
	Pulgada = 25.4
End Function

If use this function in a WorkSheet return the value 25.3999996185.

If change the word Single for Double, return the value 25.4.

I know the variable type DOUBLE is more precise than the type SINGLE, but this only using a decimal position.
Comment 1 raal 2015-06-13 12:52:24 UTC
Created attachment 116498 [details]
test file

I can confirm with Version: 5.1.0.0.alpha1+
Build ID: 782c116b5ac39aef27c3a00f7fc145c9f755e3a3
TinderBox: Linux-rpm_deb-x86_64@46-TDF, Branch:master, Time: 2015-06-10_00:04:59
Comment 2 QA Administrators 2016-09-20 10:00:37 UTC Comment hidden (obsolete)
Comment 3 Eduardo Moreno 2016-09-20 17:17:10 UTC
Hi, The problem continue in LibreOffice for Windows x64 5.2.1.2.

Thank's.
Comment 4 Gerhard Weydt 2017-07-05 14:30:42 UTC
This is not a bug, but simply due to the fact that a variable of type single has less precision than a number in Calc, in standard format, if this number has only a small integer part. And it’s not a behaviour restricted to LibreOffice Basic, but a general one.
Numbers of type single or double are stored in a binary format; you will find the details in https://en.wikipedia.org/wiki/Single-precision_floating-point_format.
By this transformation to binary format and back again to decimal format value is changed to a small extent, depending on the precision used. You can get an idea of the difference by executing the small program:

sub test
dim a as single, ai as long, af as double
a = 25.4
ai = Int(a)	'integer part
af = a – ai	'fraction part
msgbox af
end sub

It yields 0,399999618530273 if a is of type single, and 0,399999999999999, if a is of type double I used a double variable for the result, to show as much digits.).
Now Calc uses 13 digits after the decimal point in our case, where there only two digits before it. I can not swear that Calc does a rounding when transforming the return value to fit its standard format, but it does so, if you change the number of decimal digits, and this fits the results we obtain.
If you round the result of our test programm to 13 decimal digits in the case of the single type, you get 0,399996185303, exactly the result I get using your function. The number you quote is slightly different, I don’t know why, but this is not important for the argument. If, on  the other hand, you round the second result, this yields 0.4.

So the difference between using a single or double variable is explainable, and there will be no change in future releases. If you wish to have an exact value even when using a high precision, as the standard format does in your case, you have to use the double type. If you want to use only, say, two decimal digits, then the single type is sufficient.

If you accept this explanation, you should change the status to resolved.
Comment 5 QA Administrators 2018-07-06 02:46:07 UTC Comment hidden (obsolete)
Comment 6 Gerhard Weydt 2018-07-21 23:16:12 UTC
The original reporter of the bug didn't respond to my explanation and the mass ping one year later. So I set the status to resolved/notabug.