Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Exponents
- SandBean
-
New Scratcher
4 posts
Exponents
How to create exponents? I don't want to have to add a bunch of like, 3 * 3 * 3 * 3 and instead 3 ^ 4
- maowMaowQN
-
Scratcher
100+ posts
Exponents
define exponent (base) (power)
set [digit v] to [1]
set [exponent v] to (base)
repeat (power)
set [exponent v] to ((exponent) * (base))
end
- notdamienn3
-
Scratcher
29 posts
Exponents
You should be doing this instead.define exponent (base) (power)
set [digit v] to [1]
set [exponent v] to (base)
repeat (power)
set [exponent v] to ((exponent) * (base))
end
define exponent (base) (power)Without it, it will return base^power+1 instead of base^power.
set [digit v] to [1]
set [exponent v] to (base)
repeat ((power) - (1))
set [exponent v] to ((exponent) * (base))
end
Also, what is the digit variable there for?
- maowMaowQN
-
Scratcher
100+ posts
Exponents
Oh yeah sorry I'm not too used to using the blocks feature and I accidentally put thoseYou should be doing this instead.define exponent (base) (power)
set [digit v] to [1]
set [exponent v] to (base)
repeat (power)
set [exponent v] to ((exponent) * (base))
enddefine exponent (base) (power)Without it, it will return base^power+1 instead of base^power.
set [digit v] to [1]
set [exponent v] to (base)
repeat ((power) - (1))
set [exponent v] to ((exponent) * (base))
end
Also, what is the digit variable there for?
- happywaffle0
-
Scratcher
100+ posts
Exponents
define exponent (base) (power)
set [digit v] to [1]
set [exponent v] to (base)
repeat (power)
set [exponent v] to ((exponent) * (base))
end
that works, but
([10^ v] of (([log v] of (base)::operators) * (exp))::operators)is a better way to do it.
it works because log properties simplify this to
10^(exp*log(base)) -> 10^log(base^exp) ( ylogx = logx^y) -> base^exp
though some floating-point issues may occur so you may want to round() or ( of ()) that.
Last edited by happywaffle0 (Yesterday 20:25:33)
- ClarkeGhost
-
Scratcher
100+ posts
Exponents
Yeah, happywaffle0's does script works perfect,
But if you'd like more ways on how to do it, visit the Scratch Wiki Article on exponents.
([10^ v] of (([log v] of (base)::operators) * (exponent))::operators)
But if you'd like more ways on how to do it, visit the Scratch Wiki Article on exponents.
- Discussion Forums
- » Help with Scripts
-
» Exponents
