Variable error?

Pantelis

Member
Licensed User
Longtime User
Hi

I am trying to just set a numeric value to a variable and then read the variable but i get strange results

B4X:
Dim A As Float
Dim B As Float
   
A=3.44
Msgbox(A,"A") 'gives 3.440000057220459
   
B = Round2(A,2) 
Msgbox(B,"B")   'gives again 3.440000057220459

Why i get that results from Variable A? I don't do any calculation there, i just set the value to 3.44. Why i get 3.440000057220459?

Then i Round the value of variable A with 2 fractional digits only and set it to variable B. How is it possible to get that result again? Round2 function doesn't work?
 
Last edited:

Pantelis

Member
Licensed User
Longtime User
Sonny,

Thank you for your help and for your time.
Yes, i want accuracy in 2 decimal places.

I tryied Round2(Value, 2) but again the same affect. I used the same value for a and b variables as before.

B4X:
Dim a As Double
Dim b As Double

 a = 30.005
 For i = 0 To 29
     a = a + 0.1
 Next 
 
 Msgbox (a,"a")            
 
 b = 36.005
 For i = 0 To 29
     b = b - 0.1
 Next
 
 Msgbox (b,"b")       

Msgbox(Round2(a ,2),"Check A")    
Msgbox(Round2(b ,2),"Check B")    
 
If Round2(a ,2) = Round2(b ,2) Then
    Msgbox ("equal","Result")
Else
    Msgbox ("Not equal","Result")
End If
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You should not round the variables to two decimals but to much more.
This code works:
B4X:
Dim a As Double
Dim b As Double

a = 30.005
For i = 0 To 29
    a = a + 0.1
Next 

b = 36.005
For i = 0 To 29
    b = b - 0.1
Next

If Round2(a ,10) = Round2(b ,10) Then
    Msgbox ("Equal","Result")
Else
    Msgbox ("Not equal","Result")
End If
In your case you get
a = 33.00500000000004 and
b = 33.00499999999996
When you Round to two decimals you get
a = 33.01
b = 33

Rounding to ten decimals you get
a = 33.005
b = 33.005

Then if you want to display the results with 2 decimals you should use NumberFormat or NumberFormat2.

Best regards.
 
Upvote 0

Pantelis

Member
Licensed User
Longtime User
Hi guys.
Again thank you for your time and help.
Sorry for the delay. I wanted to test the various equality testing methods, so i made a little application based on my original code that searches for inequalities changing the input value for variable "a" automatically.

My findings are :
While the value of variable "a" becomes bigger the difference between the results after the additions and subtractions becomes bigger to.
For values below 1 the difference is about 0.000002622.
For value of 100 the difference is 0.000045
For 1000 is 0.0073
For 10000 is 0.011
For 1000000 is 0.75! Huge difference.

If i declare the variables "a" and "b" as double then the accurecy becomes better but still the bigger the numbers the less the accuracy.
Until now i wasn't find an accurate round like method.
Using your suggestion Kalus with double variables and 10 decimals in round2 i was get great results for small numbers but after about 1000 again inequalities.
So, probably there is not a chance to check the equality of float or double variables accuratel using round like methods.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Using your suggestion Kalus with double variables and 10 decimals in round2 i was get great results for small numbers but after about 1000 again inequalities.
As you don't explain what exactly your concrete problem is, it's not easy to give you the best answers.
At the moment I really don't understand what exactly your problem is.
You say that with Round2(a ,10) it doesn't work, did you try with Round2(a ,5) or even with Round2(a ,3) ?
In your examples you spoke about 30 iterations and not about 1000 !
You got answers for that question.

Perhaps, if the number of iterations is hudge you should consider following code:
B4X:
b0 = 36.005
For i = 0 To 29
    b0 = b0 - i * 0.1
Next
Multiplying by 1000 would be more accurate than 1000 additions.
But again without knowing the real problem we cannot help as we would like to do.

Best regards.
 
Upvote 0

Pantelis

Member
Licensed User
Longtime User
In your examples you spoke about 30 iterations and not about 1000 !

I don't change the number of iterations but the value of variables a and b. For the same 30 iterations but with bigger numbers i have very smaller accuracy. Smaller than 2 decimals

I want to make an app that does not loose its accuracy with bigger number. I want accuracy of two decimals on any possible number. For example an application calculates money.
In Visualbasic instead of float or double we can use currency datatype for exact calculations on two decimals.
That little code of repetition on additions and subtractions was just an attemp to see what happend to the accuracy of float or double variable values after some series of calculations and how to overcome this problem of inaccuracy using rounding methods.

As in B4A there is no currency datatype the sugestion of mc73 looks good.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I don't change the number of iterations but the value of variables a and b. For the same 30 iterations but with bigger numbers i have very smaller accuracy. Smaller than 2 decimals
Do you have the example where you get an error of 2 decimals with Double numbers.
An accuracy smaller than 2 decimals with Doubles sounds strange to me.
In your case you should use Round2(a ,3) only in the tests and NumberFormat or NumberFormat2 for display.

Best regards.
 
Upvote 0

Pantelis

Member
Licensed User
Longtime User
Klaus,

In my previous post (23) those results was for float numbers. Sorry.

In double variables, 2 decimal error comes with bigger numbers. I know this is a big number. But the error is big as well. And this is only for 30 additions and subtractions. What will happend to the final result after much more calculations even in smaller numbers? In some applications this inaccuracy is unacceptable. Isn't it?
I hope the bignumbers library to have the required accuracy in 2 decimals

In this example the difference is 0.375!

B4X:
Dim a As Double
Dim b As Double
Dim Difference As Double
 a = 100000000000000
 For i = 0 To 29
     a = a + 0.1
 Next 
 
 b = 100000000000006
 For i = 0 To 29
     b = b - 0.1
 Next
 
 Difference = Abs(a-b)
Msgbox(Difference,"Difference")
 
If Round2(a ,3) = Round2(b ,3) Then
    Msgbox ("equal","Result")
Else
    Msgbox ("Not equal","Result")
End If
 
Upvote 0

Sonny

Member
Licensed User
Longtime User
Pantelis,

Perhaps this will work for you:

B4X:
Dim a As Double
Dim b As Double

 a = 30.005
 For i = 0 To 29
     a = a + 0.1
     a = Round2(a,13)
 Next 
 
 Msgbox (a,"a")            
 
 b = 36.005
 For i = 0 To 29
     b = b - 0.1
     b = Round2(b,13)
 Next
 
 Msgbox (b,"b")       

Msgbox(Round2(a ,2),"Check A")    
Msgbox(Round2(b ,2),"Check B")    
 
If Round2(a ,2) = Round2(b ,2) Then
    Msgbox ("equal","Result")
Else
    Msgbox ("Not equal","Result")
End If

If cumulative error is what is unacceptable, then it would appear that the solution would be to correct for it at every iteration.

That is, if I'm correctly understanding your question.

> Sonny <
 
Last edited:
Upvote 0
Top