# Realistic math solver

@Prynok Here’s an example.

``````
function setup()
str1=split(13579)
str2=split(12589)
print(table.concat(str1," "))
print(table.concat(str2," "))
for z=1,#str1 do
v1=str1[z]
v2=str2[z]
if v1==v2 then
print(v1," equal ",v2)
end
if v1>v2 then
print(v1," greater ",v2)
end
if v1<v2 then
print(v1," less ",v2)
end
end
end

function split(val)
local str={}
while val>0 do
table.insert(str,val%10)
val=math.floor(val/10)
end
return str
end

``````

@dave1707 That’s pretty much what I did, but mine is a lot smaller…

@SkyTheCoder I’m watching TV and wasn’t paying that much attention to everything being posted. Yours is an easier way after I looked at it.

@SkyTheCoder how could I compare with yours? Sorry if I’m sounding like a noob

@SkyTheCoder I just realized that your code is comparing the digits left to right. The numbers need to be compared right to left because that’s the way @Prynok needs to do his math examples. You need to add a string.reverse for the strings.

@dave1707 No string.reverse needed. Just a slight for loop tweak.

``````local numstr = tostring(num)
local numstr2 = tostring(num2)
for i = math.max(string.len(numstr), string.len(numstr2)), 1, -1 do
local c = string.sub(numstr, i, i)
local c2 = string.sub(numstr2, i, i)
-- Compare c and c2.
end
``````

@Prynok You can compare the two digits with c and c2. Going from right to left, it will run the code with c being the first number’s character and c2 being the second number’s character, for each digit in the numbers.

@Prynok @SkyTheCoder Just a reminder, both of the numbers may not be the same length. You will have to add code to handle conditions when one string is longer than the other.

@dave1707 I thought of that earlier. It was pseudo-code, so I didn’t bother. It already goes for the maximum length of the two numbers. I’ll work on that, though.

Also: I’ll need support for decimals.

@dave1707 so if one of the numbers is bigger then the other, you could do somthing like

``````if #str1 > #str2 then

--code
elseif #str2 > # str1

--code
end
``````

?

@Prynok Something like that would work, but as @SkyTheCoder pointed out, you also have to align the decimal points if you use them. The strings could be the same length, but aligning the decimal points could cause the strings to act like they’re different lengths for the compare.

is there a way to stop the user from using decimals?

@Prynok One way to eliminate decimals is the math.floor() command.

In the reference it says it is used for rounding, how can I use it for other things?

Got a version with decimals and different length numbers working.

``````
-- Number comparing

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
local cb = function()
output.clear()
compare(a, b)
end
parameter.number("a", 0, 1000, 583, cb)
parameter.number("b", 0, 1000, 763.1, cb)
output.clear()
compare(583, 763.1)
end

function compare(num, num2)
local numstr = tostring(num)
local numstr2 = tostring(num2)
local _ = string.find(numstr, ".", 1, true)
local _2 = string.find(numstr2, ".", 1, true)
local d = false
local d2 = false
if _ ~= nil then
d = _
end
if _2 ~= nil then
d2 = _2
end
print("Decimal 1: " .. tostring(d))
print("Decimal 2: " .. tostring(d2))
for i = math.max(string.len(numstr), string.len(numstr2)), 1, -1 do
local c = "0"
if i <= string.len(numstr) then
c = string.sub(numstr, i, i) or "0"
end
local c2 = "0"
if i <= string.len(numstr2) then
c2 = string.sub(numstr2, i, i) or "0"
end
-- Compare c and c2.
if i ~= d and i ~= d2 then
local n = tonumber(c) or 0
local n2 = tonumber(c2) or 0
if n > n2 then
print(c .. " > " .. c2)
elseif n < n2 then
print(c .. " < " .. c2)
elseif n == n2 then
print(c .. " = " .. c2)
end
elseif i == d and i == d2 then
print("Both decimals reached.")
elseif i == d then
print("#1 decimal reached.")
elseif i == d2 then
print("#2 decimal reached.")
end
end
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

end

``````

@Prynok Yes, it is used for rounding. math.floor() rounds down. So any number that has a decimal (123.456) gets rounded down (123) . So basically it can be used to eliminate the fraction part of a number.

Wait, I think this one fixes a bug with decimals.

``````
-- Number comparing

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
local cb = function()
output.clear()
compare(a, b)
end
parameter.number("a", 0, 1000, 583.23, cb)
parameter.number("b", 0, 1000, 763.1, cb)
output.clear()
compare(583.23, 763.1)
end

function compare(num, num2)
local numstr = tostring(num)
local numstr2 = tostring(num2)
local _ = string.find(numstr, ".", 1, true)
local _2 = string.find(numstr2, ".", 1, true)
local d = false
local d2 = false
if _ ~= nil then
d = _
end
if _2 ~= nil then
d2 = _2
end
numstr = numstr .. string.rep("0", math.max(d or 0, d2 or 0))
numstr2 = numstr2 .. string.rep("0", math.max(d or 0, d2 or 0))
print("Decimal 1: " .. tostring(d))
print("Decimal 2: " .. tostring(d2))
for i = math.max(string.len(numstr), string.len(numstr2)), 1, -1 do
local c = "0"
if i <= string.len(numstr) then
c = string.sub(numstr, i, i) or "0"
end
local c2 = "0"
if i <= string.len(numstr2) then
c2 = string.sub(numstr2, i, i) or "0"
end
-- Compare c and c2.
if i ~= d and i ~= d2 then
local n = tonumber(c) or 0
local n2 = tonumber(c2) or 0
if n > n2 then
print(c .. " > " .. c2)
elseif n < n2 then
print(c .. " < " .. c2)
elseif n == n2 then
print(c .. " = " .. c2)
end
elseif i == d and i == d2 then
print("Both decimals reached.")
elseif i == d then
print("#1 decimal reached.")
elseif i == d2 then
print("#2 decimal reached.")
end
end
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

end

``````

No need for rounding.

@SkyTheCoder I changed the parameters to go to 1100. There seems to be a problem when the first number is below 1000 and the second number is above 1000.

Found that, fixing it, almost there.

It’s a bug where if the two decimals are in different places, it skips a number.

If it finds a decimal at the interpolated index, it skips the index for both numbers…

Harder than I thought, but I thought of a sneak way around it. Code soon…

Okay, it’s a little hacky but it’s better than nothing. Here it is:

``````
-- Number comparing

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
local cb = function()
output.clear()
compare(a, b)
end
parameter.number("a", 0, 10000, 583.235, cb)
parameter.number("b", 0, 10000, 763.1, cb)
output.clear()
compare(583.235, 763.1)
end

function compare(num, num2)
local numstr = tostring(num)
local numstr2 = tostring(num2)
local _ = string.find(numstr, ".", 1, true)
local _2 = string.find(numstr2, ".", 1, true)
local d = false
local d2 = false
if _ ~= nil then
d = _
end
if _2 ~= nil then
d2 = _2
end
local nslen = string.len(numstr)
local ns2len = string.len(numstr2)
local nsflen = string.len(tostring(math.floor(tonumber(numstr))))
local ns2flen = string.len(tostring(math.floor(tonumber(numstr2))))
numstr = string.rep("0", math.max(nslen, ns2len) - (d or 0)) .. numstr
numstr2 = string.rep("0", math.max(nslen, ns2len) - (d2 or 0)) .. numstr2
numstr = numstr .. string.rep("0", math.max(math.max(0, nsflen) - (d2 or 0), math.max(0, ns2flen) - (d2 or 0)))
numstr2 = numstr2 .. string.rep("0", math.max(math.max(0, nsflen) - (d or 0), math.max(0, ns2flen) - (d2 or 0)))
print(numstr .. "\
" .. numstr2)
local _ = string.find(numstr, ".", 1, true)
local _2 = string.find(numstr2, ".", 1, true)
local d = false
local d2 = false
if _ ~= nil then
d = _
end
if _2 ~= nil then
d2 = _2
end
print("Decimal 1: " .. tostring(d))
print("Decimal 2: " .. tostring(d2))
for i = math.max(string.len(numstr), string.len(numstr2)), 1, -1 do
local c = "0"
if i <= string.len(numstr) then
c = string.sub(numstr, i, i) or "0"
end
local c2 = "0"
if i <= string.len(numstr2) then
c2 = string.sub(numstr2, i, i) or "0"
end
-- Compare c and c2.
if i ~= d and i ~= d2 and (c ~= "0" or c2 ~= "0") then
local n = tonumber(c) or 0
local n2 = tonumber(c2) or 0
if n > n2 then
print(c .. " > " .. c2)
elseif n < n2 then
print(c .. " < " .. c2)
elseif n == n2 then
print(c .. " = " .. c2)
end
elseif i == d and i == d2 then
print("Both decimals reached.")
elseif i == d then
print("#1 decimal reached.")
elseif i == d2 then
print("#2 decimal reached.")
end
end
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)