From Test-Scratch-Wiki

m (Add interwikis) (canned edit summary)
(References)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{Eng:Solving Exponents/translate}}'''Solving [[Wikipedia:Exponentiation|exponents]]''' is the process of multiplying a number by itself multiple times. For example:
+
{{Eng:Solving Exponents/translate}}'''
 +
指数运算的结果。n<sup>m</sup>指将n自乘m次(针对m为正整数的场合)。把n<sup>m</sup>看作乘方的结果,叫做“n的m次幂”或“n的m次方”,其中,n称为“底数”,m称为“指数”(写成上标)。当不能用上标时,例如在编程语言或电子邮件中,通常写成n^m。例如:
  
 
'''2<sup>3</sup> = 2 * 2 * 2 = 8'''
 
'''2<sup>3</sup> = 2 * 2 * 2 = 8'''
 +
读作:2的3次方,或2的3次幂。
  
Many people want a block like this to make exponents easier to program:<ref>http://scratch.mit.edu/discuss/topic/15646/</ref> <sb>(() ^ ():: operators)</sb>  
+
大家可以使用这个积木块来让指数运算更容易编程:
 +
<ref>http://scratch.mit.edu/discuss/topic/15646/</ref> <sb>(() ^ ():: operators)</sb>  
  
==Using the Method in Scratch==
+
==在 Scratch中的计算方法 ==
There are multiple ways to do exponents in Scratch. Here are two:
+
在Scratch中有多种方法来求解指数。这里有两个:
  
=== Repeat Method ===
+
=== 循环的方法 ===
{{note|this method can only be used with whole numbers}}
+
{{note|此方法只能用于整数}}
This script will be able to solve exponents in Scratch; however, it cannot calculate decimals:
+
这个脚本将能够在Scratch中解决指数; 但是,它不能计算小数:
  
 
<scratchblocks>
 
<scratchblocks>
Line 28: Line 31:
 
</scratchblocks>
 
</scratchblocks>
  
==== How it Works ====
+
==== 如何操作 ====
*The Variable "1st#" is the base number. This is the number that will be multiplied by.
+
*变量 "1st#" 是底数, 这是循环运算的数字。
*The Variable "2nd#" determines how many times to repeat the multiplication.
+
*变量 "2nd#" 是指数,是程序里面循环乘法的次数。
*The variable "ans" is the answer, which is the number 1 multiplied by "1st#", "2nd#" times.
+
*变量 "ans" 是答案, 底数"1st#"循环乘"2nd#"次后的答案。
  
=== Logarithmic Method ===
+
=== 对数法 ===
The logarithm method is much faster than the repeat method.
+
对数法比循环法快得多。
 
<scratchblocks>
 
<scratchblocks>
 
([10 ^ v] of ((power) * ([log v] of ([abs v] of (base)))))
 
([10 ^ v] of ((power) * ([log v] of ([abs v] of (base)))))
 
</scratchblocks>
 
</scratchblocks>
or
+
或者
 
<scratchblocks>
 
<scratchblocks>
 
([e ^ v] of ((power) * ([ln v] of ([abs v] of (base)))))
 
([e ^ v] of ((power) * ([ln v] of ([abs v] of (base)))))
 
</scratchblocks>
 
</scratchblocks>
  
==== How it works ====
+
==== 如何操作 ====
*The variable "base" is the base and the variable "power" is the power, or the number of times the base is multiplied. The reported value is the answer.
+
*变量 "base" 底数, 变量 "power" 是指数, 或基数乘以的次数。返回值是答案。
*The math works because n * log m = log m<sup>n</sup> and 10<sup>log m<sup>n</sup></sup> = m<sup>n</sup>.
+
*数学运算中: n * log m = log m<sup>n</sup> and 10<sup>log m<sup>n</sup></sup> = m<sup>n</sup>.
  
====Special Cases====
+
====特殊案例====
These are all errors, so it is advised to have cases for them:
+
这些都是错误,所以建议编程中注意:
*The base is 0: Since log 0 is undefined, have an if-else that gives ''0'' for the answer if the base is 0, as long as the power is positive. The answer ''1'' should be given for 0<sup>0</sup> so that it does not give an error then.
+
*指数是0:当指数为零时,幂的值是有意义的。此时规定a<sup>0</sup>=1 叫作零指数幂,又叫零次幂。但是0<sup>0</sup>0是无意义的。
 +
 
 +
*底数是 0: 因为对数定义中 log 0是不允许的, 使用 if-else 判断和排除基数为''0''的问题。, 如果基数为零,指数为一个正数,答案为''0''0<sup>0</sup>0的0次方是悬而未决的,在某些领域定义为1、某些领域不定义(无意义),所以预先怎加一个判断以防止出现错误。
 +
 
 +
*底数是负数:比如 x<sup>p</sup> 其中 p(指数) 是一个整数,  x(底数) 是一个负数, 将无法使用对数方式来计算, 因为对数运算中 log(-1) 是不允许的。 ( 10<sup>x</sup> = -1是不存在的)。 解决这个问题的方法是:
 +
**使用循环乘法,只要指数是整数就可以使用循环乘法。
 +
**负指数幂等于把幂指数绝对值后所得的幂的倒数。也就是分解负指数并找出指数绝对值。此外,使用if块来判断指数p是奇数还是偶数(如果p mod 2 = 0,则p是偶数)。
  
*The base is negative: If one wants to be able to do something such as x<sup>p</sup> where p is an integer, and they want to allow x to be negative, then the solution with logarithms will not work, because log(-1) is undefined (there is no solution to 10<sup>x</sup> = -1). Ways to solve this are:
 
**Use the loop multiplication method, and even just use loop multiplication / division whenever the power is an integer.
 
** Factor out the negative and find the answer for the absolute value of the exponent, then divide 1 by it if the exponent is negative. Also, it's a good idea to use an if block for whether p is odd or even ( if p mod 2 = 0 then p is even).
 
 
<scratchblocks>
 
<scratchblocks>
 
set [result v] to ([abs v] of (base)) // used to spread out math over two lines
 
set [result v] to ([abs v] of (base)) // used to spread out math over two lines
Line 62: Line 68:
 
</scratchblocks>
 
</scratchblocks>
  
{{Note|Positive rational exponents, e.g. 10<sup>½</sup> ''do'' work. Thus, this can be used to find N-roots of numbers.}}
+
{{Note|正有理数, 例如 10<sup>½</sup> 意思是10的1次方再开2次方根. 因此,这可以用来计算根数。}}
  
==Editing Scratch==
+
==Scratch编辑==
 
{{obsolete feature|cat=no}}
 
{{obsolete feature|cat=no}}
 +
本文或部分积木块在Scratch(2.0)中未包含。
 +
 +
自定义积木块可以解决指数。相关的代码是:
  
A custom [[Blocks|block]] can be made in [[Squeak Tutorial|Squeak]] which solves exponents. The relevant codes are:
 
 
<pre>('%n ^ %n' #r #exp:of:)</pre>
 
<pre>('%n ^ %n' #r #exp:of:)</pre>
 
<pre>exp:t1 of:t2
 
<pre>exp:t1 of:t2
Line 73: Line 81:
 
</pre>
 
</pre>
  
==References==
+
==参考==
  
 
<references />
 
<references />

Latest revision as of 11:48, 9 April 2018

Translate: - English - Türkçe - 中文 

指数运算的结果。nm指将n自乘m次(针对m为正整数的场合)。把nm看作乘方的结果,叫做“n的m次幂”或“n的m次方”,其中,n称为“底数”,m称为“指数”(写成上标)。当不能用上标时,例如在编程语言或电子邮件中,通常写成n^m。例如:

23 = 2 * 2 * 2 = 8 读作:2的3次方,或2的3次幂。

大家可以使用这个积木块来让指数运算更容易编程: [1] (() ^ ():: operators)

在 Scratch中的计算方法

在Scratch中有多种方法来求解指数。这里有两个:

循环的方法

Warning Note: 此方法只能用于整数

这个脚本将能够在Scratch中解决指数; 但是,它不能计算小数:

when flag clicked
ask [What is the base number?] and wait
set [1st# v] to (answer)
ask [To what power?] and wait
set [2nd# v] to (answer)
set [ans v] to (1)
repeat ([abs v] of (2nd#))
set [ans v] to ((ans) * (1st#))
end
if <(2nd#) < [0]> then
set [ans v] to ((1) / (ans)) // a quick workaround for negative powers
end
say (join [The answer is: ] (ans)) for (2) secs

如何操作

  • 变量 "1st#" 是底数, 这是循环运算的数字。
  • 变量 "2nd#" 是指数,是程序里面循环乘法的次数。
  • 变量 "ans" 是答案, 底数"1st#"循环乘"2nd#"次后的答案。

对数法

对数法比循环法快得多。

([10 ^ v] of ((power) * ([log v] of ([abs v] of (base)))))

或者

([e ^ v] of ((power) * ([ln v] of ([abs v] of (base)))))

如何操作

  • 变量 "base" 底数, 变量 "power" 是指数, 或基数乘以的次数。返回值是答案。
  • 数学运算中: n * log m = log mn and 10log mn = mn.

特殊案例

这些都是错误,所以建议编程中注意:

  • 指数是0:当指数为零时,幂的值是有意义的。此时规定a0=1 叫作零指数幂,又叫零次幂。但是000是无意义的。
  • 底数是 0: 因为对数定义中 log 0是不允许的, 使用 if-else 判断和排除基数为0的问题。, 如果基数为零,指数为一个正数,答案为0; 000的0次方是悬而未决的,在某些领域定义为1、某些领域不定义(无意义),所以预先怎加一个判断以防止出现错误。
  • 底数是负数:比如 xp 其中 p(指数) 是一个整数, x(底数) 是一个负数, 将无法使用对数方式来计算, 因为对数运算中 log(-1) 是不允许的。 ( 10x = -1是不存在的)。 解决这个问题的方法是:
    • 使用循环乘法,只要指数是整数就可以使用循环乘法。
    • 负指数幂等于把幂指数绝对值后所得的幂的倒数。也就是分解负指数并找出指数绝对值。此外,使用if块来判断指数p是奇数还是偶数(如果p mod 2 = 0,则p是偶数)。
set [result v] to ([abs v] of (base)) // used to spread out math over two lines
set [result v] to ([e^ v] of ((power)*([ln v] of (result)))::operators)
if<(base) < [0]> then
set [result v] to ((1) / (result))
end
Warning Note: 正有理数, 例如 10½ 意思是10的1次方再开2次方根. 因此,这可以用来计算根数。

Scratch编辑

Archive.png This article or section documents a feature not included in the current version of Scratch (2.0). It is only useful from a historical perspective.

本文或部分积木块在Scratch(2.0)中未包含。

自定义积木块可以解决指数。相关的代码是:

('%n ^ %n' #r #exp:of:)
exp:t1 of:t2
^ t1 raisedTo: t2

参考

  1. http://scratch.mit.edu/discuss/topic/15646/