(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

 () / () `() / ()` 類別 運算類積木 形狀 橢圓形積木

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)

替換第 (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))`

## 應用

Main article: List of Block Workarounds

The block can be replicated with the following code:

```刪除第 (全部 v) 項 \( [dividend digits v] \)

變數 [count v] 設為 [1]

變數 [count v] 設為 [0]
end

變數 [count v] 改變 (1)
如果 <(letter (count) of (dividend)) = [.]> 那麼
變數 [dividend dec v] 設為 ((length of (dividend)) - (count))

新增項目 (字串中第 (count) 字\( (dividend) \)) \( [dividend digits v] \)
end
end

新增項目 [0] \( [dividend digits v] \)
end

變數 [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] 改變 (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)))))))
```

## 參見

Cookies help us deliver our services. By using our services, you agree to our use of cookies.