B4J Question For Next with decimal step not going to the final value

kostefar

Active Member
Licensed User
Longtime User
Dear All,

The below code generates only numbers up to 0.19. Shouldn´t it go to 0.2?
Apart from that, I dont get the long strange decimal numbers it throws without the rounding but thats of minor importance.

B4X:
    Dim i As Double
    For i = 0.05 To  0.2 Step 0.01
    
        Log (Round2(i,2))
    Next
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You should add a Log(i) after the loop is done to see what the value is. Without testing, my guess is that it's 0.20000000001 or something like that, which is why it's not giving you that last value. As for why this happens, remember doubles are internally stored in a binary format. This means some decimal values cannot be stored in their exact form. That is why you get these small rounding errors and "long strange decimal numbers", as you put it.

Whenever possible, you should prefer integers for loop indices.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Use NumberFormat if you want to Format the Output.
 
Upvote 0

PatrikCavina

Active Member
Licensed User
Longtime User
Fully agree with @Roycefer.
I think the only way to view your last value is:
B4X:
    Dim i As Double
    For i = 0.05 To 0.2 Step 0.01
        Log(Round2(i, 2))
    Next
    Log(Round2(i, 2))
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
B4X:
 Dim i As Double = 0.05d
 Do While (i<=0.2d)
  Log (Round2(i,2))
  i = Round2(i+0.01d,2)
 Loop

Thanks to all for the suggestions, but this one suits the best what I´m trying to do. The reason is that in the loop there´s a whole lot of things going on, that for clarity reasons are not posted here. So I´ll be changing for/next into a do/loop, but what does the d stand for?
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
B4X:
Dim i As Int
Dim j As Double
For i = 5 To  20
    j = i / 100
    Log (NumberFormat(j, 0, 2))
Next

Thank you Klaus, I thought about that too. The thing is that it´d be confusing to look at the code if I first had to multiply the values when they´re set, and then later divide when in the loop.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
My java head showing through, numbers suffixed with a 'd' denote they are double values.
suffix 'f' = float

Ah so it can be done without the d then?

Hmm, no it can´t - even if i as defined as a double earlier....?

EDIT: Yes it can :)
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The answer is possibly, but by adding the d suffix it helps the compiler know I mean a double.
The (i<=0.20) without the 'd' could be a float value, where the compiler 'could' convert the 'i' to a float before the comparison.(it probably wouldn't as 'i' is defined as Double)
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
The answer is possibly, but by adding the d suffix it helps the compiler know I mean a double.
The (i<=0.20) without the 'd' could be a float value, where the compiler 'could' convert the 'i' to a float before the comparison.(it probably wouldn't as 'i' is defined as Double)

Good that you´re pointing this out Daestrum, thanks!
 
Upvote 0
Top