I have two variables x,y that are calculating very small numbers between 0 and 1 depending on variables a and b. It is understandable that the values are going to sometimes come up as “nan”. However, when I set a and b to values that make x or y a small number, then a value that computes nan for x or y, then set a or b back to what should compute a small number again, the values stay as nan. The project is running while doing those steps. Can someone try this out to see if it’s a bug, or just a problem in my code? Or teach me how to round to the nearest nth?
@PlatniumFrog If you want to check if a value is nan, then check the value against itself. If it’s not equal to itself, it’s nan.
function setup()
a=0/0
print(a)
if a~=a then
print("a = nan, setting a to 0")
a=0
end
print("a = ",a)
end
If you still have problems, post the code
Could I have a function that rounds an oversized decimal to the maximum number of decimal places a number can have in codea/lua?
Here’s a routine. Pass it a number and how many decimal places you want. Just figure out how many decimal places you want. If you always want a fixed number of decimal places, replace dp with that value.
EDIT: The number of decimal places you can have will depend on how many digits are before the decimal point.
EDIT: With this routine, it looks like you can have a total of 14 digits.
function setup()
a=123.45678901645678912
b=round(a,8)
print(b)
end
function round(val,dp)
return((val*10^dp+0.5)//1)/10^dp
end
Here is what I have been trying to build.
The forum post handler leaves out multiplication stars and auto detects code which personally is slightly annoying ![]()
Ths means that to use this while in the codea editor, you will have to insert a multiplication star inbetween joined variables.
Obviously all varibles are defined in the setup function so you will have no trouble.
Code starts here and ends before “code stops”:
-- Fractler
-- This draws a mandlebrot fractal and later will draw custom fractals given a mathimatical formula
-- code is translated to lua from the code discussed in this video:
-- https://youtu.be/WXdB_cLe8ZI
-- Use this function to perform your initial setup
function setup()
-- canvas x and y values
canX = 175
canY = 175
-- the shapes that can be drawn are "point", "circle", and "rect"
shape = "rect"
-- values used for mathimatical formulas
-- (I haven't gotten the fornula right...)
x = 0
y = 0
a = -2
b = -2
-- res is a value between 0 and 1 that is how detailed the fractal renders
res = 0.05
-- this table contains "valid" points and is explained later
points = {}
-- this bool tell wether the fractal is done rendering
finished = false
-- this is used to tell where to add walues to "points"
key = 0
end
-- functions separator and console are going to be used later for creating custom fractals
function separator()
pushStyle()
pushMatrix()
strokeWidth(5)
stroke(0, 0, 0, 128)
line(200,0,200,HEIGHT)
popMatrix()
popStyle()
end
function console()
s = keyboardBuffer()
pushStyle()
pushMatrix()
textAlign(LEFT)
textWrapWidth(180)
fill(0, 0, 0, 128)
fontSize(20)
text(s,20,(HEIGHT/2)+200)
popMatrix()
popStyle()
end
-- the function below has not been implemented
-- function round(val,dp)
-- return((val*10^dp+0.5)//1)/10^dp
-- end
-- This function gets called once every frame
function diverges()
-- the point at which the mandlebrot set diverges
-- is 2 from the center
if (x*x)+(y*y) < 4 then
return true
else
return false
end
end
function iterate(n)
-- this function contains formulas that can be called over and over again
-- to iterate
for i=0,1,n do
local tmp = (x*x) - (y*y) + a
y = (x*y) + (x*y) + b
x = tmp
end
end
function draw()
--[[if CurrentTouch.state == BEGAN
and CurrentTouch.x <= 200 then
showKeyboard()
end]]
-- This sets a light background color
background(0, 11, 25, 255)
-- this draws all the points that are within the fractal
for i=0,1,res do
iterate(res) -- this iterates the mathimatical funtion for each
-- value of "a" and "b"
if finished == false then
-- this makes "a" and "b" go through all values
-- between 2 and -2
if a <= 2 then
a = a + res
end
if a >= 2 then
if b <= 2 then
a = -2
b = b + res
end
end
if b >= 2 then
-- this discontiues the loop by setting "finished"
-- to "true" when the loop requires it to be "false"
finished = true
a = -2
b = -2
end
if diverges() == true then
-- if the point does not diverge, then we add it to
-- the table with a key greater than the previous key by 1
key = key + 1
points[key] = {a = a,b = b}
end
end
end
fill(3, 0, 38, 255)
rect((WIDTH/2)-(canX*2),(HEIGHT/2)-(canY*2),canX*4,canY*4)
fill(89, 255, 0, 255)
for k,v in pairs(points) do
-- this draws all the points
pushStyle()
noSmooth()
if shape == "point" then
point((WIDTH/2)+(v.a*canX),(HEIGHT/2)+(v.b*canY))
end
if shape == "rect" then
rect((WIDTH/2)+(v.a*canX),(HEIGHT/2)+(v.b*canY),canX*res,canY*res)
end
if shape == "circle" then
ellipse((WIDTH/2)+(v.a*canX),(HEIGHT/2)+(v.b*canY),canX*res,canY*res)
end
smooth()
popStyle()
end
-- below are safe guards from Nan and inf values
-- I would like them to round these numbers instead of
-- setting them to 0.
-- Codeas values dont change/get stuck when they have the value of Nan or inf
if x ~= x then
x = 0
end
if y ~= y then
y = 0
end
-- below certain variables are printed so I can view them live
text(x.." "..y,WIDTH/2,HEIGHT-20)
text(a.." "..b,WIDTH/2,HEIGHT-40)
text(tostring(diverges()).." "..key,WIDTH/2,HEIGHT-60)
end
Code stops
Cool right? Anyway, the reason behind all this is that I am trying to get the Mandelbrot formula right. You can copy/paste this if you want to check it out ![]()
@PlatniumFrog Anytime you want to post code, put 3 ~'s on a line before and after the code so it formats correctly. I added them to your code above. There is a Mandelbrot example included with Codea. You can look at it for the formula.
I knew that, but I thought that it just edited the sprite/icon. Am I wrong?
The Mandelbrot example creates the Mandelbrot image. You can change the limit of the Mandelbrot calculations. I have some code for Mandelbrot that allows you to change the limit and also zoom in on selected areas.
@PlatniumFrog See this link for a better Codea Mandelbrot program.
https://codea.io/talk/discussion/2213/mandelbrot-essl-an-experiment-with-fragment-shaders-codea-1-5