As X doubles Y should become the half of X

I trying but I can’t figure out how to do it without hard coding.

Thats how it works but its ugly:


if x < 0.03125 then
	y = 0.015625
elseif x < 0.0625 then
	y = 0.03125
elseif x < 0.125 then
	y = 0.0625
elseif x < 0.25 then
	y = 0.125
elseif x < 0.5 then
	y = 0.25
elseif x < 1 then
	y = 0.5
elseif x <= 2 then
	y = 1
elseif x <= 4 then
	y = 2
elseif x <= 8 then
	y = 4
elseif x <= 16 then
	y = 8
elseif x <= 32 then
	y = 16
elseif x <= 64 then
	y = 32
end


Any Ideas?

Is it only when the number has doubled or is y always half of x?

only when it doubled.

else i could do y = x/2

[LUA]
local exponent = math.floor(math.log(x)/math.log(2))-1
local y = math.pow(2, exponent)
[/LUA]

Edit:

It might do exactly what you want but if not just tweak it until it does

Wow thank you!

Edit:
Exactly what I wanted. But I replaced floor with Round because describing my problem to get he solution with Round would have been more difficult^^
I actually was messing around with log but I dont know how to use it properly.