From Test-Scratch-Wiki

Translate: - English - 中文 
The correct title of this article is () / (). The change of name is due to technical restrictions.
() / ()
() / ()
类别 运算类积木
形狀 椭圆形积木

The () / () 积木 is an 运算类积木 and a 椭圆形积木. The block divides the second value from the first and returns the result.

If the first value is not evenly divisible by the second, the reported value will have decimals. To find the remainder instead, use the () Mod () block.

The numbers can be typed directly into the block, or Reporter blocks can be used instead.

This block can be stacked inside itself — this can be used to fit more numbers in.

Note, dividing by 0 in the online editor will return infinity, 0, or negative infinity. It depends on if the numerator is positive, 0, or negative; respectively. If attempted on an offline editor, it will give a Script Error and stops the script.

用法示例

In many 专案, numbers must be divided — this block will do the job.

Some common uses for the () / () block are as follows:

  • Calculator scripts
如果 <(operation) = [division]> 那麼 
  變數 [answer v] 設為 ((input1) / (input2))
end
  • Dividing lists of numbers
變數 [i v] 設為 (1)
重複 (清單 [list v] 的項目數 :: list) 次 
  替換第 (i) 項於 [list v] 成 (四捨五入數值 ((清單第 (i) 項項目\( [list v] \) :: list) / (2)))
end
  • Mathematical formulas
變數 [area v] 設為 (((base) * (height)) / (2)) // area of a triangle

科学计数法

Scratch 1.4 及更早版本,它有时会将非常大的数字转换成科学记数法以节省空间。科学计数法以 A*10B的方式书写,显示为AeB。这些可以通过对其执行任何数学运算(例如加法)转换为正常数字。如果有一个变量 "数字" 的值是 3*103 ,你想把它转换为正常数字,你可以这样做:

((数字) + (0))

它会返回"3000".

应用

Main article: List of Block Workarounds

The block can be replicated with the following code:

刪除第 (全部 v) 項 \( [dividend digits v] \)
刪除第 (全部 v) 項 \( [quotient v] \)
變數 [divident dev v] 設為 [0]
詢問 [Divident (x)] 並等待
變數 [dividend v] 設為 (詢問的答案)
詢問 [Divisor (y)] 並等待
變數 [divisor v] 設為 ([abs v] 數值 (詢問的答案))
變數 [no2 neg v] 設為 <(詢問的答案) < [0]>
如果 <(dividend) < [0]> 那麼 
  變數 [count v] 設為 [1]

  變數 [count v] 設為 [0]
end
變數 [no1 neg v] 設為 <(dividend) < [0]>
重複 (字串長度\( (dividend) \)) 次 
  變數 [count v] 改變 (1)
  如果 <(letter (count) of (dividend)) = [.]> 那麼 
    變數 [dividend dec v] 設為 ((length of (dividend)) - (count))
  
    新增項目 (字串中第 (count) 字\( (dividend) \)) \( [dividend digits v] \)
  end
end
重複 ((10) - (dividend dec)) 次 
  新增項目 [0] \( [dividend digits v] \)
end
如果 <(四捨五入數值 (divisor)) = (divisor)> 那麼 
  變數 [dec pos v] 設為 [0]

  變數 [count v] 設為 [0]
  重複直到 <(字串中第 (count) 字\( (dividend) \)) = [.]> 
    變數 [count v] 改變 (1)
  end
  變數 [dec pos v] 設為 ((字串長度\( (divisor) \)) - (count))
  變數 [dividend v] 設為 ((dividend) * ([10^ v] 數值 (dec pos)))
  變數 [divisor v] 設為 (四捨五入數值 ((divisor) * ([10^ v] 數值 (dec pos))))
end
變數 [count v] 設為 [0]
變數 [currently solving v] 設為 [] // That's an empty input, not a space.
重複 (清單 [dividend digits v] 的項目數 :: list) 次 
  變數 [count v] 改變 (1)
  變數 [currently solving v] 設為 (字串組合 (currently solving) 和 (清單第 (count) 項項目\( [dividend digits v] \) :: list))
  變數 [times v] 設為 [9]
  重複直到 <((divisor) * (times)) < ((currently solving) + (1))> 
    變數 [times v] 改變 (-1)
    新增項目 [times v] \( [quotient v] \)
    變數 [currently solving v] 設為 ((currently solving) - ((divisor) * (times)))
  end
  添加 [.] 為第 ((清單 [quotient v] 的項目數 :: list) - (8)) 項\( [quotient v] \)
  重複直到 <<<(清單第 (last v) 項項目\( [quotient v] \) :: list) = [.]> 或 <<<(清單第 (last v) 項項目\( [quotient v] \) :: list) = [0]> 且 <((四捨五入數值 (quotient :: list))) = (quotient :: list)>> 且 <清單 [quotient v] 包含 [.] ?>>> 不成立> 
    刪除第 (last v) 項 \( [quotient v] \)
    重複直到 <<<(清單第 (1 v) 項項目\( [quotient v] \) :: list) = [0]> 不成立> 或 <(清單第 (2 v) 項項目\( [quotient v] \) :: list) = [.]>> 
      刪除第 (1 v) 項 \( [quotient v] \)
    end
  end
end

The list "quotient" will contain the quotient.

A more concise workaround is this:

如果 <(b) < (0)> 那麼 
  變數 [result v] 設為 (((a) * ([e^ v] of ((-1) * ([ln v] of ((-1) * (b)))))) * (-1))

  變數 [result v] 設為 ((a) * ([e^ v] of ((-1) * ([ln v] of (b)))))
end

The above script can replace "(a) / (b)", and actually has a few distinct advantages over the original block: 1/0 is reported as "Infinity", instead of reporting an error and stopping the script -1/0 is reported as "-Infinity" 0/0 is reported as "Error!" rather than stopping the script

How it Works:
Let's look at the expression in the "else" part of the statement. This is the expression:

    result = a * e^ (-1*ln b) = a / b
    
    divided both sides by "a" and take the natural log of both sides to get:
    
    -1 * ln b = ln (1 / b) or ln(b^-1)
    
    The negative before a log can be rewritten as an exponent in the log, like this:
    
    a * log b = log (b^a)
    
    So: -1 * ln b = ln (b^-1)
    
    Because the log of a negative number does not exist, the formula in the "if" part of the
    statement pretends like "b" is positive and then just flips the answer.
    
    Because Scratch reports "ln 0" as -infinity, you can divide by zero using this expression
    and not get an error.

The following script replicates the block most exactly, as it is a reporter, and returns "Error!" for division by zero:

(((b) * ([e^ v] 數值 ((-1) * ([ln v] 數值 ([abs v] of (b)))))) * ((a) * ([e^ v] 數值 ((-1) * ([ln v] 數值 ([abs v] 數值 (a)))))))

参见