Paolo Pini Member Licensed User Longtime User Apr 27, 2023 #1 What is it that I didn't understand? B4X: Copy Dim Value As Float=8.21 Log(Value <=8.20) 'FALSE' Log(Value <=8.21) 'FALSE'? Log(Value <=8.22) 'TRUE'
What is it that I didn't understand? B4X: Copy Dim Value As Float=8.21 Log(Value <=8.20) 'FALSE' Log(Value <=8.21) 'FALSE'? Log(Value <=8.22) 'TRUE'
aeric Expert Licensed User Longtime User Apr 27, 2023 #2 B4X: Copy Log(Value) ' 8.210000038146973 Log(Round2(Value, 2) <= 8.21) ' TRUE Upvote 0
Paolo Pini Member Licensed User Longtime User Apr 27, 2023 #3 aeric said: B4X: Copy Log(Value) ' 8.210000038146973 Log(Round2(Value, 2) <= 8.21) ' TRUE Click to expand... I see, thank you! Upvote 0
aeric said: B4X: Copy Log(Value) ' 8.210000038146973 Log(Round2(Value, 2) <= 8.21) ' TRUE Click to expand... I see, thank you!
aeric Expert Licensed User Longtime User Apr 27, 2023 #4 Not sure why you want to use Float. I think it is better to use Double. Upvote 0
Paolo Pini Member Licensed User Longtime User Apr 27, 2023 #5 aeric said: Not sure why you want to use Float. I think it is better to use Double. Click to expand... I agree, for my calculations, Double is sufficent. ? Upvote 0
aeric said: Not sure why you want to use Float. I think it is better to use Double. Click to expand... I agree, for my calculations, Double is sufficent. ?
Erel B4X founder Staff member Licensed User Longtime User Apr 27, 2023 #6 I don't like Round2. Both Float and Double cannot 100% accurately represent all values. Using BigDecimal from BigNumber library is one option. Another one is to use a method such as this one: Code: Copy Public Sub AreEqual(d1 As Double, d2 As Double) As Boolean Return Abs(d1 - d2) < 0.000001 End Sub If value1 < value2 Or AreEqual(value1, value2) Then ... Upvote 0
I don't like Round2. Both Float and Double cannot 100% accurately represent all values. Using BigDecimal from BigNumber library is one option. Another one is to use a method such as this one: Code: Copy Public Sub AreEqual(d1 As Double, d2 As Double) As Boolean Return Abs(d1 - d2) < 0.000001 End Sub If value1 < value2 Or AreEqual(value1, value2) Then ...
ilan Expert Licensed User Longtime User Apr 27, 2023 #7 this works too: B4X: Copy Dim Value As Float=8.21 Log(NumberFormat2(Value,1,2,0,False) <=8.20) 'FALSE' Log(NumberFormat2(Value,1,2,0,False) <=8.21) 'TRUE' Log(NumberFormat2(Value,1,2,0,False) <=8.22) 'TRUE' Upvote 0
this works too: B4X: Copy Dim Value As Float=8.21 Log(NumberFormat2(Value,1,2,0,False) <=8.20) 'FALSE' Log(NumberFormat2(Value,1,2,0,False) <=8.21) 'TRUE' Log(NumberFormat2(Value,1,2,0,False) <=8.22) 'TRUE'
Erel B4X founder Staff member Licensed User Longtime User Apr 27, 2023 #8 This is quite hacky. NumberFormat returns a string which is them implicitly parsed to a number. The only reason that it works is that the implicit parsing returns a double which is the same type of the right hand side. I wouldn't consider it safe code. The correct way to compare two floating point numbers for equality is by dealing with the expected inaccuracy. Upvote 0
This is quite hacky. NumberFormat returns a string which is them implicitly parsed to a number. The only reason that it works is that the implicit parsing returns a double which is the same type of the right hand side. I wouldn't consider it safe code. The correct way to compare two floating point numbers for equality is by dealing with the expected inaccuracy.
Daestrum Expert Licensed User Longtime User Apr 27, 2023 #9 Or make the comparison valid Code: Copy Dim Value As Float=8.21 Log(Value <= (8.20).As(Float)) 'FALSE' Log(Value <= (8.21).As(Float)) 'TRUE' Log(Value <= (8.22).As(Float)) 'TRUE' Upvote 0
Or make the comparison valid Code: Copy Dim Value As Float=8.21 Log(Value <= (8.20).As(Float)) 'FALSE' Log(Value <= (8.21).As(Float)) 'TRUE' Log(Value <= (8.22).As(Float)) 'TRUE'