Loops in floats

Just starting out? Need help? Post your questions and find answers here.
AlfaRomeo
New User
New User
Posts: 9
Joined: Fri Jun 21, 2019 10:28 am

Loops in floats

Post by AlfaRomeo »

Hi, I want to convert a routine from QB64 to PureBasic but it includes a loop with a STEP in floating point..
How I could convert this:

Code: Select all

FOR a = 0 TO 50 STEP .04
...
code
...
NEXT
Thanks in advance
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5022
Joined: Sun Apr 12, 2009 6:27 am

Re: Loops in floats

Post by RASHAD »

Hi
Try

Code: Select all

Define a.f
Repeat
  Debug a
  a.f+0.04 
Until a => 50

Code: Select all

Define a.f
While a < 50.04
  Debug a
  a.f+0.04 
Wend
Egypt my love
User avatar
STARGÅTE
Addict
Addict
Posts: 2277
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Loops in floats

Post by STARGÅTE »

Please note that loops with floats are not numerically stable.
Depending on the increment value, you sometimes reach the final value and sometimes not, even if you use doubles!

Code: Select all

Define a.d, da.d

a = 0.0  :  da = 0.1
While a <= 2
	Debug StrD(a, 3)
	a + da
Wend
Debug "---"

a = 0.0  :  da = 0.2
While a <= 2
	Debug StrD(a, 3)
	a + da
Wend
Debug "---"
It is more stable to use:

Code: Select all

Define ia.i, a.d
For ia = 0 To 5000 Step 4  :  a = ia / 100.0
	; code with "a"
Next
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
AlfaRomeo
New User
New User
Posts: 9
Joined: Fri Jun 21, 2019 10:28 am

Re: Loops in floats

Post by AlfaRomeo »

With the code:

Define ia.i, a.d
For ia = 100 To 5000 Step 4 : a = ia / 100.0
; code with "a"
Next

it worked like a charm :)

Thank you all
User avatar
jacdelad
Addict
Addict
Posts: 2049
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Loops in floats

Post by jacdelad »

I would prefer the RASHAD version since it avoids the division. But beside speed, both work.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Loops in floats

Post by wilbert »

jacdelad wrote: Fri Dec 19, 2025 1:57 pm I would prefer the RASHAD version since it avoids the division. But beside speed, both work.
If the goal is to avoid division you could also do

Code: Select all

Define ia.i, a.d
For ia = 100 To 5000 Step 4 : a = ia * 0.01
; code with "a"
Next
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
jacdelad
Addict
Addict
Posts: 2049
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Loops in floats

Post by jacdelad »

Yes, that's good too. But RASHADs version uses an addition, this one a multiplication. I guess the addition is still a bit faster.
So many solutions for one problem, that's one of the points why I like programming. :mrgreen:
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
STARGÅTE
Addict
Addict
Posts: 2277
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Loops in floats

Post by STARGÅTE »

jacdelad wrote: Sat Dec 20, 2025 7:17 am I guess the addition is still a bit faster.
No, not on modern CPUs/FPUs. Nowadays, also MUL or FMUL need only a single clock cycle, similar to ADD or FADD.
But of course, adding 0.04 directly to the iteration variable is faster than iterating a dummy integer variable and then scale it.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply