Repeat by Decimal (Loop with a decimal increment)

hi,

I would like to create a list of decimal numbers. I could do this manually but I would like to keep the value of the bin_width flexible.

I tried to generate the result in a script similar to that below but it runs infinitely.

a little bit of trial and errors tells me that applescript rounds the step value up or down depending whether it is less than or greater than 0.5.

can anyone offer a solution to this prob?

( i could set x to 10 and bin_width to 1 and divide the output by 10, but i would rather now how to step by a decimal)

also… has anyone figured out how to step in a non-linear manner? e.g. log(base10)???

cheers

Nic


set x to 1
set bin_width to 0.1
set x_list to {}

repeat with x_ from 0 to x by bin_width
	
	
	log "x_ is now " & x_
	
	set x_list to x_list & x_
	
end repeat

and, just quickly. a script showing the “factor of 10” solution (just in case anyone is interested)


set x to 10
set bin_width to 1
set x_list to {}

repeat with x_ from 0 to x by bin_width
	
	
	log "x_ is now " & x_
	
	set x_div to x_ / 10
	
	set x_list to x_list & x_div
	
	
	
end repeat

log "x_list is " & x_list






set x to 1
set bin_width to 0.1
set x_list to {}

set x_ to 0
repeat until x < x_
	set x_ to x_ + bin_width
	log "x_ is now " & x_
	
	set x_list to x_list & x_
	
end repeat

excellent!

thanks Mike (again)

but why does it work? mmmm,

i see that you are adding the decimal ( + sign ) rather than using “by”. I guess that means that it was “by” that was forcing the rounding error.

is this correct?

I modified the script a little. The final value in the list was (x + bin_width) (i.e. 1.1 where bin_with equals 0.1). so i negated this by adding bin_width to x_. conversely i could have subtracted bin_width from x on the left hand side of the less than sign.

thanks again!



set x to 1
set bin_width to 0.1
set x_list to {}

set x_ to 0
repeat until x < x_ + bin_width
	
	
	set x_ to x_ + bin_width
	log "x_ is now " & x_
	
	set x_list to x_list & x_
	
end repeat


In addition to the final value of x_, you might want to consider how many items you want in x_list when x = 1.05 and bin_size = .1

Not using the + bin_size in the comparison and putting the line

set x_ to item -1 of x_list

after the end repeat might help coordinate those two factors.

thanks mike,

i’ll give that a try.