This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
IceCrasher's profile picture

[lua][failure] Sphere Program, Nan With X^0.5

Started by IceCrasher, 14 October 2013 - 12:51 PM
IceCrasher #1
Posted 14 October 2013 - 02:51 PM
Hi guys i just wrote a little programme to build a quarter of the under half of a sphere. If i run it i get about 9 times a "nan" and then it goes on…but this part doesn't matter.
I think something with the goSide function is wrong, because if i use it and then print the result i get "nan". But i really don't know how to solve it, maybe you got a tip with x^0.5?

I hope you can help me. Here the programm:

Pastebin: http://pastebin.com/xqfbzerh

Code:
Spoiler

-------------------------------------- configurable
local r = 10 --r = Radius -0.5
local refuel = 15--Slot with Ender for Fuel
local mat = 16--slot with Ender for Buildingmaterial
local matTurtle = 1--free Slot
---------------------------------- not configurable
local matTurtle2 = matTurtle + 1 --free Slot
local emh = r --Entfernung zur Mitte horizontal
local emv = 0 --Entfernung zur Mitte vertikal
local aEMH = emh
---------------------------------------------- math
function round(a)
  if (a - math.floor(a)) < 0.5 then
	return math.floor(a)
  else
	return math.ceil(a)
  end
end
function goSide(r, emh, emv)
  local xGoSide = ((r ^ 2 - emv ^ 2) ^ 0.5 - emh ^ 2) ^ 0.5
  if xGoSide < 0 then
	return xGoSide * -1
  else
	return xGoSide
  end
end
----------------------------------------------- re-
function recharge()
  while turtle.getFuelLevel() < r + 100 do
	turtle.select(refuel)
	turtle.placeDown()
	turtle.suckDown()
	turtle.refuel()
	turtle.digDown()
  end

  while turtle.getItemCount(matTurtle) == 0 do
	turtle.select(mat)
	turtle.placeDown()
	turtle.select(matTurtle)
	turtle.suckDown()
	turtle.select(matTurtle2)
	turtle.dropDown()
	turtle.select(mat)
	turtle.digDown()
  end
end
-------------------------------------- move / place
function build()
  for i = 1, emh do
	turtle.turnLeft()
	turtle.forward()
	turtle.turnRight()
	print(round(goSide(aEMH, emh, emv)))
	for i = 1, round(goSide(aEMH, emh, emv)) do
	  recharge()
	  turtle.select(matTurtle)
	  turtle.placeUp()
	  if i > 1 then
		turtle.forward()
	  end
	end
	aEMH = aEMH - 1
  end
end
for i = 1, r do
  recharge()
  for i = 1, emh + 1 do
	turtle.forward()
  end
  turtle.turnLeft()
  build()
  turtle.turnLeft()
  for i = 1, emh do
	recharge()
	turtle.forward()
  end
  turtle.turnLeft()
  turtle.down()
  emv = emv + 1
  emh = round(r^2-emv^2)
  aEMH = emh
end
electrodude512 #2
Posted 14 October 2013 - 04:40 PM
Your problem is on this line:

local xGoSide = ((r ^ 2 - emv ^ 2) ^ 0.5 - emh ^ 2) ^ 0.5
Lets say we call goSide(1,2,3). That means r=1, emv=2, and emh=3.
(r ^ 2 - emv ^ 2) evaluates to -8
(r ^ 2 - emv ^ 2) ^ 0.5 takes the square root of -8, but Lua only supports real numbers and so returns nan. You're taking the sqaure root of a negative number, which you can't do with real numbers.
IceCrasher #3
Posted 14 October 2013 - 04:54 PM
Wow that was so obvious and i didn't find it out myself hahaha

Thank you a lot :)/>