Rounding a float to the nearest multipul

BlitzMax Forums/BlitzMax Programming/Rounding a float to the nearest multipul

Duckstab[o](Posted 2006) [#1]
Hi

been trying for a hour to do this without much luck

I have Have some arrays
Gridz[9,9,9]
Gridx[9,9,9]
Gridy[9,9,9]

For x=0 to 8
For y=0 to 8
For z=0 to 8
Gridx[x,y,z]=-256+(64*x)
Gridy[x,y,z]=256-(64*y)
Gridz[x,y,z]=-256+(64*z)
next
next
next

Next i Create 3 random floats within the range of -256 to 256

floatx#=Rnd(-288,288)
floaty#=Rnd(-288,288)
floatz#=Rnd(-288,288)

now comes my problem

I want to Get these values in ints so i can find out what was the closest Grid array in x,y,z

So Floatx,Floaty,Floatz need to be Rounded to the nearest multipul of 64

anyideas welcome thanks


H&K(Posted 2006) [#2]
X = Int (FloatX)/64
or
x = int (FloatX +32)/64


sswift(Posted 2006) [#3]
Your post made absolutely no sense to me.

But if you want to know the nearest multiple of 64 to a floating point value, then all you need to do is this:

Mul = Floor(Floatx#/64) * 64

Ie, find out how many times 64 goes into the floating point value, (say, 2.5) discard any fractional bits, (2) then multiply the result by 64 to get the nearest multiple.

Unless by nearest multiple you don't mean nearest multiple without going over, but rather, nearest as in the multiple can be higher than the value, but the difference is less than that of the nearest multiple which is lower than the value. In that case, I think you just have to add 0.5 to FloatX#/64 before you floor it to get it to do banker's rounding.


Yan(Posted 2006) [#4]
X = Int((floatX / 64) + 0.5) * 64


[edit]
Hmmm...Getting a cup of tea, and not refreshing before you post is never a good idea...Ah well...*slurp*...
[/edit]


Brendane(Posted 2006) [#5]
Like sswift says.

Or rather :-
nearestMultiple = value - (value Mod 64)

[Edit] - but that's not right is it.. I'll get my coat.