Code: Select all
Test.f = 1.50
For i = 1 To 5
Test + 0.30
Next i
Div.f = Round(Test / 1.00, 0) * 0.05
Debug Div ; Should be 0.15 instead of 0.10 ((3.0 / 1.0) * 0.05 = 0.15) ...)

Running PB 4.10 b4

Code: Select all
Test.f = 1.50
For i = 1 To 5
Test + 0.30
Next i
Div.f = Round(Test / 1.00, 0) * 0.05
Debug Div ; Should be 0.15 instead of 0.10 ((3.0 / 1.0) * 0.05 = 0.15) ...)
Code: Select all
Test.d = 1.50
For i = 1 To 5
Test + 0.30
Next i
Div.d = Round(Test / 1.00, 0) *0.05
Debug Div
Code: Select all
Test.f = 1.50
For i = 1 To 5
Test + 0.30
Next i
;test.f is now 2.99999....
Div.f = Round(Test, 0) * 0.05 ; Round(2.999999999999999999999999999999,0) is 2.0 , And then 2.0 * 0.05 is 0.1
Debug Div; Correct result is 0.1
Code: Select all
Test.f = 1.50
For i = 1 To 5
Test + 0.30
Next i
Div.f = Round(ValF(StrF(Test / 1.00)), 0) * 0.05
Debug Div ; Should be 0.15 and it is 0.15 now :D
Code: Select all
Result.f = Round(Test.f+0.5,0)
Exactly!Thade wrote:Round is wrong named ... it replaces Floor and Ceil of other languages but does not round in the actual sense.
To get a rounded result always use + 0.5Code: Select all
Result.f = Round(Test.f+0.5,0)
Code: Select all
Test.f = 1.50
For i = 1 To 5
Test + 0.30
Next i
Div.f = Round(Test.f+0.5,0) * 0.05
Debug Div ; Should be 0.15 and it is 0.15 now :D
Code: Select all
Result.f = Int(Test.f+0.5)
Hi Phlos.Phlos wrote:I am sorry guys, but your fix seems to be wrong![]()
If I have the float value 2.7 and I want the integer part "2", doing Round(2.7 + 0.5, 0) will return me "3" instead of "2"![]()
You can't fix the float bad accuracy by adding a constant value
If you just want the integer part then you should use int() not round() as round() implied that you wanted the nearest whole number which is what the +0.5 was obtaining.Phlos wrote:If I have the float value 2.7 and I want the integer part "2", doing Round(2.7 + 0.5, 0) will return me "3" instead of "2"
At the moment may be this workaround can help you, even it is not very elegant, it helps if you need floats and no doubles:Phlos wrote:I am sorry guys, but your fix seems to be wrong![]()
If I have the float value 2.7 and I want the integer part "2", doing Round(2.7 + 0.5, 0) will return me "3" instead of "2"![]()
You can't fix the float bad accuracy by adding a constant value
Code: Select all
Test.f = 1.50
For i = 1 To 5
Test + 0.30
Next i
Div.f = Round(Test.f+0.01,0) * 0.05
Debug Div ; Should be 0.15 and it is 0.15 now :D