Hello my friends,
I have been using for a long time a basic function which allows to calculate the number of digits in an integer.
Yesterday I came across a case that seemed incorrect to me so I did a test and indeed it seems that there is an error calling the FLOOR function.
Dim Result As Integer
Dim Division As Double
Dim a As Integer
a = 1000
Division = Log(a)/Log(10)
Result = Floor( Log(a)/Log(10) ) + 1
t_Out.WriteLine "a : " + Str(a)
t_Out.WriteLine "Log(a) : " + Format( Log(a), "#.#########" )
t_Out.WriteLine "Log(10) : " + Format( Log(10), "#.#########" )
t_Out.WriteLine "Division : " + Format( Division, "#.#########" )
t_Out.WriteLine "Floor : " + Format( Floor(Division), "#.#########" )
t_Out.WriteLine "nb: " + Str( Result )
t_Out.WriteLine "Test 3.005: " + Str( Floor(3.005) )
t_Out.WriteLine "Test 3.: " + Str( Floor(3.) )
The results :
a : 1000
Log(a) : 6,907755279
Log(10) : 2,302585093
Division : 3,
Floor : 2, Here is the error.
nb: 3
Test 3.005: 3
Test 3.: 3
Am I making a mistake somewhere or is there really a rounding error?
How can we get around this problem?
Thank you.
If I understand correctly it’s rather annoying because it doesn’t happen for example with Excel or another programming language that I use.
I had to convert the number to text and calculate the length of the string, which is less elegant.
My guess is that this is related to using floating point arithmetic. I imagine Excel doesn’t use it and maybe the other programming language doesn’t.
If the accuracy of the calculations is important then you probably need to use some kind of fixed precision library / plugin such as the ones available from Bob Delaney and MBS.