Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 Re: Coder's Corner » LUA how is a function navigating through fields of a table » 2021-05-25 02:29:35

i got an answer on another website, so i was under the impression that in this part

super.s = s

the parameter passed to the function

tb.new

replaces both

s=s

turns out it does not and only replaces the rvalue so

 return s1.s...s2.s

works because the index of

 super

is

s

not the value passed to the function  and the table

super

is destroyed upon exit of the

tb.new

function and when it is called again the table is created again, so there are two different versions of the same table.

#2 Coder's Corner » LUA how is a function navigating through fields of a table » 2021-05-22 01:28:39

Xuer
Replies: 1

i already asked this question on stackoverflow and accepted an answer i think i did not really understand the answer and i got some more question, im embarrassed to necro it so im posting it here


im learning lua and got to metatable part, in this example

 local tb = {}
local meta = {}

function tb.new(s)
local super = {}
super.s = s

setmetatable(super,meta)

return super
end

function tb.add(s1,s2)

return s1.s..s2.s

end

meta.__add = tb.add

f= tb.new("W")
t= tb.new("E")

print(f+t)

when compiler gets to

 f = tb.new("W") 

i think this happens

 function tb.new("W")  super.W = W return super end 

so

print(f+t) 

looks like

print(super+super) 

how does

 tb.add(s1,s1) 

find the fields of super table using

 return s1.s..s2.s

also as the

 tb.new

function is called twice and

setmetatable(super,meta) 

happens twice is there any difference between the first and second iteration? if any of the above are incorrect please correct me.

#3 Re: Help Me ! » simple Long division problem » 2020-03-18 03:27:14

bob bundy wrote:

hi Xuer

For any division ('long' or 'short'), start by trying to divide into the first digit ie. 7 into 1.  As this 'won't go' then try 7 into 16.  This goes twice ie 2 units so the 2 goes above the 6.

Or think about a simple case such as 14 / 7.  You know this is 2.  16 is a little bigger so the answer should be 2 and a bit.  Doing a rough calculation first often helps.

Bob


thnx for your reply

#4 Help Me ! » simple Long division problem » 2020-03-16 02:58:30

Xuer
Replies: 2

toup.png

i have thought for a while and found my mistake in the first step when i did 16/7 i placed 2 on top of 1.why is 2 placed on top of 6? bc its a 1 digit number so we place it on ones place which is 6? pls correct me if im wrong

Board footer

Powered by FluxBB